我不知道如何编写main()类来测试该程序。
我应该呼叫哪个构造函数来启动此纸牌游戏?哪个变量
这是我第一次在OO编程中运行,我设法获得了我需要的所有构造函数和方法。如何编写main()
函数以运行所有代码?
public SimpaticoGame(DeckOfCards aDeck){
aDeck.shuffle();
DeckOfCards[] startingDecks = aDeck.dealCards(9);
playerDeck = startingDecks[0];
computerDeck = startingDecks[1];
playerHand = new StandardHand();
computerHand = new StandardHand();
playerWonLastRound = false;
}
/**
* Plays a round of the Simpactico card game, flipping each of the top cards and
* adding the cards to the winner's deck.
*/
public void playRound()
{
try{
playerLastCard = playerDeck.drawCard();
computerLastCard = computerDeck.drawCard();
playerHand.add(playerLastCard);
computerHand.add(computerLastCard);
}
catch (OutOfCardsException e) {
//Game is over.
return;
}
if(playerLastCard.compareTo(computerLastCard) == -1){
computerDeck.add(playerHand);
computerDeck.add(computerHand);
playerHand = new StandardHand();
computerHand = new StandardHand();
playerWonLastRound = false;
}
if(playerLastCard.compareTo(computerLastCard) == 1) {
playerDeck.add(playerHand);
playerDeck.add(computerHand);
playerWonLastRound = true;
}
if(playerLastCard.compareTo(computerLastCard) == 0){
try{
playerLastCard = playerDeck.drawCard();
computerLastCard = computerDeck.drawCard();
playerHand.add(playerDeck.drawCard());
computerHand.add(computerDeck.drawCard());
playRound();
}
catch (OutOfCardsException e)
{
//Game is over.
return;
}
}
}
答案 0 :(得分:2)
public static void main(String[]args){
DeckOfCards aDeck = new DeckOfCards(); //or whatever your DeckOfCards constructor looks like
SimpaticoGame thisGame = new SimpaticoGame(aDeck);
//creating a new instance of the game! whatever is in your constructor will happen
thisGame.playRound(); //round will be played
}
答案 1 :(得分:1)
不确定我是否了解,但是您可以尝试:
public class Main {
public static void main(String[] args) {
playRound();
}
}
答案 2 :(得分:1)
由于您的示例中缺少mroe类,因此我尝试了您的代码,但未编译。
但是与此同时,Java中的main方法如下:
public static void main(String... args) {
// Code you want to execute first
playRound(); // For example
}
只需将该方法放入您想要的任何类中,然后在其中添加一些逻辑并执行文件即可。如果您使用的是Eclipse和Netbeans之类的IDE,只需右键单击编辑器,然后单击即可运行。
答案 3 :(得分:0)
尝试将您的playRound()方法作为静态类。
public static void playRound()
以您的主角为
public static void main(String [] args)
{
playRound();
}