diff --git a/.idea/misc.xml b/.idea/misc.xml index 78381ea..12fbc75 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,5 @@ - - + \ No newline at end of file diff --git a/src/Dice.java b/src/Dice.java new file mode 100644 index 0000000..4956f43 --- /dev/null +++ b/src/Dice.java @@ -0,0 +1,30 @@ +import java.util.Random; +import java.util.random.RandomGenerator; + +public class Dice { + private int sideFacingUp; + private int sides; + private Random randomGenerator; + + public Dice(int sides) { + this.sides = sides; + this.randomGenerator = new Random(); + } + + public int getSides() { + return this.sides; + } + + public int roll() { + this.sideFacingUp = this.randomGenerator.nextInt(this.sides) + 1; + return sideFacingUp; + } + + public int view() { + return this.sideFacingUp; + } +} + + + + diff --git a/src/Game.java b/src/Game.java index f4a3a3c..76da664 100644 --- a/src/Game.java +++ b/src/Game.java @@ -6,18 +6,40 @@ public class Game { Dice die; public void play() { - } + Player current = p1; + takeTurn(current); + current = nextPlayer(current); + takeTurn(current); + + System.out.print(announceWinner()); - public Player nextPlayer(Player current) { } public void takeTurn(Player player) { player.toss(this.die); } + public Player nextPlayer(Player current) { + if(current == this.p1){ + return this.p2; + }else { + return this.p1; + } + } public String announceWinner() { + System.out.printf("%s rolled %d%n%s rolled %d", p1.getName(), p1.getScore(), p2.getName(), p2.getScore()); + if(this.p1.getScore() > this.p2.getScore()){ + return String.format("%n%s won!", p1.getName());} + else if(this.p2.getScore() > this.p1.getScore()){ + return String.format("%n%s won!", p2.getName());} + else { + return String.format("%n%s and %s tied!", p1.getName(), p2.getName()); + } } + public Game(Player p1,Player p2,Dice die){ + this.p1= p1; + this.p2 = p2; + this.die = die; - public static void main(String[] args) { } } diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..041f3da --- /dev/null +++ b/src/Main.java @@ -0,0 +1,17 @@ +import java.util.Scanner; +public class Main { + 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 does the die you're playing with 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..5d2d88d --- /dev/null +++ b/src/Player.java @@ -0,0 +1,18 @@ +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(); + } + +}