Anguis Nox
Anguis Nox

Reputation: 21

Dice Toss Game on java

I'm coding a dice roll game on java with some set rules.

The player seeks to obtain a 7 or 11 in the combination of dice to win. If on the other hand he gets a 2, 3 or 12 loses. If during the first throw you do not get a 7 or 11 (with which you win), or a 2, 3 or 12 (with which you lose), the game will enter a second stage, in which you will mark the "point" in the number that is obtained in said launch (4, 5, 6, 8, 9 or 10). In this stage, the shooter will seek to get that number again in the dice, with which he will win before obtaining a 7. If he manages to repeat the point number, the player will win. If on the other hand if a 7 appears, you will lose.

import myClass.dice1;
import myClass.dice2;

public class game{
    public static void main(String [] args)
    {
        int t1, t2, total;

        dice1 d1 = new dice1();
        t1 = d1.tossdice();
        dice2 d2 = new dice2();
        t2 = d2.tossdice();
        d1.drawdice(t1);
        d2.drawdice(t2);
        total = t1+t2;
        if(total == 7 || total == 11)
        {
            System.out.println("Game won with "+ total);
        }else if (total == 2 || total == 3 || total == 12)
        {
            System.out.println("Game lost with "+ total);
        }else if (total == 4)
        {
            do
            {
                System.out.println("Total is "+total+" Throw again");
                t1 = d1.tossdice();
                t2 = d2.tossdice();
                d1.drawdice(t1);
                d2.drawdice(t2);
                total = t1+t2;
                break;
            }while(total !=4 || total !=7);
            if (total == 4)
            {
                System.out.println("Won game with "+ total);
            }else
            {
                System.out.println("Lost game with "+ total);
            }                   
        }
    }
}

The issue I'm having is once I begin the else if, so if I get a 4 I must get a 4 again to win, if I get a 7 I lose, an if it is another number just throw again until the 4 or 7, issue is that when I get a 4 it "throws" again but if is anything other than a 4 is a lost game, don't know if it is for the break line that I have but if I don't put break it goes on a loop forever and I have to close the cmd and start again.

Only the else if for the 4 is what I have done since once the code for the 4 is working I just have to do the same with the 5, 6, 8, 9, and 10.

This code that I have made separately

import myClass.dice1;
import myClass.dice2;

just print this:

case 1: System.out.println("   ");
        System.out.println(" o ");
        System.out.println("   ");

Depending of the value of t1 and t2 it prints between case 1 and 6.

Upvotes: 0

Views: 935

Answers (1)

Ian Mc
Ian Mc

Reputation: 5829

The following code fixes the problem. The main issues were:

  1. In the second phase of the game, you need to store the dice roll which will win the game in a variable
  2. The condition in the second phase is roll again if (roll != winTarget && roll != 7) .. you had an or here.

Here is the code:

public class game {
public static void main(String[] args) {
    int t1, t2, total;

    dice1 d1 = new dice1();
    t1 = d1.tossdice();
    dice2 d2 = new dice2();
    t2 = d2.tossdice();
    d1.drawdice(t1);
    d2.drawdice(t2);
    total = t1 + t2;
    if (total == 7 || total == 11) {
        System.out.println("Game won with " + total);
    } else if (total == 2 || total == 3 || total == 12) {
        System.out.println("Game lost with " + total);
    } else {
        int winTarget = total;
        System.out.println("Rolling until dice roll="+winTarget+" or 7");
        do {
            t1 = d1.tossdice();
            t2 = d2.tossdice();
            d1.drawdice(t1);
            d2.drawdice(t2);
            total = t1 + t2;
            System.out.println("Total is " + total + " Throw again");
        } while (total != winTarget && total != 7);
        if (total == winTarget) {
            System.out.println("Won game with " + total);
        } else {
            System.out.println("Lost game with " + total);
        }
    }
}

Upvotes: 1

Related Questions