我一直在为我的Java类介绍开发一个Uno游戏。该套牌功能齐全,所有代码均可编译。我在许多行上得到了运行时StackOverflowError。在制作新的Player或Game对象时,我会收到这些错误。
再次感谢您的帮助!
public class Game
{
private Player player1;
private Player player2;
private Player player3;
private Deck thisDeck;
private Card currentCard;
public Game()
{
player1 = new Player(1,0);
player2 = new Player(2,1);
player3 = new Player(3,1);
thisDeck = new Deck();
currentCard = null;
startGame();
}
public void startGame()
{
setCurrentCard(thisDeck.drawRandomCard());
System.out.println("The game has been started by drawing a random card and setting it as the current card");
currentCard.print();
}
public void setCurrentCard(Card newCurrentCard)
{
currentCard = newCurrentCard;
System.out.println("The current card has been changed");
}
public Player getPlayer1()
{
return player1;
}
public Player getPlayer2()
{
return player2;
}
public Player getPlayer3()
{
return player3;
}
public Card getCurrentCard()
{
return currentCard;
}
}
import java.util.ArrayList;
public class Player
{
private int playerID;
private int playerType; //0 is human, 1 is computer
private Deck thisDeck;
private Game thisGame;
private ArrayList<Card> hand;
private ArrayList<Card> handToPlay;
public Player(int pPlayerID, int pPlayerType)
{
playerID = pPlayerID;
playerType = pPlayerType;
thisDeck = new Deck();
thisGame = new Game();
hand = new ArrayList<Card>();
handToPlay = new ArrayList<Card>();
}
public void dealtAHand()
{
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
hand.add(thisDeck.drawRandomCard());
}
public void createHandToPlay()
{
Card currentCard = thisGame.getCurrentCard();
for(Card thisCard: hand)
{
if(thisCard.getColor().equals(currentCard.getColor()) || thisCard.getValue()== currentCard.getValue())
{
handToPlay.add(thisCard);
System.out.println("A card has been added to Hand To Play");
}
if(thisCard.getValue()==13 || thisCard.getValue()==14)
{
handToPlay.add(thisCard);
System.out.println("A wild card has been added to Hand To Play");
}
}
for(Card thisCard: handToPlay)
{
thisCard.print();
}
}
public void takeAComputerTurn()
{
createHandToPlay();
if(handToPlay.isEmpty())
{
drawACard();
}
else
{
playACard();
}
}
public void takeAHumanTurn()
{
createHandToPlay();
if(handToPlay.isEmpty())
{
drawACard();
System.out.println("As you had no cards to play in your hand, you have drawn a card");
}
else
{
System.out.println("Based on these available choices, which card would you like to play?");
}
}
public void playACard()
{
//int randomIndex = randomGenerator.nextInt(handToPlay.size());
//Card cardToPlay = handToPlay.get(randomIndex);
//System.out.println("The card to play has been randomly chosen");
Card cardToPlay = handToPlay.get(0);
System.out.println("The card to play has been chosen (first card in the ArrayList)");
thisGame.setCurrentCard(cardToPlay);
thisGame.getCurrentCard().print();
handToPlay.clear();
}
public void drawACard()
{
hand.add(thisDeck.drawRandomCard());
}
public int getPlayerType()
{
return playerType;
}
}
import java.util.ArrayList;
import java.util.Random;
public class Deck
{
private Card thisCard;
private ArrayList<Card> deck;
private ArrayList<String> colors;
private ArrayList<Integer> values;
private Random randomGenerator;
public Deck()
{
deck = new ArrayList<Card>();
randomGenerator = new Random();
initalizeColors();
initalizeValues();
createDeck();
}
private void initalizeColors()
{
colors = new ArrayList<String>();
colors.add("Red");
colors.add("Yellow");
colors.add("Green");
colors.add("Blue");
}
private void initalizeValues()
{
values = new ArrayList<Integer>();
values.add(0);
values.add(1);
values.add(2);
values.add(3);
values.add(4);
values.add(5);
values.add(6);
values.add(7);
values.add(8);
values.add(9);
values.add(10);
values.add(11);
values.add(12);
values.add(13);
values.add(14);
}
public void createDeck()
{
for(int thisValue:values)
{
if(thisValue==0)
{
for(String thisColor:colors)
{
thisCard = new Card(thisColor,thisValue);
deck.add(thisCard);
}
}
if(thisValue>=1 && thisValue<=9)
{
for(String thisColor:colors)
{
thisCard = new Card(thisColor,thisValue);
deck.add(thisCard);
thisCard = new Card(thisColor,thisValue);
deck.add(thisCard);
}
}
if(thisValue>=10 && thisValue<=12)
{
for(String thisColor:colors)
{
thisCard = new ActionCard(thisColor,thisValue);
deck.add(thisCard);
thisCard = new ActionCard(thisColor,thisValue);
deck.add(thisCard);
}
}
if(thisValue==13)
{
for(String thisColor:colors)
{
thisCard = new WildCard(null,thisValue);
deck.add(thisCard);
}
}
if(thisValue==14)
{
for(String thisColor:colors)
{
thisCard = new WildActionCard(null,thisValue);
deck.add(thisCard);
}
}
}
}
public Card drawCardByIndex(int index)
{
thisCard = deck.get(index);
deck.remove(index);
return thisCard;
}
public Card drawRandomCard()
{
int randomIndex = randomGenerator.nextInt(deck.size());
thisCard = drawCardByIndex(randomIndex);
return thisCard;
}
public void printDeckInfo()
{
for(Card thisCard:deck)
{
thisCard.print();
}
}
}
以下是我在终端中收到的错误(缩写为适合字符限制):
java.lang.StackOverflowError
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at Deck.initalizeColors(Deck.java:24)
at Deck.<init>(Deck.java:16)
at Player.<init>(Player.java:16)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
at Player.<init>(Player.java:17)
at Game.<init>(Game.java:12)
答案 0 :(得分:0)
这个答案是不完整的,因为代码通常很乱,而且几乎肯定还有其他问题。话虽如此,StackOverflowError
的原因是您的Game
构造函数创建了一个Player
对象,而您的Player
构造函数创建了一个Game
对象。这个错误可能会超过编译,但是一旦你运行这个代码,它就会爆炸。
public Game() {
player1 = new Player(1,0); // create a new Player
player2 = new Player(2,1);
player3 = new Player(3,1);
thisDeck = new Deck();
currentCard = null;
startGame();
}
public Player(int pPlayerID, int pPlayerType) {
playerID = pPlayerID;
playerType = pPlayerType;
thisDeck = new Deck();
thisGame = new Game(); // create a new Game (really??)
hand = new ArrayList<Card>();
handToPlay = new ArrayList<Card>();
}