Saturday, March 7, 2015

Auto Game
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































I am working on a dice game and I am having trouble with the autoGame method. The method is supposed to stop once the payer or the computer wins but it continues until both the player and computer have reached 100+. Not sure why this is happening. autoGame method is line 182.
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Java Code:












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































import javax.swing.JOptionPane;
/**************************************
* Game of dice pig
*
* @author
* @version March 9, 2015
**************************************/
public class PigGame
{
/** Dice */
private GVdie d1,d2;

/** Player Score */
private int pScore;

/** Computer Score */
private int cScore;

/** Round Score */
private int rScore;

/** Winning Score */
private final int WSCORE = 100;

/** Number of rounds */
private int rNum;

/** Is Player Turn */
private boolean pTurn;

/*********************************
Constructor
*********************************/
public PigGame(){
//Set initial values
System.out.println("Welcome to George's Game of Pig! \n");
pScore = 0;
cScore = 0;
rScore = 0;
rNum = 0;
d1 = new GVdie();
d2 = new GVdie();
}

private void rollDice(){
//Declare variables for values of both dice
int val1;
int val2;

//Roll both dice
d1.roll();
d2.roll();

//Assign the value rolled by each die to given value
val1 = d1.getValue();
val2 = d2.getValue();

//Set values of Round Score and Player Score depending on roll of dice
if (val1 == 1 || val2 == 1){
rScore = 0;
}
else{
rScore += val1 + val2;
}

System.out.println("" + val1 + " " + val2 + " Round Score: " + rScore);
}

public void playerRolls(){
//Invoke roll dice method
rollDice();

//Outputs for different situations
if(d1.getValue() + d2.getValue() == 2){
pScore = 0;
rScore = 0;
rNum ++;
System.out.println("---- Your Score: " + pScore);
System.out.println("");

}
else if(rScore == 0){
rNum ++;
System.out.println("---- Your Score: " + pScore);
System.out.println("");

}
else if (pScore + rScore >= WSCORE){
rNum ++;
System.out.println("---- Your Score: " + (pScore + rScore));
System.out.println("You won! \n");
System.out.println("Number of rounds: " + rNum);

}

}

public void playerHolds(){
//Calcluate players score, set round score back to 0, print players score
pScore += rScore;
rScore = 0;
rNum ++;
System.out.println("---- Your Score: " + pScore);
System.out.println("");
pTurn = false;

}

public void computerTurn(){
//Set rScore to 0;
rScore = 0;

//Continues to roll dice until condition met
do{
rollDice();
}while(d1.getValue() != 1 && d2.getValue() != 1 && rScore <= 19 && (cScore + rScore)<= WSCORE);

//Updates computer score
cScore += rScore;

//Conditions for computer and round score
if (d1.getValue() + d2.getValue() == 2){
cScore = 0;
rScore = 0;
pTurn = true;
}
else if (d1.getValue() == 1 || d2.getValue() == 1){
rScore = 0;
pTurn = true;
}
else if (cScore >= WSCORE){
System.out.println("Computer Wins! \n");
System.out.println("Number of rounds: " + (rNum + 1));

}

//Print computer score, reset round score to 0, increases round # by 1
System.out.println("---- Computer Score: " + cScore);
System.out.println("");
rScore = 0;
rNum ++;
pTurn = true;

}

private void playerTurn(){
//Set rScore to 0;
rScore = 0;

//Continues to roll dice until condition met
do{
rollDice();
}while(d1.getValue() != 1 && d2.getValue() != 1 && rScore <= 19 && (pScore + rScore) <= WSCORE);

//Updates computer score
pScore += rScore;

//Conditions for computer and round score
if (d1.getValue() + d2.getValue() == 2){
pScore = 0;
rScore = 0;
pTurn = false;
}
else if (d1.getValue() == 1 || d2.getValue() == 1){
rScore = 0;
pTurn = false;
}
else if (pScore >= WSCORE){
System.out.println("You win! \n");
System.out.println("Number of rounds: " + (rNum + 1));

}
//Print computer score, reset round score to 0, increases round # by 1
System.out.println("---- Your Score: " + pScore);
System.out.println("");
rScore = 0;
rNum ++;
pTurn = false;

}

public void autoGame(){

do{
playerTurn();
if(pTurn == false){
computerTurn();
}
}while(playerWon() != true || computerWon() != true);

reset();
}

/***************************************************************************
Reset Game
****************************************************************************/
public void reset(){
//Resets register totals to 0
String message = "Reset Game?";
int answer = JOptionPane.showConfirmDialog(null, message);
if (answer == JOptionPane.YES_OPTION) {
pScore = 0;
cScore = 0;
rScore = 0;
rNum = 0;

}
}

/***************************************************************************
Determine if it is player's turn

@return player turn
****************************************************************************/
public boolean isPlayerTurn(){
return pTurn;
}

/***************************************************************************
Get round score

@return round score
****************************************************************************/
public int getRoundScore(){
return rScore;
}

/***************************************************************************
Get player score

@return player score
****************************************************************************/
public int getPlayerScore(){
return pScore;
}

/***************************************************************************
Get computer score

@return computer score
****************************************************************************/
public int getComputerScore(){
return cScore;
}

/***************************************************************************
Get die value

@return die value
@param num
****************************************************************************/
public GVdie getDie (int num){
if(num == 1){
return d1;
}
else if(num == 2){
return d2;
}
return d1;
}

/***************************************************************************
Did player win

@return player won
****************************************************************************/
public boolean playerWon(){
if (pScore >= WSCORE){
return true;
}else{
return false;
}
}

/***************************************************************************
Did computer win

@return computer win
****************************************************************************/
public boolean computerWon(){
if (cScore >= WSCORE){
return true;
}else{
return false;
}
}
}









































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































No comments:

Post a Comment