简体   繁体   中英

counter issue encountered with java

I am making a dice game, the rules are 2 dice are thrown randomly, if the sum of both dice are 7 or 11 in the first try, the user wins. if the sum is 2,3 or 12 in the first try, the user loses. if the sum is 4,5,6,8,9,10 then that sum is set as an establish point and continues to throw the dice till the sum matches the established point and the user wins, however if the establish point is 7 the user loses.

the game plays 3 times and records how many times I have won or lost. however, I am not able to get that part finished, help would be appreciated, my code is bellow.

package roll;

import java.util.Random;

public class RandomSumGame {

    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuepoint;
    String plus = "+";

    public void play(int d1, int d2) {
        int win = 0;
        int loss = 0;
        sum = d1 + d2;

        for (;;) {
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                loss++;
                System.out.println(" = " + sum + ";you lose");
                break;

            } else if (sum == 7 || sum == 11) {
                start = true;
                win++;
                System.out.println(" = " + sum + ";you win ");
                break;
            }

            valuepoint = sum;

            if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
                    || valuepoint == 10) {
                System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
                break;

            }

        }
        for (;;) {
            if (sum == 7 || sum == 11) {
                break;

            } else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int f1 = d1;
                    System.out.print("\t" + "-you rolled again " + d1 + " " + plus);

                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int f2 = d2;
                        int loda = f1 + f2;
                        System.out.print(" " + d2 + " = " + loda + "\n");

                    }

                }
                sum = d1 + d2;

                if (sum == valuepoint) {
                    start = true;
                    win++;

                    System.out.print("\t" + "you win!" + "\n");
                    break;

                } else if (sum == 7) {
                    loss++;
                    start = false;
                    System.out.print("\t" + "you lose " + "\n");
                    break;

                }
            }

        }
        System.out.print("wins: " + win + "\n");
        System.out.print("Losses:" + loss);
    }

    public void play() {

        for (int x = 0; x < 3; x++) {

            rolldice();
            play(d1, d2);
            System.out.print("\n");

        }

    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + plus);

        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        RandomSumGame Teet = new RandomSumGame();
        RandomSumGame d1counter = new RandomSumGame();
        RandomSumGame d2counter = new RandomSumGame();

        Teet.play();

    }
}

What part are you not able to finish? Seems like the game is running 3 times? Is it the total number of wins and loses? To fix that, you could just move "int win" and "int lose" outside the play method to make them global variables.

EDIT: If you don't want to use global variables, you could make a method that calls itself (recursive method)


import java.util.Random;

public class RandomSumGame {
    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuepoint;
    String plus = "+";

    public void play(int d1, int d2, int win, int loss) {
        sum = d1 + d2;

        for (;;) {
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                loss++;
                System.out.println(" = " + sum + ";you lose");
                break;

            } else if (sum == 7 || sum == 11) {
                start = true;
                win++;
                System.out.println(" = " + sum + ";you win ");
                break;
            }

            valuepoint = sum;

            if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
                    || valuepoint == 10) {
                System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
                break;

            }

        }
        for (;;) {
            if (sum == 7 || sum == 11) {
                break;

            } else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int f1 = d1;
                    System.out.print("\t" + "-you rolled again " + d1 + " " + plus);

                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int f2 = d2;
                        int loda = f1 + f2;
                        System.out.print(" " + d2 + " = " + loda + "\n");

                    }

                }
                sum = d1 + d2;

                if (sum == valuepoint) {
                    start = true;
                    win++;

                    System.out.print("\t" + "you win!" + "\n");
                    break;

                } else if (sum == 7) {
                    loss++;
                    start = false;
                    System.out.print("\t" + "you lose " + "\n");
                    break;

                }
            }

        }

        System.out.print("wins: " + win + "\n");
        System.out.print("Losses:" + loss);
        if (win < 2 && loss < 2) {
            System.out.print("\n");
            //Recursive
            play(win, loss);
        }
    }

    public void play(int win, int loss) {
        rolldice();
        play(d1, d2, win, loss);
    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + plus);

        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }
    }

    public static void main(String[] args) {
        RandomSumGame test = new RandomSumGame();
        test.play(0,0);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM