Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Random;
public class Dice {
// Instance variables
private int sideFacingUp;
private int sides;
private Random randomGenerator;
// Constructor
public Dice(int sides) {
this.sides = sides;
this.randomGenerator = new Random();
}
// Roll method
public void roll() {
this.sideFacingUp = randomGenerator.nextInt(this.sides) + 1;
}
// View method
public int view() {
return this.sideFacingUp;
}
}
48 changes: 41 additions & 7 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,57 @@
import java.util.Scanner;

public class Game {
// instance variables
Player p1;
Player p2;
Dice die;

// constructor
public Game(Player p1, Player p2, Dice die) {
this.p1 = p1;
this.p2 = p2;
this.die = die;
}
// play method
public void play() {
Player current = this.p1;
takeTurn(current);
current = nextPlayer(current);
takeTurn(current);
announceWinner();
}

// next player method
public Player nextPlayer(Player current) {
if(current.equals(this.p1)) {
return this.p2;
}
return this.p1;
}

// take turn method
public void takeTurn(Player player) {
player.toss(this.die);
}

public String announceWinner() {
// announce winner method
public void announceWinner() {
System.out.printf("Name: %s Score: %d%n", this.p1.getName(), this.p1.getScore());
System.out.printf("Name: %s Score: %d%n", this.p2.getName(), this.p2.getScore());
if (p1.getScore() > p2.getScore()) {
System.out.println(this.p1.getName() + " is the winner!");
}
else if (p1.getScore() < p2.getScore()) {
System.out.println(this.p2.getName() + " is the winner!");
}
else if (p1.getScore() == p2.getScore()) {
System.out.println("It's a draw!");
}
}

// main method
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many sides on the die? ");
int sides = scan.nextInt();
Player play1 = new Player("Taylor");
Player play2 = new Player("Ann");
Dice d = new Dice(sides);
Game game = new Game(play1, play2, d);
game.play();
}
}
18 changes: 18 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -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();
}
}