大家好,我试图让这个来计算4名玩家的胜利者(游戏是经典的纸牌游戏大战),但我只能做到两个。代码的第一位是我的Card类,第二位是主程序,其中有更多if语句用于计算胜利者。我遇到的问题是" getWinner"。如果需要,我也包括了甲板类(在底部)。 包裹warcard;
public class Card
{
private int value, suit;
private String result, suitStr;
/**
* @param num represents value of card
* @param type represents the suit of card
*/
public Card(int num, int type)
{
/**
* Class constructor.
*/
value = num;
suit = type;
switch(value)
{
case 1: result = "Ace";
break;
case 2: result = "Two";
break;
case 3: result = "Three";
break;
case 4: result = "Four";
break;
case 5: result = "Five";
break;
case 6: result = "Six";
break;
case 7: result = "Seven";
break;
case 8: result = "Eight";
break;
case 9: result = "Nine";
break;
case 10: result = "Ten";
break;
case 11: result = "Jack";
break;
case 12: result = "Queen";
break;
case 13: result = "King";
break;
default: result = "";
break;
}
switch(suit)
{
case 1: suitStr = "Clubs";
break;
case 2: suitStr = "Diamonds";
break;
case 3: suitStr = "Hearts";
break;
case 4: suitStr = "Spades";
break;
}
}
/**
* getters
*/
public int getNum()
{
return value;
}
public int getSuit()
{
return suit;
}
/**
* setters
*/
public void setVal(int choice)
{
value = choice;
}
public void setSuit(int choice)
{
suit = choice;
}
/**
* @param play represents Cards object
* @return flag the String result of the game
*/
public String getWinner(Card play)
{
String flag = "";
if(value == play.getNum())
{
if(suit > play.getSuit())
flag = "win";
else if (suit == play.getSuit())
flag = "tie";
else
flag = "lose";
}
else if (value > play.getNum())
flag = "win";
else
flag = "lose";
return flag;
}
/**
* toString method
*/
public String toString()
{
String info = "";
info = info + result + " of " + suitStr;
return info;
}
} /** end Cards class */
主程序
package warcard;
public class Warcard
{
public static void main(String[] args)
{
// instantiation of 2 decks
Deck hand1, hand2, hand3, hand4;
hand1 = new Deck();
hand2 = new Deck();
hand3 = new Deck();
hand4 = new Deck();
String result = "";
// shuffle the decks
hand1.shuffle();
hand2.shuffle();
hand3.shuffle();
hand4.shuffle();
/** tracker variables
* wins1 tracks user's wins
* wins2 tracks computer's wins
*/
int count = 0;
int wins1 = 0;
int wins2 = 0;
int wins3 = 0;
int wins4 = 0;
int ties = 0;
for (int i= 0; i < 52; i++)
{
Card tester = hand1.getCard(count);
Card tester2 = hand2.getCard(count);
Card tester3 = hand3.getCard(count);
Card tester4 = hand4.getCard(count);
count++;
System.out.println("Player 1's hand: " + tester);
System.out.println("Player 2's hand: " + tester2);
System.out.println("Player 3's hand: " + tester3);
System.out.println("Player 4's hand: " + tester4);
result = tester.getWinner(tester2);
if(result == "win")
{
System.out.println("Win!");
wins1++;
}
else if(result == "tie")
{
System.out.println("Tie!");
ties++;
}
else
{
System.out.println("Lose!");
wins2++;
}
} // end for loop
System.out.println();
System.out.println("Total score:");
System.out.println();
System.out.println("Player 1's wins: " + wins1 + "\n" + "Player 2's wins: " + wins2 + "\n" + "Player 3's wins: " + wins3 + "\n" + "Player 4's wins: " + wins4 + "\n" + "Ties: " + ties);
if (wins1 > wins2 && wins1 > wins3 && wins1 > wins4)
System.out.println("Player 1 won the game!");
else if(wins2 > wins1 && wins2 > wins3 && wins2 > wins4)
System.out.println("Player 2 wins!");
else if(wins3 > wins1 && wins3 > wins2 && wins3 > wins4)
System.out.println("Player 3 wins!");
else if(wins4 > wins1 && wins4 > wins2 && wins4 > wins3)
System.out.println("Player 4 wins!");
else if(wins1 == wins2 && wins1 == wins3 && wins1 == wins4)
System.out.println("It's a tie!");
} // end main
} // end class
甲板课
package warcard;
import java.util.Random;
public class Deck
{
private Card[] deck;
private int[] suit = {1, 2, 3, 4};
private int[] numbers = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
private int count;
public Deck()
{
deck = new Card[52];
count = 0;
for(int i = 0; i <= 3; i++)
{
for(int k = 2; k <= 14; k++)
{
deck[count] = new Card(k, suit[i]);
count++;
}
} /**
* end outer loop
*/
}
/**
* end constructor
*/
public Card getCard(int index)
{
return deck[index];
}
public void shuffle()
{
int rand;
Random mix = new Random();
for (int i = 0; i < deck.length; i++)
{
/**
* shuffles deck
*/
rand = mix.nextInt(deck.length);
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
} /**
* end for loop
*/
}
} /** end DeckCards class */
答案 0 :(得分:0)
目前,您只是比较2张牌的价值;您将tester
与tester2
进行比较,但不包括玩家3或玩家4的卡。不仅如此,你还没有返回哪个玩家赢了。相反,您只需指定p1是赢还是输。
您的String getWinner(Card)
方法需要更多参数,因此您可以将更多卡片传入其中。比较所有卡。它还应该返回哪个玩家赢了:
public String getWinner(Card p2, Card p3, Card p4) {
//compare against ALL cards
//return which player won
}
然后你会调用它,传递所有相反的牌,而不仅仅是tester2
:
result = tester.getWinner(tester2, tester3, tester4);
最后,你必须修好你的得分管理员。而不是检查“胜利”或“失败”,检查哪个玩家赢了:
/* could use a switch statement */
if(result == "p1") {
System.out.println("Player 1 wins!");
wins1++;
} else if(result == "p2") {
System.out.println("Player 2 wins!");
wins2++;
} else if(result == "p3") {
//...
}
如果我在电脑上,我会建议更多,例如将“战斗”封装在其他地方,而不是在第一个玩家的卡片内;一旦完成,我建议发布到CodeReview。如果您有任何问题,请告诉我。
答案 1 :(得分:-1)
据我所知,只有测试者牌组正在对着测试员2牌。如果那不是意图,原因是,你的4个套牌中只有2个是“竞争对手”#34;是的,其中只有2个玩,对吗?