简体   繁体   English

从 ArrayList 存储和读取

[英]Storing & Reading from ArrayList

I took the Bernie's suggestion and cleaned up my program a little, and updated我接受了伯尼的建议并稍微清理了我的程序,并更新了

I am trying to create a java card game however, I am at the point where I need to calculate the number of wins of each player for all of the rounds.我正在尝试创建一个 Java 纸牌游戏,但是,我需要计算每个玩家在所有回合中的获胜次数。 My code currently looks like this,我的代码目前看起来像这样,

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();
 }

}

My other class looks like this,我的另一堂课看起来像这样,

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();
}
}

I must use an arraylist in order to count the number of times a player has a round, and then also be able to announce who has won the game.我必须使用 arraylist 来计算玩家在一轮中的次数,然后还可以宣布谁赢得了比赛。 I am still having issues with wrapping my head around how I would need to populate the list, in order to be able to determine who has won.我仍然无法确定我需要如何填充列表,以便能够确定谁赢了。

The way to win a round is by determining which player has the highest face rank, in the event that there is a tie then the rank of the suit will determine the winner of the round (between the two players).赢得一轮的方法是确定哪个玩家的面子排名最高,如果出现平局,则花色的排名将决定该轮的获胜者(在两个玩家之间)。

Example of what the completed program should output完成的程序应该输出什么的例子

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

There are so many ways to improve your code, but I'll try to answer your specific questions:有很多方法可以改进您的代码,但我会尝试回答您的具体问题:

1) Use a method which takes a startIndex . 1) 使用采用startIndex的方法。 Something like:就像是:

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();
}

Really, each deal should be a separate object, or separate arrays at the very least.实际上,每笔交易都应该是一个单独的对象,或者至少是单独的数组。 You're just going to find it more and more difficult keeping the data in a single array.您会发现将数据保存在单个数组中变得越来越困难。

2) I'd suggest writing a class to represent a round, but if you must use ArrayList s, yes, I'd advise a new ArrayList for each round. 2)我建议编写一个类来表示一轮,但如果您必须使用ArrayList ,是的,我建议为每一轮使用一个新的ArrayList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM