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
2 changes: 1 addition & 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 {
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) + 1;
}

public int view() {
return this.sideFacingUp;
}
}
35 changes: 35 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,54 @@ public class Game {
Player p2;
Dice die;

public Game(Player p1, Player p2, Dice dice) {
}

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) {
player.toss(this.die);
}

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.p2.getScore() > this.p1.getScore()) {
return String.format("%s won", p2.getname());
} else {
return String.format("%s and %s 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 our die have? ");
int sides = Integer.parseInt(s.nextLine());

Game g = new Game(p1, p2, new Dice(sides));
g.play();
}
}
21 changes: 21 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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();
}
}