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.

25 changes: 25 additions & 0 deletions src/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Random;
/**
* @author Trevor Hartman
* @author MJ Fracess
*
* @since Version 1.0
*/
public class Dice {
private int sideFacingUp;
private int sides;
private Random randomGenerator;

// Constructor
public Dice(int sides) {
this.sides = sides; // this is a keyword for "this" object that's being created.
this.randomGenerator = new Random();
}
public void roll(){
this.sideFacingUp = this.randomGenerator.nextInt(sides) + 1;
}
public int view(){
return this.sideFacingUp;
}

}
42 changes: 41 additions & 1 deletion src/Game.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
import java.util.Scanner;

import java.util.Scanner;
/**
* @author Trevor Hartman
* @author MJ Fracess
*
* @since Version 1.0
*/
public class Game {
Player p1;
Player p2;
Dice die;

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

}

public void play() {
Player current = this.p1;
takeTurn(current);
current = nextPlayer(current);
takeTurn(current);
System.out.println("and the WINNER is:" + announceWinner());
}

public Player nextPlayer(Player current) {
if(current.getName().equals(this.p1.getName())){
return this.p2;
}
return this.p1;
}

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

public String announceWinner() {
System.out.printf("Player:%s, %d%n", this.p1.getName(), this.p1.getScore());
System.out.printf("Player:%s, %d%n", this.p2.getName(), this.p2.getScore());
if(p1.getScore()> p2.getScore()) {
return p1.getName();
}else if(p2.getScore()> p1.getScore()) {
return p2.getName();
}
return "TIE";
}

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print ("Please enter the number of sides:");
int sides = Integer.parseInt(s.nextLine());
Player p1 = new Player("MJ");
Player p2 = new Player("Mike");
Dice die = new Dice(sides);

Game game = new Game(p1, p2, die);
game.play();
}
}
28 changes: 28 additions & 0 deletions src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @author Trevor Hartman
* @author MJ Fracess
*
* @since Version 1.0
*/

public class Player {
private String name;
private int score;

//Constructor
public Player(String name) {
this.name = name;
this.score = 0;
}
public String getName(){
return name;
}
public int getScore() {
return score;
}
public void toss(Dice die) {
die.roll();
this.score = die.view();
}
}