I am building a simple card game based on game called War. It works fine with two players, but I need it to be modified to work with any amount of players entered by the user in the beginning. I am having trouble figuring out how to keep track of both players and scores since I no longer have concrete variables to work with (can't create a variable for every possible player so players are added dynamically). I've hit a wall and can't figure out where to go from here.
Here is my main method:
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());
}
}
This method checks the value of the card drawn and assigns a point value.
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;
}
My main question is what is the best way to handle an unknown amount of multiple players in a way that would allow me to keep track of scores and declare winners. What I have so far prints me the names and how many points they get each round but I don't have a way to manipulate or compare the values with each other.
I used dictionary
for the players to have the player key
linked with the score value
.
EDIT: Any solutions that do not include multiple classes are preferred.