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
3 changes: 1 addition & 2 deletions .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 {
private int sideFacingUp;
private int sides;
private Random randomGenerator;
public Dice(int sides) {
this.sides = sides;
this.randomGenerator = new Random();
}

public void roll(){
sideFacingUp = randomGenerator.nextInt(sides)+1;
}
public int view(){
return sideFacingUp;
}
}


46 changes: 39 additions & 7 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import java.util.Scanner;

public class Game {
Player p1;
Player p2;
Dice die;

public void play() {
private Player p1;
private Player p2;
private Dice die;

public Game(Player p1, Player p2, Dice die) {
this.p1 = p1;
this.p2 = p2;
this.die = die;
}

public Player nextPlayer(Player current) {
public void play() {
Player current = p1;
takeTurn(current);
current = nextPlayer(current);
takeTurn(current);
System.out.println(announceWinner());
}

public void takeTurn(Player player) {
player.toss(this.die);
player.toss(die);
}

public Player nextPlayer(Player player) {
if (player == p1) {
return p2;
} else {
return p1;
}
}

public String announceWinner() {
String result = "";
result += "Player 1: " + p1.getName() + ", Score: " + p1.getScore() + "\n";
result += "Player 2: " + p2.getName() + ", Score: " + p2.getScore() + "\n";
if (p1.getScore() > p2.getScore()) {
result += "The winner is " + p1.getName() + "!";
} else if (p1.getScore() < p2.getScore()) {
result += "The winner is " + p2.getName() + "!";
} else {
result += "It's a tie!";
}
return result;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("How many sides should the dice have?");
int sides = scanner.nextInt();
Game game = new Game(new Player("Alice"), new Player("Bob"), new Dice(sides));
game.play();
}
}
24 changes: 24 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Player {

private String name;
private int score;

public Player(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getScore() {
return score;
}

public void toss(Dice die) {
die.roll();
int view = die.view();
score += view;
}
}