DiceGameCodeway

Codeway QA Engineer Case Study
mail@pastecode.io avatar
unknown
java
5 months ago
3.4 kB
1
Indexable
import java.util.Random;
import java.util.Scanner;

public class DiceGame {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        // Step 1: Ask for the target number of rounds
        int targetRounds = 0;
        while (true) {
            System.out.print("Enter the target number of rounds (1-99): ");
            targetRounds = scanner.nextInt();
            if (targetRounds >= 1 && targetRounds <= 99) {
                break;
            } else {
                System.out.println("Invalid input! Please enter a number between 1 and 99.");
            }
        }

        // Initialize player points
        int player1Points = 0;
        int player2Points = 0;
        int player3Points = 0;

        // Print initial header
        printLine(7);
        System.out.println("| ROUND | DICE1 | DICE2 | DICE3 | TOTAL1 | TOTAL2 | TOTAL3 |");
        printLine(7);

        // Main game loop
        for (int round = 1; round <= targetRounds; round++) {
            // Step 3: Roll dice for each player
            int player1Roll = random.nextInt(6) + 1;
            int player2Roll = random.nextInt(6) + 1;
            int player3Roll = random.nextInt(6) + 1;

            // Determine points according to the rules
            if (player1Roll == player2Roll && player2Roll == player3Roll) {
                // Case 4: All players get the same number
                player1Points += player1Roll;
                player2Points += player1Roll;
                player3Points += player1Roll;
            } else if (player1Roll != player2Roll && player2Roll != player3Roll && player1Roll != player3Roll) {
                // Case 5: All players get different numbers
                player1Points += player1Roll;
                player2Points += player2Roll;
                player3Points += player3Roll;
            } else {
                // Case 6: Two players get the same number, one gets different
                if (player1Roll == player2Roll) {
                    player1Points += 2 * player1Roll;
                    player2Points += 2 * player2Roll;
                    player3Points += player3Roll;
                } else if (player1Roll == player3Roll) {
                    player1Points += 2 * player1Roll;
                    player2Points += player2Roll;
                    player3Points += 2 * player3Roll;
                } else { // player2Roll == player3Roll
                    player1Points += player1Roll;
                    player2Points += 2 * player2Roll;
                    player3Points += 2 * player3Roll;
                }
            }

            // Step 7: Print current round details
            System.out.printf("| %5d | %5d | %5d | %5d | %6d | %6d | %6d |\n",
                    round, player1Roll, player2Roll, player3Roll, player1Points, player2Points, player3Points);
            printLine(7);
        }

        // Close scanner
        scanner.close();
    }

    // Helper method to print a dynamic line
    private static void printLine(int columns) {
        StringBuilder line = new StringBuilder("+");
        for (int i = 0; i < columns; i++) {
            line.append("-------+");
        }
        System.out.println(line.toString());
    }
}
Leave a Comment