Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Last active December 19, 2016 18:18
Show Gist options
  • Save gitaficionado/554009263b3bfcb37c896ee259b38327 to your computer and use it in GitHub Desktop.
Save gitaficionado/554009263b3bfcb37c896ee259b38327 to your computer and use it in GitHub Desktop.
public class ComputerPlayer extends Player {
public ComputerPlayer(String name, PairOfDice pod) {
super(name, pod);
}
@Override
public boolean rollAgain() {
if(sk.getRoundTotal() < 20) {
return ______;
}
return _______;
}
}
import java.util.Random;
public class Die {
_________ int value;
public Die() {
value = __;
}
public void roll() {
Random r = new Random();
value = r.nextInt(__) + 1;
}
public int getValue() {
return _____;
}
public String toString() {
return ("Die Value: " + value);
}
public static void main(String[] args) {
Die d = new Die();
d.roll();
String thedie = d.toString();
System.out.println(thedie);
}
}
import java.util.Scanner;
public class Game {
private Player __;
private Player p2;
public Game() {
Scanner s = new Scanner(System.in);
System.out.println("How many human players? [1/2]");
int i = s.nextInt();
while(i != 1 && i != 2) {
System.out.println("_____________________? [1/2]");
i = s.nextInt();
}
PairOfDice pod = new PairOfDice();
p1 = new HumanPlayer("Player 1", pod);
if(i == 2) {
p2 = new HumanPlayer("______________", pod);
} else {
p2 = new ComputerPlayer("Computer Player 2", pod);
}
}
public void play() {
Player curplayer = p1;
boolean result = false, again = false;
while(p1.isWinner() == false && p2.isWinner() == false) {
result = curplayer.rollDice();
if(result == true) {
again = curplayer.rollAgain();
} else {
again = false;
}
while(again == true && curplayer.isWinner() == false) {
result = curplayer.rollDice();
if(result == true) {
again = curplayer.rollAgain();
} else {
again = false;
}
}
curplayer.endRound();
System.out.println("Total Score of " + curplayer._________() + ": " + curplayer.___________());
System.out.println("*************");
if(!curplayer.isWinner()) {
System.out.println("Switching players");
}
if(curplayer == p1) {
curplayer = p2;
} else {
___________ = p1;
}
}
if(p1.isWinner()) {
System.out.println(p1.getName() + " _______");
} else {
System.out.println(p2.getName() + " ______!");
}
}
public static void main(String[] args) {
Game g = new Game();
g.play();
}
}
import java.util.Scanner;
public class HumanPlayer extends Player {
public HumanPlayer(String name, PairOfDice pod) {
super(name, pod);
}
@Override
public boolean rollAgain() {
Scanner s = new Scanner(System.in);
System.out.println("Would you like to roll again [y/n]? ");
String response = s.nextLine();
while(!response.equals("y") && !response.equals("n")) {
System.out.println("Would you like to roll again [y/n]? ");
response = s.nextLine();
}
if(response.equals("y")) {
return true;
}
// TODO Auto-generated method stub
return false;
}
public static void main(String[] args) {
HumanPlayer hp = new HumanPlayer("Bob", new PairOfDice());
System.out.println(hp.rollAgain());
}
public class PairOfDice {
Die d1;
Die d2;
public PairOfDice() {
d1 = new Die();
d2 = new Die();
}
public void roll() {
d1.roll();
d2.roll();
}
public boolean hasTwoOnes() {
if(d1.getValue() == 1 && d2.getValue() == 1) {
return true;
}
return false;
}
public boolean hasOneOne() {
if(d1.getValue() == 1 || d2.getValue() == 1) {
return true;
}
return false;
}
public int totalValue() {
return (d1.getValue() + d2.getValue());
}
public String toString() {
String d1string = d1.toString();
String d2string = d2.toString();
return d1string + "\n" + d2string;
}
public static void main(String[] args) {
PairOfDice pod = new PairOfDice();
pod.roll();
System.out.println(pod);
}
}
public abstract class Player {
protected PairOfDice pod;
protected String name;
protected ScoreKeeper sk;
public Player(String name, PairOfDice pod) {
this.name = name;
this.pod = pod;
sk = new ScoreKeeper();
}
public abstract boolean rollAgain();
public boolean rollDice() {
pod.roll();
System.out.println(pod);
if(pod.hasOneOne() == false) {
sk.addToRoundTotal(pod.totalValue());
if(sk.getRoundTotal() + sk.getGameTotal() >= 100) {
return false;
}
} else if(pod.hasTwoOnes() == true) {
System.out.println("You rolled two ones.");
sk.resetGameTotal();
return false;
} else {
System.out.println("You rolled a one.");
sk.resetRoundTotal();
return false;
}
return true;
}
public boolean isWinner() {
if(sk.getGameTotal() >= _____) {
return true;
}
return false;
}
public String getName() {
return _____;
}
public void endRound() {
sk.addToGameTotal();
}
public int getGameTotal() {
return sk.___________();
}
}
public class ScoreKeeper {
private int gametotal;
private int __________;
public ScoreKeeper() {
gametotal = roundtotal = 0;
}
public void addToGameTotal() {
gametotal += roundtotal;
resetRoundTotal();
}
public void addToRoundTotal(int value) {
roundtotal += _______;
}
public void resetGameTotal() {
gametotal = ___;
resetRoundTotal();
}
public void resetRoundTotal() {
roundtotal = 0;
}
public int getRoundTotal() {
return roundtotal;
}
public int getGameTotal() {
return gametotal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment