mardi 28 juillet 2020

Dice game between 2 players up to a score of 50

I am creating a game where two players are rolling dice against each other.

Two people (player A, player B) play a dice game. they roll a dice each round and the higher number wins the game and the winner earn 5 points.If two people roll the same value of dice, they both earn 3 points. The game ends if one player reaches 50 points or higher. If both players reach 50 at the same round, that becomes deuce and one more round of game to go until higher value player wins the game (if the 11th round still tie, play next round. Here is my code so far, but I am not getting any of the scores to go to 50 to end the game.

public static void main(String[] args) {
    
    int playerA[] = new int[10];
    int playerB[] = new int[10];
    int playerAScore = 0;
    int playerBScore = 0;
    int round = 1;
    
    for (int i =0; i < playerA.length; i++) {
        System.out.println("Roll the die for round " + round++);
        playerA[i] = (int) ((Math.random() * 6) + 1);
        playerB[i] = (int) ((Math.random() * 6) + 1);
    
        System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);
    
        if(playerA[i] == playerB[i]) {
            playerAScore = playerAScore + 3;
            playerBScore = playerBScore + 3;
        }
        else if (playerA[i] > playerB[i]) {
            playerAScore = playerAScore + 5;
        }
        else if (playerB[i] > playerB[i]) {
            playerBScore = playerBScore + 5;
        }
        if(playerAScore >= 50 || playerBScore >= 50) {
            break;
        }
    }
    
    System.out.println("The game is over.");
    
    if(playerAScore >= playerBScore)
        System.out.println("The winner is player A");
    else
        System.out.println("The winner is player B");
    
    System.out.println("How many rounds of game played?");
    System.out.println("Rounds played: " + round);
    
    System.out.println("Total Score per player:");
    System.out.println("Player A score: " + playerAScore);
    System.out.println("Player B score: " + playerBScore);
    
}

}




Aucun commentaire:

Enregistrer un commentaire