From 16f0e5341cf40a6167c87bffbc9cfe6e04e8aa6e Mon Sep 17 00:00:00 2001 From: Cassandra Portlock Date: Wed, 18 Oct 2023 02:04:44 -0700 Subject: [PATCH] Completed Lab005 --- src/Dice.java | 28 ++++++++++++++++++++++++++++ src/Game.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/Player.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/Dice.java create mode 100644 src/Player.java diff --git a/src/Dice.java b/src/Dice.java new file mode 100644 index 0000000..e460ab3 --- /dev/null +++ b/src/Dice.java @@ -0,0 +1,28 @@ +import java.util.Random; + +/** + * + * @author Cassandra Portlock + * + * @since Version 1.0 + * + */ + +public 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 = this.randomGenerator.nextInt(this.sides); + } + + public int view(){ + return this.sideFacingUp; + } +} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java index f4a3a3c..75beeef 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,14 +1,38 @@ import java.util.Scanner; +/** + * + * @author Trevor Hartman + * @author Cassandra Portlock + * + * @since Version 1.0 + * + */ + public class Game { Player p1; 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 = p1; + takeTurn(current); + current = nextPlayer(current); + takeTurn(current); + System.out.println(announceWinner()); } public Player nextPlayer(Player current) { + if(current == this.p1) { + return this.p2; + } else { + return this.p1; + } } public void takeTurn(Player player) { @@ -16,8 +40,30 @@ public void takeTurn(Player player) { } public String announceWinner() { + System.out.printf("%s rolled %d%n%s rolled %d%n", p1.getName(), p1.getScore(), p2.getName(), p2.getScore()); + + if(this.p1.getScore() > this.p2.getScore()) { + return String.format("%s won!", p1.getName()); + } else if(this.p1.getScore() < this.p2.getScore()) { + return String.format("%s won!", p2.getName()); + } else { + return String.format("%s and %s have tied", p1.getName(), p2.getName()); + } } public static void main(String[] args) { + Scanner s = new Scanner(System.in); + System.out.println("Enter Player 1's Name:"); + String playerName = s.nextLine(); + Player p1 = new Player(playerName); + System.out.println("Enter Player 2's Name:"); + playerName = s.nextLine(); + Player p2 = new Player(playerName); + System.out.println("How many sides should the dice have?"); + int sides = Integer.parseInt(s.nextLine()); + + Game g = new Game(p1, p2, new Dice(sides)); + g.play(); + } } diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..e2145d1 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,29 @@ +/** + * + * @author Cassandra Portlock + * + * @since Version 1.0 + * + */ +public class Player { + private String name; + private int score; + + public Player(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } + + public int getScore(){ + return this.score; + } + + public void toss(Dice die){ + die.roll(); + this.score = die.view(); + } + +}