From 56c9fb92ee91159958dbc6fc1e583472aac33fdc Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 7 Oct 2023 16:15:00 -0700 Subject: [PATCH] lab 5 done --- .idea/Java-Lab-005.iml | 1 + .idea/kotlinc.xml | 6 ++++++ .idea/misc.xml | 1 - src/Dice.java | 21 +++++++++++++++++++++ src/Game.java | 34 ++++++++++++++++++++++++++++++++++ src/Player.java | 18 ++++++++++++++++++ 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .idea/kotlinc.xml create mode 100644 src/Dice.java create mode 100644 src/Player.java diff --git a/.idea/Java-Lab-005.iml b/.idea/Java-Lab-005.iml index c90834f..245d342 100644 --- a/.idea/Java-Lab-005.iml +++ b/.idea/Java-Lab-005.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..0dd4b35 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file 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/Dice.java b/src/Dice.java new file mode 100644 index 0000000..62f8dd7 --- /dev/null +++ b/src/Dice.java @@ -0,0 +1,21 @@ +import java.util.Random; + +public class Dice { + private int sideFacingUp; + private final int sides; + private final Random randomGenerator; + + // Constructor + public Dice(int sides) { + this.sides = sides; // this is a keyword for "this" object that's being created. + this.randomGenerator = new Random(); + } + + public void roll() { + this.sideFacingUp = this.randomGenerator.nextInt(this.sides) + 1; + } + + public int view() { + return this.sideFacingUp; + } +} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java index f4a3a3c..92244b1 100644 --- a/src/Game.java +++ b/src/Game.java @@ -5,10 +5,25 @@ 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( announceWinner() ); } public Player nextPlayer(Player current) { + if(current == this.p1){ + return this.p2; + } + return this.p1; } public void takeTurn(Player player) { @@ -16,8 +31,27 @@ public void takeTurn(Player player) { } public String announceWinner() { + System.out.printf("Player: %s Score: %d%n", this.p1.getName(), this.p1.getScore()); + System.out.printf("Player: %s Score: %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 "TIE"; + } } public static void main(String[] args) { + Scanner s = new Scanner(System.in); + System.out.println("How many sides is your dice:"); + int sides = Integer.parseInt(s.nextLine()); + Player p1 = new Player("Quinn"); + Player p2 = new Player("Trevor"); + Dice die = new Dice(sides); + 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..9bc1124 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,18 @@ +public class Player { + private final 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(); + } +} \ No newline at end of file