Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pigdice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}