0

There is a logical error somewhere in the game that has to do with my win and lose boolean values making the game print out both a win or loss on the first roll, as well as the end value always being true, but I'm not sure where it is.

My best guess is around where it checks for the win or loss conditions.

package bergman.java.ass2;

import java.util.Random;

/**
 *
 * @author Jason
 */
public class GameSimulation {

public static void main(String[] args) {
//create dice
    int die1, die2;
//create the counters for the wins and the times the program runs
    int runsCounter, winsCounter = 0, loseCounter = 0;
//Create the variable to check whether it is the initial roll or not
    boolean firstRoll;
//create a variable to store the initial point of the roll
    int rollPoint = 0;
//Create the variable to store the random number 
    Random rand = new Random();
//Create a variable to check whether a win/loss is true or not
    boolean win = false, lose = false;
for(;runsCounter < 10000;){
//roll the dice for initial roll
        firstRoll = true;
        die1 = rand.nextInt(6) + 1;
        die2 = rand.nextInt(6) + 1;

//check if it's the first roll
        if (firstRoll = true) {
    //if it's a 7 or 11 on first roll add a win
            if (die1 + die2 == 7 || die1 + die2 == 11) {
                System.out.print("you win! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                win = true;

            }
    //if it's a loss on the first roll add a loss
            if (die1 + die2 == 2 || die1 + die2 == 3 || die1 + die2 == 12) {
                System.out.print("You lose! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                lose = true;

            } 
    //if it's neither, store the roll and end the loop
            else {
                rollPoint = die1 + die2;
                firstRoll = false;
            }
        }
    //check if first roll was a win or loss, if not continue rolling
        for (; win == false && lose == false;) {
            die1 = rand.nextInt(6) + 1;
            die2 = rand.nextInt(6) + 1;
    //check if the new roll matches point or 7
            if (die1 + die2 == rollPoint) {
                win = true;
            } else if (die1 + die2 == 7) {
                lose = true;
            }
        }

    //utilize win/loss statements within loop
        if (win = true) {
            winsCounter = winsCounter++;
            System.out.print("You win with rolls " + die1
                    + " and " + die2 + " with a point roll for " + rollPoint);
            win = false;
        } else if (lose = true) {
            System.out.print(" you lose by rolling a 7 before point score");
            loseCounter = loseCounter++;
            lose = false;
        }
      }
    }

}
Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

-1

There are a few issues with this code, one being that you're doing assignment inside the if statements that will always evaluate to true when you should be doing a comparison. These parts:

if (firstRoll = true) {...
if (win = true) {...
} else if (lose = true) {...

should be

if (firstRoll == true) {...
if (win == true) {...
} else if (lose == true) {...

I believe this is your logic issue as once you reach the end, even if lose is true and win is false, you set win to true and only go inside the body of the win statement.

Another is you're never incrementing runsCounter so the line

for(;runsCounter < 10000;)

is equivalent to

while(true)

to prevent the program from looping forever and do 10,000 runs you need

for(;runsCounter < 10000; runsCounter++)

Then to keep track of wins/losses you need to change

winsCounter = winsCounter++;

to

winsCounter++;

as well as

loseCounter = loseCounter++;

to

loseCounter++;

The issue here is, for example, winsCounter initially is 0, when you do winsCounter++ you create a copy which has the current value (0), then you increment winsCounter to 1, then return the copy (0) and set winsCounter to 0. more information on this

Here is the modified code

public static void main(String[] args) {
//create dice
int die1, die2;
//create the counters for the wins and the times the program runs
int winsCounter = 0, loseCounter = 0;
//Create the variable to check whether it is the initial roll or not
boolean firstRoll;
//create a variable to store the initial point of the roll
int rollPoint = 0;
//Create the variable to store the random number 
Random rand = new Random();
//Create a variable to check whether a win/loss is true or not
boolean win = false, lose = false;
for(int runsCounter = 0;runsCounter < 10000; runsCounter++){
//roll the dice for initial roll
    firstRoll = true;
    die1 = rand.nextInt(6) + 1;
    die2 = rand.nextInt(6) + 1;

//check if it's the first roll
    if (firstRoll == true) {
    //if it's a 7 or 11 on first roll add a win
        if (die1 + die2 == 7 || die1 + die2 == 11) {
            System.out.print("you win! with rolls " + die1 + " and "
                    + die2 + "\n");
            firstRoll = false;
            win = true;

        }
//if it's a loss on the first roll add a loss
        if (die1 + die2 == 2 || die1 + die2 == 3 || die1 + die2 == 12) {
            System.out.print("You lose! with rolls " + die1 + " and "
                    + die2 + "\n");
            firstRoll = false;
            lose = true;

        } 
//if it's neither, store the roll and end the loop
        else {
            rollPoint = die1 + die2;
            firstRoll = false;
        }
    }
//check if first roll was a win or loss, if not continue rolling
    for (; win == false && lose == false;) 
    {
        die1 = rand.nextInt(6) + 1;
        die2 = rand.nextInt(6) + 1;
//check if the new roll matches point or 7
        if (die1 + die2 == rollPoint) {
            win = true;
        } else if (die1 + die2 == 7) {
            lose = true;
        }
    }

//utilize win/loss statements within loop
    if (win == true) {
        winsCounter = winsCounter++;
        System.out.print("You win with rolls " + die1
                + " and " + die2 + " with a point roll for " + rollPoint+"\n");
        win = false;
    } else if (lose == true) {
        System.out.print(" you lose by rolling a 7 before point score\n");
        loseCounter++;
        lose = false;
    }
  }

  System.out.println("Wins: " + winsCounter + " Losses: " + loseCounter);
}