Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Last active December 15, 2016 17:22
Show Gist options
  • Save gitaficionado/9dfe2f8484341185a5c0cd13fbc85e0a to your computer and use it in GitHub Desktop.
Save gitaficionado/9dfe2f8484341185a5c0cd13fbc85e0a to your computer and use it in GitHub Desktop.
Programming Project 4.5
package chapter4;
public class PairOfDie {
private int _dieSides1;
private int _dieSides2;
private int _die1;
private int _die2;
PairOfDie(){
_dieSides1 = 6;
_dieSides2 = 6;
rollBothDie();
}
PairOfDie(int sides1, int sides2){
_dieSides1 = sides1;
_dieSides2 = sides2;
rollBothDie();
}
public void rollBothDie(){
_die1 = (int)(Math.random()*_dieSides1)+1;
_die2 = (int)(Math.random()*_dieSides2)+1;
}
public void rollDie1(){
_die1 = (int)(Math.random()*_dieSides1)+1;
}
public void rollDie2(){
_die2 = (int)(Math.random()*_dieSides1)+1;
}
public int getDie1(){
return _die1;
}
public int getDie2(){
return _die2;
}
public int getDieTotal(){
return _die1 + _die2;
}
}
package chapter4;
import java.util.*;
public class Pig {
public static void main (String args[]) {
PairOfDie dice = new PairOfDie();
int compScore = 0, playerScore = 0;
int compRound = 0, playerRound = 0;
while (compScore < 100 && playerScore < 100){
while(compRound < 20){
dice.rollBothDie();
System.out.print("The comp rolled a '" + dice.getDie1() + "' and a '" + dice.getDie2() + "'\n");
compRound += dice.getDieTotal();
if(dice.getDie1() == 1 && dice.getDie2() == 1){
compScore = compRound = 0;
break;
}
if(dice.getDie1() == 1 || dice.getDie2() == 1){
compRound = 0;
break;
}
}
compScore += compRound;//add the comps score
compRound = 0;
if(compScore > 100) break;
while(playerRound < 30){
dice.rollBothDie();
System.out.print("The player rolled a '" + dice.getDie1() + "' and a '" + dice.getDie2() + "'\n");
playerRound += dice.getDieTotal();
if(dice.getDie1() == 1 && dice.getDie2() == 1){
playerScore = playerRound = 0;
break;
}
if(dice.getDie1() == 1 || dice.getDie2() == 1){
playerRound = 0;
break;
}
}
playerScore += _________________;//add the players score
playerRound = ____;
}
if(playerScore > compScore)
System.out.print("\n_____________________!"); //Message to user if player won
else
System.out.print("\n______________________!"); //Message to user if computer won
// Lists user score and computer score
System.out.print("\n____________________ " + __________ + " and _______: " + compScore);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment