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
28 changes: 28 additions & 0 deletions src/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Random;

/**
*
* @author Cassandra Portlock
*
* @since Version 1.0
*
*/

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);
}

public int view(){
return this.sideFacingUp;
}
}
46 changes: 46 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,69 @@
import java.util.Scanner;

/**
*
* @author Trevor Hartman
* @author Cassandra Portlock
*
* @since Version 1.0
*
*/

public class Game {
Player p1;
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 = 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.p1.getScore() < this.p2.getScore()) {
return String.format("%s won!", p2.getName());
} else {
return String.format("%s and %s have 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 the dice have?");
int sides = Integer.parseInt(s.nextLine());

Game g = new Game(p1, p2, new Dice(sides));
g.play();

}
}
29 changes: 29 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
*
* @author Cassandra Portlock
*
* @since Version 1.0
*
*/
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();
}

}