From 75d9a8ed8ee08906b70aa425fb48fc9f47f48bc7 Mon Sep 17 00:00:00 2001 From: Daniel Faulkner Date: Sun, 1 Oct 2023 00:23:44 -0700 Subject: [PATCH] comment --- .idea/misc.xml | 1 - src/Game.java | 23 ---------- src/Main.java | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 24 deletions(-) delete mode 100644 src/Game.java create mode 100644 src/Main.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 78381ea..80154d2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/Game.java b/src/Game.java deleted file mode 100644 index f4a3a3c..0000000 --- a/src/Game.java +++ /dev/null @@ -1,23 +0,0 @@ -import java.util.Scanner; - -public class Game { - Player p1; - Player p2; - Dice die; - - public void play() { - } - - public Player nextPlayer(Player current) { - } - - public void takeTurn(Player player) { - player.toss(this.die); - } - - public String announceWinner() { - } - - public static void main(String[] args) { - } -} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..5f9257a --- /dev/null +++ b/src/Main.java @@ -0,0 +1,118 @@ +import java.util.Random; +import java.util.Scanner; + +class Dice { + private int sideFacingUp; + private int sides; + private Random randomGenerator; + + public Dice(int sides) { + this.sides = sides; + this.randomGenerator = new Random(); + } + + public void roll() { + this.sideFacingUp = randomGenerator.nextInt(sides) + 1; + } + + public int view() { + return this.sideFacingUp; + } +} + +class Player { + private String name; + private int score; + + public Player(String name) { + this.name = name; + this.score = 0; + } + + public String getName() { + return this.name; + } + + public int getScore() { + return this.score; + } + + public void toss(Dice die) { + die.roll(); + int rolledValue = die.view(); + this.score += rolledValue; + System.out.println(this.name + " rolled a " + rolledValue + "!"); + } +} + +class Game { + private Player p1; + private Player p2; + private Dice die; + public void takeTurn(Player player) { + player.toss(this.die); + } + + + public Game(Player p1, Player p2, Dice die) { + this.p1 = p1; + this.p2 = p2; + this.die = die; + } + + public void play() { + Player current = this.p1; + + while (true) { + takeTurn(current); + current = nextPlayer(current); + takeTurn(current); + + if (current.getScore() >= 100) { + break; + } + } + + String winner = announceWinner(); + System.out.println("Winner: " + winner); + } + + public Player nextPlayer(Player current) { + if (current == this.p1) { + return this.p2; + } else { + return this.p1; + } + } + + public String announceWinner() { + System.out.println("Player " + p1.getName() + "'s score: " + p1.getScore()); + System.out.println("Player " + p2.getName() + "'s score: " + p2.getScore()); + + if (p1.getScore() > p2.getScore()) { + return p1.getName(); + } else if (p2.getScore() > p1.getScore()) { + return p2.getName(); + } else { + return "It's a tie!"; + } + } +} + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of sides for the dice: "); + int sides = scanner.nextInt(); + scanner.nextLine(); // Consume the newline character + + Player player1 = new Player("Player 1"); + Player player2 = new Player("Player 2"); + Dice die = new Dice(sides); + + Game game = new Game(player1, player2, die); + game.play(); + + scanner.close(); + } +}