Skip to content
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Random;
public class Dice {
private int sideFacingUp;
private int sides;
private Random randomGenerator;

// Constructor
public Dice(int sides) {
this.sides = sides;
this.randomGenerator = new Random();
roll(); // Roll the dice upon instantiation to set an initial sideFacingUp value
}

// Method to roll the dice
public void roll() {
// Generate a random number between 1 and sides (inclusive)
sideFacingUp = randomGenerator.nextInt(sides) + 1;
}

// Method to view the current side facing up
public int view() {
return sideFacingUp;
}
}

49 changes: 48 additions & 1 deletion src/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,66 @@ public class Game {
Player p2;
Dice die;

// Constructor
public Game(Player p1, Player p2, Dice die) {
this.p1 = p1;
this.p2 = p2;
this.die = die;
}

public void play() {
Player current = this.p1;
takeTurn(current);
nextPlayer(current);
takeTurn(current);
announceWinner();
}

public Player nextPlayer(Player current) {
// Switch to the other player
if (current == this.p1) {
return this.p2;
} else {
return this.p1;
}
}

public void takeTurn(Player player) {
player.toss(this.die);
player.toss();
}

public String announceWinner() {
// Display each player's name and score
System.out.println(p1.getName() + "'s score: " + p1.getScore());
System.out.println(p2.getName() + "'s score: " + p2.getScore());

// Determine the winner
if (p1.getScore() > p2.getScore()) {
System.out.println("Winner: " + p1.getName());
return p1.getName();
} else if (p2.getScore() > p1.getScore()) {
System.out.println("Winner: " + p2.getName());
return p2.getName();
} else {
System.out.println("It's a tie!");
return "Tie";
}
}

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

// Request and store the number of sides for the die
System.out.print("Enter the number of sides for the die: ");
int numberOfSides = scanner.nextInt();

// Create two players and a die
Player player1 = new Player("Player 1", numberOfSides);
Player player2 = new Player("Player 2", numberOfSides);
Dice die = new Dice(numberOfSides);

// Create a new Game object and play the game
Game game = new Game(player1, player2, die);
game.play();
}
}
31 changes: 31 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Random;
public class Player {
private String name;
private int score;
private Dice dice;

// Constructor
public Player(String name, int numberOfSides) {
this.name = name;
this.score = 0;
this.dice = new Dice(numberOfSides);
}

// Method to get the player's name
public String getName() {
return name;
}

// Method to get the player's score
public int getScore() {
return score;
}

// Method to roll the die and record its view in the score
public void toss() {
// Roll the dice
dice.roll();


}
}