关于java:跟踪未定义的变量

Keeping track of undefined variables

我正在建立一个简单的纸牌游戏的基础上的游戏称为战争。它可以与两个玩家一起使用,但我需要对它进行修改,以便与用户在开始时输入的任何数量的玩家一起使用。我很难知道如何跟踪玩家和分数,因为我不再有具体的变量可以使用(不能为每个可能的玩家创建一个变量,所以玩家是动态添加的)。我撞到墙上了,不知道从这里到哪里去。

以下是我的主要方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
System.out.println("Welcome to War! How many players are playing?"); //User prompt
    Scanner scanner = new Scanner(System.in); //Scanner init
    int numberOfPlayers = scanner.nextInt();//set number of players to next integer entered

    ArrayList<String> deck = createDeck(numberOfPlayers);

    ArrayList<String> playerNames = new ArrayList<String>();
    Map<String, Integer> players = new HashMap<String, Integer>();

    System.out.println("Enter player name");

    for (int p = 0; p <= numberOfPlayers; p++) {
        String playerID = scanner.next();
        addPlayer(playerID);
        players.put(playerID, 100);
        playerNames.add(playerID);
    }

        for (int j = createDeck(numberOfPlayers).size(); j > 0; j = j - numberOfPlayers) {  //j is set to deck length. Increment down two number each loop.

            String readString = scanner.nextLine(); //Set variable"readString" to user input
            int score = 0;

            if (readString.equals("")) {    //If user input equals"enter"...

                ArrayList<Integer> currentScore = new ArrayList<Integer>();

                for (int q = 0; q <= numberOfPlayers; q++){

                    score = checkValue(deck);
                    String name = playerNames.get(q);
                    currentScore.add(score);
                    System.out.println(name +" drew a"+ deck.get(0) +" worth" + score +" points!"); //Print player 1's card


                    deck.remove(0); //Remove card
                    players.put(name, players.get(name).intValue() - score);
                }System.out.println(players.values());

            }
        }

此方法检查所绘制卡的值并指定点值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int currentScore = 0;

    boolean checkAce = deck.get(0).startsWith("Ace");
    boolean checkTwo = deck.get(0).startsWith("Two");
    boolean checkThree = deck.get(0).startsWith("Three");
    boolean checkFour = deck.get(0).startsWith("Four");
    boolean checkFive = deck.get(0).startsWith("Five");
    boolean checkSix = deck.get(0).startsWith("Six");
    boolean checkSeven = deck.get(0).startsWith("Seven");
    boolean checkEight = deck.get(0).startsWith("Eight");
    boolean checkNine = deck.get(0).startsWith("Nine");
    boolean checkTen = deck.get(0).startsWith("Ten");
    boolean checkJack = deck.get(0).startsWith("Jack");
    boolean checkQueen = deck.get(0).startsWith("Queen");
    boolean checkKing = deck.get(0).startsWith("King");


    if (checkAce) {
        currentScore = 14;
    }else if (checkTwo) {
        currentScore = 2;
    }else if (checkThree) {
        currentScore = 3;
    }else if (checkFour) {
        currentScore = 4;
    }else if (checkFive) {
        currentScore = 5;
    }else if (checkSix) {
        currentScore = 6;
    }else if (checkSeven) {
        currentScore = 7;
    }else if (checkEight) {
        currentScore = 8;
    }else if (checkNine) {
        currentScore = 9;
    }else if (checkTen) {
        currentScore = 10;
    }else if (checkJack) {
        currentScore = 11;
    }else if (checkQueen) {
        currentScore = 12;
    }else if (checkKing) {
        currentScore = 13;
    }


    return currentScore;

}

我的主要问题是什么是最好的方法来处理未知数量的多个玩家,这样我就可以跟踪分数并宣布胜出。到目前为止,我所拥有的打印出了它们的名称和每轮得到的点数,但我没有办法操作这些值或将它们相互比较。

我用dictionary让玩家把keyvalue的分数联系起来。

编辑:任何不包含多个类的解决方案都是首选。


你可以在每轮结束时对你的Hashmap进行排序。然后将第一个元素作为值打印出来。这是获得冠军的最简单方法。

例如,可以使用以下方法进行排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static <K,V extends Comparable<? super V>>
            List<Entry<K, V>> entriesSortedByValues(Map<K,V> map) {

    List<Entry<K,V>> sortedEntries = new ArrayList<Entry<K,V>>(map.entrySet());

    Collections.sort(sortedEntries,
            new Comparator<Entry<K,V>>() {
                @Override
                public int compare(Entry<K,V> e1, Entry<K,V> e2) {
                    return e2.getValue().compareTo(e1.getValue());
                }
            }
    );

    return sortedEntries;
}

那你就打电话给:entriesSortedByValues(players);

示例如下:根据值按降序对map进行排序

更新:通过比较scorebest的值,您可以为每轮中最好的玩家存储index,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int best = 0, index = 0;
for (int q = 0; q <= numberOfPlayers; q++){
   score = checkValue(deck);
   if (best < score){
      index = q;
      best = score;
   }
   String name = playerNames.get(q);
   currentScore.add(score);
   System.out.println(name +" drew a"+ deck.get(0) +" worth" + score +" points!"); //Print player 1's card
   deck.remove(0); //Remove card
   players.put(name, players.get(name).intValue() - score);
   }
System.out.println("Best player in this round:" + playerNames.get(index);