From b18603dc24e39c8ee3ad568c44d88b4ffdf02b89 Mon Sep 17 00:00:00 2001 From: Skyler Date: Wed, 18 Oct 2023 14:34:34 -0700 Subject: [PATCH] finished --- .idea/misc.xml | 2 +- src/Dice.java | 19 +++++++++++++++++++ src/Game.java | 41 +++++++++++++++++++++++++++++++++++++++++ src/Player.java | 22 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/Dice.java create mode 100644 src/Player.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 78381ea..862d09b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Dice.java b/src/Dice.java new file mode 100644 index 0000000..f04823f --- /dev/null +++ b/src/Dice.java @@ -0,0 +1,19 @@ +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 is a keyword for "this" object that's being created. + randomGenerator = new Random(); + } + public void roll(){ + sideFacingUp = randomGenerator.nextInt(sides) + 1; + } + public int view(){ + return sideFacingUp; + } +} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java index f4a3a3c..f8d3d2f 100644 --- a/src/Game.java +++ b/src/Game.java @@ -5,10 +5,26 @@ public class Game { Player p2; Dice 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; + takeTurn(current); + current = nextPlayer(current); + takeTurn(current); + System.out.println("Winner is " + announceWinner()); } public Player nextPlayer(Player current) { + if(current.getName().equals(this.p1.getName())){ + return this.p2; + } else { + return this.p1; + } } public void takeTurn(Player player) { @@ -16,8 +32,33 @@ public void takeTurn(Player player) { } public String announceWinner() { + System.out.printf("Player %s, %d%n", this.p1.getName(), this.p1.getScore()); + System.out.printf("Player %s, %d%n", this.p2.getName(), this.p2.getScore()); + if(this.p1.getScore() > this.p2.getScore()){ + return this.p1.getName(); + } else if(this.p1.getScore() < this.p2.getScore()) { + return this.p2.getName(); + } else{ + return "nobody."; + } } public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.print("Please enter the number of sides on the die: "); + int sides = Integer.parseInt(scan.nextLine()); + Dice die = new Dice(sides); + + //System.out.print("Please enter the number of sides on die 2: "); + //Dice d2 = new Dice(Integer.parseInt(scan.nextLine())); + + System.out.print("Please enter the name of player 1: "); + Player p1 = new Player(scan.nextLine()); + + System.out.print("Please enter the name of player 2: "); + Player p2 = new Player(scan.nextLine()); + + Game game = new Game(p1,p2,die); + game.play(); } } diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..a8fdce7 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,22 @@ +public class Player { + private String name; + private int score; + + public Player(String name) { + this.name = name; + score = 0; + } + + public String getName() { + return name; + } + + public int getScore() { + return score; + } + + public void toss(Dice die){ + die.roll(); + score = die.view(); + } +}