我接受了伯尼的建议,并稍微整理了程序,然后进行了更新
我正在尝试创建Java纸牌游戏,但是我需要计算所有回合中每个玩家的获胜次数。我的代码目前看起来像这样,
import java.util.ArrayList;
public class Play {
public static void main(String[] args) {
// initialize deck
Cards CardsObject = new Cards();
CardsObject.initializeDeck();
String[] deck = CardsObject.getdeck();
//shuffle
Cards ShuffleObject = new Cards();
ShuffleObject.shuffleDeck(deck);
//distribute deck
Cards distributeObject = new Cards();
distributeObject.distributeDeck(deck);
//print summary of rounds
int numRounds=1;
int hands=0;
System.out.println("Status of all deals after 13:");
while (numRounds<14){
System.out.print("Deal Number "+numRounds+": ");
printRounds(hands,deck);
numRounds++;
hands=hands+4;
}
//score
}
public static void printRounds(int startIndex, String[] deck){
System.out.print(deck[startIndex]);
for (int l=startIndex+1;l<startIndex+4;l++){
System.out.print(", "+deck[l]);
}
System.out.println();
}
}
我的另一堂课是这样的,
public class Cards {
String[] deck;
String[] suits = {"0","1","2","3"};
String[] ranks = {"2","3","4","5","6","7","8","9","10","11","12","13","14"};
int n = suits.length * ranks.length;
public void initializeDeck (){
deck = new String[n];
for (int i = 0; i < ranks.length; i++) {
for (int j = 0; j < suits.length; j++) {
deck[suits.length * i + j] = suits[j] + " " + ranks[i];
}
}
}
public void shuffleDeck(String[] deck){
for (int i = 0; i < n; i++) {
int r = i + (int)(Math.random() * (n - i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
}
public void distributeDeck(String[] deck){
for (int i = 0; i < 4; i++) {
System.out.println("Cards for Player " + (i + 1));
for (int j = 0; j < 13; j++) {
System.out.println(deck[i + j * 4] + " (Card " + (i + j * 4 + 1) + ")");
}
System.out.println();
}
}
public String[] getdeck() {
return deck.clone();
}
}
我必须使用arraylist来计算玩家获得回合的次数,然后才能宣布谁赢得了比赛。为了确定谁赢了,我仍然无法解决如何填充列表的问题。
赢得回合的方法是确定哪个球员的面孔排名最高,如果出现平局,则西服等级将确定回合的胜者(在两个球员之间)。
完整程序应输出的示例
Cards for Player 1
3 13(Card 1)
3 3(Card 5)
0 8(Card 9)
0 3(Card 13)
0 4(Card 17)
3 6(Card 21)
3 5(Card 25)
1 3(Card 29)
3 4(Card 33)
2 7(Card 37)
0 5(Card 41)
3 7(Card 45)
1 13(Card 49)
Cards for Player 2
2 6(Card 2)
1 2(Card 6)
2 14(Card 10)
1 10(Card 14)
1 14(Card 18)
2 3(Card 22)
1 8(Card 26)
1 11(Card 30)
0 9(Card 34)
0 14(Card 38)
0 11(Card 42)
0 7(Card 46)
1 12(Card 50)
Cards for Player 3
1 7(Card 3)
0 6(Card 7)
2 8(Card 11)
3 8(Card 15)
1 5(Card 19)
2 13(Card 23)
2 9(Card 27)
2 12(Card 31)
1 6(Card 35)
3 2(Card 39)
1 9(Card 43)
3 11(Card 47)
0 12(Card 51)
Cards for Player 4
2 5(Card 4)
1 4(Card 8)
3 14(Card 12)
2 11(Card 16)
2 10(Card 20)
3 12(Card 24)
2 2(Card 28)
0 13(Card 32)
0 10(Card 36)
3 10(Card 40)
0 2(Card 44)
3 9(Card 48)
2 4(Card 52)
Deal Number 01: 3 13, 2 6, 1 7, 2 5
Deal Number 02: 3 3, 1 2, 0 6, 1 4
Deal Number 03: 0 8, 2 14, 2 8, 3 14
Deal Number 04: 0 3, 1 10, 3 8, 2 11
... etc
Deal winners:
Player: No. of Deal(s) Won
1: 1
2: 6
3: 2
4: 4
Overall winner(s): Player-2
答案 0 :(得分:1)
有很多方法可以改善您的代码,但是我将尝试回答您的特定问题:
1)使用采用startIndex
的方法。像这样:
private void printDeal(int startIndex) {
System.out.print(deck[startIndex]);
for (int l = startIndex + 1; l < startIndex + 4; l++) {
System.out.print(", " + deck[l]);
}
System.out.println();
}
真的,每笔交易都应该是一个单独的对象,或者至少是一个单独的数组。您将发现越来越难以将数据保存在单个数组中。
2)我建议写一个类来代表一个回合,但是,如果必须使用ArrayList
,是的,我建议为每个回合使用一个新的ArrayList
。