I had a small problem, perhaps very simple, but I can’t solve it at all. A zombie dice game, the principle is this: each player rolls 3 dice (brains, shot, legs), you can throw several times, there are 2 options (throw, leave), if you leave, the move goes to another player. Only brains are saved. If 3 “shots” are typed, then the move automatically goes to another player, with this, all the bones are reset (the “brains” already saved). So, the transition of moves works fine for me, but now the “brains” are saved to all players at once, but it is necessary, naturally, that each player has his own number of “brains”. Please tell me how to do this. Here is my code, do not judge strictly, I'm new. Thank you in advance.
import java.util.*; import java.io.IOException; class zombiegame { public static void main(String[] args)throws IOException, InterruptedException { int i; Random randomDice = new Random(); int rollDice = 0; int brains = 0; int scoreBrains = 0; int shotgun = 0; int footprints = 0; int counter = 0; int howManyPlayers = 0; int currentPlayer = 0; Scanner kb = new Scanner(System.in); clearScreen(); System.out.println(" ______________ _________ _____ _____ ______ _____ _____ _____"); System.out.println("|___ / _ | \\/ || ___ \\_ _| ___| | _ \\_ _/ __ \\| ___|"); System.out.println(" / /| | | | . . || |_/ / | | | |__ | | | | | | | / \\/| |__"); System.out.println(" / / | | | | |\\/| || ___ \\ | | | __| | | | | | | | | | __|"); System.out.println("./ /__\\ \\_/ / | | || |_/ /_| |_| |___ | |/ / _| |_| \\__/\\| |___"); System.out.println("\\_____/\\___/\\_| |_/\\____/ \\___/\\____/ |___/ \\___/ \\____/\\____/"); System.out.println("\n"); System.out.println("Enter number of players: "); howManyPlayers = kb.nextInt(); int [] numOfPlayers = new int [howManyPlayers]; // Number of players String [] players = new String[howManyPlayers]; //names int [] score = new int[howManyPlayers]; for(i = 0;i < howManyPlayers;i++){ System.out.println("PLAYER " + (i + 1) + ", Please Enter Your Name:"); players[i] = kb.next(); } clearScreen(); System.out.print("PLAYERS ARE: "); for(i = 0;i<=players.length-1;i++){ System.out.print("---" + players[i] + "---"); } System.out.println("\n"); String [] Red = {"Shotgun","Shotgun","Shotgun","Footprints","Footprints","Brain"}; String [] Yellow = {"Shotgun","Shotgun","Footprints","Footprints","Brain","Brain"}; String [] Green = {"Shotgun","Footprints","Footprints","Brain","Brain","Brain"}; while(true){ System.out.println("PLAYER " + players[currentPlayer] + ", PRESS 1 TO ROLL THE DICE:"); rollDice = kb.nextInt(); System.out.println("\n"); int random = randomDice.nextInt(6); if(rollDice == 1){ //--------------RED DICE--------------- if(Red[random] == "Brain"){ brains++; scoreBrains++; } else if(Red[random] == "Shotgun"){ shotgun++; System.out.println("+1 Shotgun RED"); } else if(Red[random] == "Footprints"){ footprints++; } //--------------YELLOW DICE--------------- if(Yellow[random] == "Brain"){ brains++; scoreBrains++; } else if(Yellow[random] == "Shotgun"){ shotgun++; System.out.println("+1 Shotgun YELLOW"); } else if(Yellow[random] == "Footprints"){ footprints++; } //--------------GREEN DICE--------------- if(Green[random] == "Brain"){ brains++; scoreBrains++; } else if(Green[random] == "Shotgun"){ shotgun++; System.out.println("+1 Shotgun GREEN"); } else if(Green[random] == "Footprints"){ footprints++; } clearScreen(); if((Red[random] == "Shotgun" && Yellow[random] == "Shotgun" && Green[random] == "Shotgun") || shotgun >= 3){ System.out.println("YOU'VE ROLLED 3 SHOTGUNS, NEXT PLAYER'S TURN"); scoreBrains = scoreBrains - brains; // Keeping only previously saved brains. brains = 0; // reseting dice shotgun = 0;// reseting dice footprints = 0;// reseting dice //System.out.println("Current Score for " + players[currentPlayer] + ": " + scoreBrains); currentPlayer++; // At the start of the game currentPlayer is 0, because first index in players array is 0, PLAYER 0. //Adding 1 switches to second index of an array, players[1] or PLAYER 1 and so on, until it reaches the last index of an array. if(currentPlayer == howManyPlayers){ // When the last player gets 3 shotguns,it means that the end of players array is reached. //This statement checks that the last index of players array is reached, // if so, it jumps back to the first index (players[currentPlayer] which now is players[0]) currentPlayer = 0; } } System.out.print("[ " + Red[random] + "[RED] ] " ); System.out.print("[ " + Yellow[random] + "[YELLOW] ] "); System.out.print("[ " + Green[random] + "[GREEN] ] "); System.out.println("\n"); System.out.println("-------------------------------------------------------"); System.out.println(players[currentPlayer] + "'s score is: " + brains + " Brains " + footprints + " Footprints " + shotgun + " Shotguns"); System.out.println("-------------------------------------------------------"); System.out.println("\n"); for(i = 0;i < numOfPlayers.length;i++){ score[i] = scoreBrains; System.out.println(players[i] + " score: " + score[i]); } } // ------------END OF IF (ROLLDICE == 1)---------------- if(rollDice == 2){ brains = 0; shotgun = 0; footprints = 0; System.out.println("KEEPING THE ROLLED BRAINS AND SWITCHING TO NEXT PLAYER"); //System.out.println("Current Score for " + players[currentPlayer] + ": " + scoreBrains); //scoreBrains = 0; currentPlayer++; // At the start of the game currentPlayer is 0, because first index in players array is 0, PLAYER 0. //Adding 1 switches to second index of an array, players[1] or PLAYER 1 and so on, until it reaches the last index of an array. if(currentPlayer == howManyPlayers){ // When the last player gets 3 shotguns,it means that the end of players array is reached. //This statement checks that the last index of players array is reached, // if so, it jumps back to the first index (players[currentPlayer] which now is players[0]) currentPlayer = 0; } } // --------------END OF IF (ROLLDICE == 2)------------- } // END OF WHILE LOOP } private static void clearScreen() throws IOException, InterruptedException { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); } }