2013-12-18 103 views
0

在我的遊戲代碼中,我試圖添加一張牌。只要我這樣做,我的陣列就出界了。一切看起來不錯,但也許我錯過了一些東西。Java數組索引超出界限?

僅供參考,一個和兩個是播放器實例。從主類相關的代碼(約格式化對不起,我吮吸將其轉移到堆棧溢出):

import java.util.*; 

public class Program { 

    public static void main(String args[]) { 
     String[] rank = {"two", "three", "four", "five", "six", "seven", "eight", 
        "nine", "ten", "jack", "queen", "king", "ace"}; 
     String[] suit = {"hearts", "diamonds", "spades", "clubs"}; 
     Scanner scan = new Scanner(System.in); 
     String something = "yes", something2 = "yes"; //Use with while loop 
     String winner = "yes"; //Use for while loop 
     String temp; //Use with setting names 
     Card[] deck = new Card[52]; //Deck array 
     int playercount = 0; 
     Player one = new Player("temp"); 
     Player two = new Player("temp"); 
     Player three = new Player("temp"); 
     Player four = new Player("temp"); 

     while (something2.equals("yes") || playercount < 2) { //Add players to game 

      System.out.println("Would a(nother) player like to join?"); 
      something2 = scan.nextLine(); 
      System.out.println(); 
      if (something2.equals("yes")) { 
       if (playercount <= 4) { 
        if (playercount == 0) { 
         System.out.println("What is your name: "); 
         Player one1 = new Player(scan.nextLine()); 
         one = one1; 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 1) { 
         System.out.println("What is your name: "); 
         Player two2 = new Player(scan.nextLine()); 
         two = two2; 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 2) { 
         System.out.println("What is your name: "); 
         Player three3 = new Player(scan.nextLine()); 
         three = three3; 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 3) { 
         System.out.println("What is your name: "); 
         Player four4 = new Player(scan.nextLine()); 
         four = four4; 
         playercount++; 
         System.out.println(); 
        } 
        else {System.out.println("Only four players are allowed."); 
         something2 = "no";} 
       } 
      } 
      else if (playercount < 2) { 
       System.out.println("You need at least two players..."); 
       System.out.println(); 
      } 
      else something2 = "no"; 
     } 

     //Start game 
     while (something.equals("yes")) { 
      //Prepare game 
      Card.makeDeck(deck, rank, suit); 
      deck = Card.getDeck(); 
      Card.shuffle(deck); 
      deck = Card.getDeck(); 

      //Deal cards 
      if (playercount == 2) { 
       for (int i = 1; i < 8; i++) { 
        one.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        two.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
       } 
      } 
      else if (playercount == 3) { 
       for (int i = 1; i < 8; i++) { 
        one.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        two.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        three.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
       } 
      } 
      else { 
       for (int i = 1; i < 8; i++) { 
        one.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        two.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        three.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
        four.addCard(Card.draw(deck)); 
        deck = Card.getDeck(); 
       } 
      } 
     } 
    } 
} 

卡類:

import java.util.*; 

public class Card { 
    private String suit; 
    private String rank; 
    private static int temp = 0, temp2 = 0; //Use for reseting rank and suit 
    private static Card temp3; //Use for draw method 
    private static int temp4; //Use for shuffle method 
    private static Card[] deck = new Card[52]; 

    //Constructors 
    public Card() { 
     this.rank = "two"; 
     this.suit = "hearts"; 
    } 
    public Card(String r, String s) { 
     this.rank = r; 
     this.suit = s; 
    } 

    //Mutators 
    //Make deck 
    public static void makeDeck(Card[] c, String[] r, String[] s) { 
     for (int i = 0; i < c.length; i++) { 
      c[i] = new Card(r[temp], s[temp2]); 
      temp++; temp2++; 
      //Reset rank and suit 
      if (temp > 12) 
       temp = 0; 
      if (temp2 > 3) 
       temp2 = 0; 
     } 
     deck = c; 
    } 

    //Accessors 
    //Return deck 
    public static Card[] getDeck() { 
     return deck; 
    } 
    //Shuffle 
    public static Card[] shuffle(Card[] c) { 
     for (int i = 0; i < c.length; i++) { 
      int rand = (int)(Math.random()*(i + 1)); 
      //Don't let anything be in a slot that doesn't exist 
      while (rand > c.length) { 
       temp4 = (int)Math.random(); 
       rand -= temp4; 
      } 
      if (rand < 0) 
       rand += temp4; 
       Card temp = c[i]; 
       c[i] = c[rand]; 
       c[rand] = temp; 
     } 
     deck = c; 
     return deck; 
    } 
    //Draw 
    public static Card draw(Card[] c) { 
     if (c != null) { 
     for (int i = 0; i < c.length; i++) { 
     if (c[i] != null) { 
      try { 
        return c[i]; 
       } finally { 
        c[i] = null;} //Remove i from c 
      } 
     } 
     }  
     return null; 
    } 
} 

Player類:

import java.util.*; 

public class Player { 
    private String name; 
    private Card[] hand = new Card[52]; 
    private int handsize = 0; 

    //Constructor 
    public Player(String n) { 
    name = n; 
    } 

    //Mutators 
    public void addCard(Card c) { 
     hand[handsize] = c; 
     handsize++; 
    } 

    //Accessors 
    public String getName() { 
     return name; 
    } 
    public Card[] getHand() { 
     return hand; 
    } 
} 
+2

與您的錯誤沒有關係,但這些Player方法/變量都不應該是靜態的。 –

+0

我不認爲你的錯誤出現在你發佈的代碼中。你能展示你的整個程序嗎? –

+0

@DavidWallace https://compilr.com/bottomsniffer/final/Card.java – William

回答

0

問題是與你的循環

while (something.equals("yes")) 

有沒有什麼設置something爲任何其他值的,所以這個循環只是善有善報不休,直到所有的球員都有超過52張牌。一旦有人擁有超過52張卡片,添加一張新卡片會導致異常。

我認爲你需要刪除這個while。它內部的代碼只能運行一次。

+0

謝謝。我會擺脫它,看看這是否解決了我的問題,但我需要它能夠重放我的遊戲。 – William

+0

我才意識到我的一些代碼失蹤,當我跑它由於某種原因..我補充回來,我仍然得到同樣的錯誤......它不是while循環。 – William

+0

你的意思是,它不是while循環。 @David提供了明確的解釋,這是嚴重的問題。如果你想重播你的遊戲,你必須重新在玩家手中的牌(即重置handsize爲0每while循環(如果你認爲球員應該只有8在給定的時間卡) – Mani

1

你平局方法被破壞。

// get the first non-null Card from the cards "c". 
public static Card draw(Card[] c) { 
    if (c != null) { 
    for (int i = 0; i < c.length; i++) { 
     if (c[i] != null) { 
     try { 
      return c[i]; 
     } finally { 
      // now remove element i from the `c` array. 
      c[i] = null; 
     } 
     } 
    } 
    } 
    return null; 
} 
+0

原始'draw'方法破壞的方式是什麼?對我來說這看起來很好,除了它會在牌組中創建大量最後一張牌的副本(這不會導致ArrayIndexOutOfBoundsException)。 –

+0

@DavidWallace你看不到線路'c = deck;'的問題? –

+0

只是把它放在裏面,我在同一個地方也有同樣的錯誤。它發生在我的玩家類的手裏[handsize] = c; 其他部分在我所在的位置one.addCard(...); – William

0

我覺得你的代碼的順序是不正確的(很難用這個代碼告訴)

for (int i = 1; i < 8; i++) 
{ 
one.addCard(Card.draw(deck)); 
deck = Card.getDeck(); 
two.addCard(Card.draw(deck)); 
deck = Card.getDeck(); 
} 

也許應該

for (int i = 1; i < 8; i++) 
{ 
deck = Card.getDeck(); 
one.addCard(Card.draw(deck)); 
deck = Card.getDeck(); 
two.addCard(Card.draw(deck)); 
} 

更新

而且

public void addCard(Card c) { 
    hand[handsize] = c; 
    handsize++; 
} 

handsize永遠不會增加 - 它始終是0

+0

我有一個 甲板= Card.getDeck(); 但它沒有顯示。我的整個代碼是https://compilr.com/bottomsniffer/final/Program.java – William

+0

對不起,我不希望在另一個網站在這裏爲了回答您的問題進行登記。爲什麼不發佈相關的代碼。 –

+0

我將現在發佈全部 – William