From ea4d4d850d42b793d549bf3f762312a56ca2e41f Mon Sep 17 00:00:00 2001 From: Chaseungjun Date: Mon, 6 Mar 2023 16:43:20 +0900 Subject: [PATCH] =?UTF-8?q?pig=20dice=20game=EC=97=90=EC=84=9C=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EA=B0=80=20=EB=82=98=EC=98=AC=20=EB=95=8C=EA=B9=8C?= =?UTF-8?q?=EC=A7=80=20=EA=B2=8C=EC=9E=84=EC=9D=84=20=EB=B0=98=EB=B3=B5?= =?UTF-8?q?=ED=95=98=EB=8A=94=20Game=20loop=20=EB=B6=80=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigdice.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 pigdice.java diff --git a/pigdice.java b/pigdice.java new file mode 100644 index 0000000..a54e892 --- /dev/null +++ b/pigdice.java @@ -0,0 +1,29 @@ +public class pigdice { + + // Game loop + int currentPlayerIndex = 0; + int turnScore = 0; + boolean gameOver = false; + while (!gameOver) { + // Roll the dice + int roll = (int)(Math.random() * 6) + 1; + // Update turn score + if (roll == 1) { + turnScore = 0; + System.out.println(players[currentPlayerIndex] + " rolled a 1. Turn over."); + currentPlayerIndex = (currentPlayerIndex + 1) % 2; + } else { + turnScore += roll; + System.out.println(players[currentPlayerIndex] + " rolled a " + roll + "."); + System.out.println("Turn score: " + turnScore); + System.out.print("Roll again? (y/n) "); + String rollAgain = input.nextLine(); + if (rollAgain.toLowerCase().equals("n")) { + scores[currentPlayerIndex] += turnScore; + turnScore = 0; + System.out.println(players[currentPlayerIndex] + "'s turn over. Score: " + scores[currentPlayerIndex]); + currentPlayerIndex = (currentPlayerIndex + 1) % 2; + } + } + } +}