Results 1 to 7 of 7
  1. #1
    zombian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Exclamation help with program!! looping issue?

    hi! i'm new here and really new to programming! i come here with an issue, of course.
    please bear with me, my syntax, etc. feel free to evaluate my current errors, since it's not necessarily done. my main issue right now, however, is how to loop it back to the user's turn once the computer is done rolling , particularly after it rolls a 1.. & i'm not particularly sure on where to place another 'if', for the 100 point goal.

    solved, thanks to muskar!
    Last edited by zombian; 11-21-2010 at 11:00 PM.

  2. #2
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by zombian View Post
    hi! i'm new here and really new to programming! i come here with an issue, of course.
    please bear with me, my syntax, etc. feel free to evaluate my current errors, since it's not necessarily done. my main issue right now, however, is how to loop it back to the user's turn once the computer is done rolling , particularly after it rolls a 1.. & i'm not particularly sure on where to place another 'if', for the 100 point goal.

    the problem:
    Design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a 1, all points accumulated for that round are forfeited and control of the dices moves to the other player. If the player tolls two 1’s in one turn, the player loses all points accumulated thus far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. Therefore the player must decide to either roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round.

    This is my shot at it:

    Java Code:
    import java.util.Scanner;
    public class PigGame{
    
        public static void main (String[] args){
            int [COLOR="Red"]userRound[/COLOR] = 0, [COLOR="Green"]userPoints[/COLOR] = 0, [COLOR="Navy"]compPoints[/COLOR] = 0, num1, num2, [COLOR="Plum"]compRound[/COLOR] = 0;
            Die die1 = new Die();
            Die die2 = new Die();
            Scanner Keyboard = new Scanner(System.in);
            boolean turnover = false;
            boolean play = false;
            System.out.println("Play Pig? (1 for Yes, 2 for No): ");
            int input = Keyboard.nextInt();
            if (input == 2){
                play = false;
                System.out.print("Good bye.");
            } else if (input == 1) {
                play = true;
            }
            while (play == true)
            {
                //PLAYER'S TURN
                while (turnover == false)
                {
                    //Rolls dice
                    num1 = die1.roll();
                    num2 = die2.roll();
                    [COLOR="Red"]userRound[/COLOR] = [COLOR="Red"]userRound[/COLOR] + num1 + num2;
                    System.out.println("You rolled: " +num1+ " & " + num2);
                    System.out.println("Points: " + [COLOR="Red"]userRound[/COLOR]);
                    System.out.println();
                    //Checks for 1's
                    if (num1 == 1 || num2 == 1) {
                        [COLOR="Red"]userRound[/COLOR] = 0;
                        if (num1 == 1 && num2 == 1) {
                            [COLOR="Green"]userPoints[/COLOR] = 0;
                        }
                        turnover = true;
                        System.out.println("Sorry! No 1's allowed.");
                        System.out.println("Points: " + [COLOR="Green"]userPoints[/COLOR]);
                        System.out.println("Computer's turn.");
                        System.out.println();
                    } [B][COLOR="Lime"]else if ([COLOR="Green"]userPoints[/COLOR] >= 100) {
                        System.out.println("You have " + [COLOR="Navy"]compPoints[/COLOR] + "points!");
                        System.out.println("You won!");
                        play = false;
                    } [/COLOR][/B]else {
                        System.out.print("Roll again? (1 for Yes, 2 for No): ");
                        input = Keyboard.nextInt();
                        if (input == 2) {
                            turnover = true;
                            [COLOR="Green"]userPoints[/COLOR] = [COLOR="Green"]userPoints[/COLOR] + [COLOR="Red"]userRound[/COLOR];
                            System.out.println("Computer's turn!");
                            System.out.println();
                        }
                    }
                }
    
                //COMPUTER'S TURN
                while (turnover == true)
                {
                    num1 = die1.roll();
                    num2 = die2.roll();
                    [COLOR="Plum"]compRound[/COLOR] = [COLOR="Plum"]compRound[/COLOR] + num1 + num2;
                    System.out.println("Computer rolled: " + num1 + " & " + num2);
                    System.out.println("Points: " + [COLOR="Plum"]compRound[/COLOR]);
                    System.out.println();
                    //Checks for 1's
                    if (num1 == 1 || num2 == 1) {
                        [COLOR="Plum"]compRound[/COLOR] = 0;
                        turnover = false;
                        if (num1 == 1 && num2 == 1) {
                            [COLOR="Navy"]compPoints[/COLOR] = 0;
                        }
                        [COLOR="Navy"]compPoints[/COLOR] = [COLOR="Navy"]compPoints[/COLOR] + [COLOR="Plum"]compRound[/COLOR];
                        System.out.println("Computer's points: " + [COLOR="Navy"]compPoints[/COLOR]);
                        System.out.println("Rolled a 1. Back to user.");
                        System.out.println();
                        turnover = false;
                    } [B][COLOR="Lime"]else if ([COLOR="Navy"]compPoints[/COLOR] >= 100) {
                        System.out.println("Computer has " + [COLOR="Navy"]compPoints[/COLOR] + "points!");
                        System.out.println("You lost!");
                        play = false;
                    }[/COLOR][/B] else if ([COLOR="Plum"]compRound[/COLOR] >= 20){
                        System.out.println("User's turn.");
                        System.out.println();
                        turnover = false;
                    }
                }
            }
        }
    }
    List of changes:
    • I've renamed a few variables (I'll edit this post in a second and use colours so they stand out - done)
    • Changed a few syntaxes so they are easier to read in my opinion
    • Changed your "while input == 1" to "while play == true" (could just be "while play" since it's a boolean but let's keep it simple) and added an 'else if' just before that, which changed play to true.
    • Added the bolded lime-coloured parts to implement winning.


    Aditionally the following examples do the same:
    Java Code:
    [B]Example 1:[/B]
    if (number > 100 || number == 100) {
        //Do whatever
    }
    
    [B]Example 2:[/B]
    if (number >= 100) {
        //Do whatever
    }
    Last edited by Muskar; 11-21-2010 at 11:00 PM. Reason: Added colours

  3. #3
    zombian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    :eek: wow! thank you very much!! definitely makes a lot more sense. my code did seem kinda confusing. thanks again.

  4. #4
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by zombian View Post
    :eek: wow! thank you very much!!
    So does that mean it works?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zombian View Post
    hi! i'm new here and really new to programming! i come here with an issue, of course.
    please bear with me, my syntax, etc. feel free to evaluate my current errors, since it's not necessarily done. my main issue right now, however, is how to loop it back to the user's turn once the computer is done rolling , particularly after it rolls a 1.. & i'm not particularly sure on where to place another 'if', for the 100 point goal.

    solved, thanks to muskar!
    Zombian, please don't delete the code in your original post. The answers in these forums are not just for you but for all, especially those in the future with similar questions. Thanks for your cooperation.

  6. #6
    zombian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    @Muskar : it does. my syntax was confusing me. thanks again.

    @Fubarable : sorry! ill make note of that in the future.

  7. #7
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Zombian, please don't delete the code in your original post. The answers in these forums are not just for you but for all, especially those in the future with similar questions. Thanks for your cooperation.

    Luckily I have the original message saved if you wish to edit it back in (you are free to remove this post once it's edited back in):


    Quote Originally Posted by zombian View Post
    hi! i'm new here and really new to programming! i come here with an issue, of course.
    please bear with me, my syntax, etc. feel free to evaluate my current errors, since it's not necessarily done. my main issue right now, however, is how to loop it back to the user's turn once the computer is done rolling , particularly after it rolls a 1.. & i'm not particularly sure on where to place another 'if', for the 100 point goal.

    the problem:
    Design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a 1, all points accumulated for that round are forfeited and control of the dices moves to the other player. If the player tolls two 1’s in one turn, the player loses all points accumulated thus far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. Therefore the player must decide to either roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round.



    what i've got so far
    Java Code:
    import java.util.Scanner;
    public class PigGame{
    
        public static void main (String[] args){
            int round = 0, userpoints = 0, compoints=0, num1, num2, cround=0;
            Die die1 = new Die();
            Die die2 = new Die();
            Scanner Keyboard = new Scanner(System.in);
            boolean turnover = false;
            boolean play = false;
            System.out.println("Play Pig? (1 for Yes, 2 for No): ");
            int input = Keyboard.nextInt();
            if (input==2){
                play = false;
                System.out.print("Good bye.");}
            while(input==1)
            {play = true;
                //PLAYER'S TURN
                while(turnover==false)
                {
                    //Rolls dice
                    num1 = die1.roll();
                    num2 = die2.roll();
                    round=round+num1+num2;
                    System.out.println("You rolled: " +num1+ " & " + num2);
                    System.out.println("Points: " + round);
                    System.out.println();
                    //Checks for 1's
                    if(num1==1||num2==1)
                    {round=0;
                        if(num1==1&&num2==1)
                            userpoints=0;
                        turnover=true;
                    System.out.println("Sorry! No 1's allowed.");
                    System.out.println("Points: " + userpoints);
                    System.out.println("Computer's turn.");
                    System.out.println();
                    }
    
                        else{
                            System.out.print("Roll again? (1 for Yes, 2 for No): ");
                            input = Keyboard.nextInt();
                            if(input == 2)
                            {
                            turnover=true;
                            userpoints=userpoints+round;
                            System.out.println("Computer's turn!");
                            System.out.println();
                        }
            }
    }
    
    //COMPUTER'S TURN
                while(turnover==true)
                {
                    num1 = die1.roll();
                    num2 = die2.roll();
                    cround=cround+num1+num2;
                    System.out.println("Computer rolled: " +num1+ " & " + num2);
                    System.out.println("Points: " + cround);
                    System.out.println();
                    //Checks for 1's
                    if(num1==1||num2==1)
                    {cround=0;
                    turnover=false;
                        if(num1==1&&num2==1)
                            compoints=0;
                        compoints=compoints+cround;
                        System.out.println("Computer's points: " + compoints);
                        System.out.println("Rolled a 1. Back to user.");
                        System.out.println();
                        turnover=false;
                    }
                    else{
                        if(cround>20 || cround==20){
                            System.out.println("User's turn.");
                            System.out.println();
                            turnover=false;
            }
    }
                }
                }
            }
        }
    Last edited by Muskar; 11-21-2010 at 11:18 PM.

Similar Threads

  1. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 03:01 PM
  2. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 11:54 PM
  3. Program Issue
    By coder09 in forum New To Java
    Replies: 5
    Last Post: 01-27-2009, 02:36 AM
  4. Replies: 1
    Last Post: 11-14-2008, 07:56 AM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •