Nim program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #31
    Nah, this program (if it ever completes) is as ugly as it is, so better print all rows like this:

    [code=java]
    System.out.prin tln("|||||".sub string(0, row1));
    System.out.prin tln("|||||".sub string(0, row2));
    System.out.prin tln("|||||".sub string(0, row3));
    System.out.prin tln("|||||".sub string(0, row4));
    System.out.prin tln("|||||".sub string(0, row5));
    [/code]

    It would save you all those identical loops.

    kind regards,

    Jos

    Comment

    • SuperDuper
      New Member
      • Oct 2007
      • 27

      #32
      Originally posted by JosAH
      Nah, this program (if it ever completes) is as ugly as it is, so better print all rows like this:

      [code=java]
      System.out.prin tln("|||||".sub string(0, row1));
      System.out.prin tln("|||||".sub string(0, row2));
      System.out.prin tln("|||||".sub string(0, row3));
      System.out.prin tln("|||||".sub string(0, row4));
      System.out.prin tln("|||||".sub string(0, row5));
      [/code]

      It would save you all those identical loops.

      kind regards,

      Jos
      Wow, that basically completed the program right there.
      The only thing is if I try to remove three(3) sticks from the second row, then it prints out an ugly section of errors.

      I'd rather it just simply ignore the choice, and remove the maximum from that row.. Ex: Removing five(5) sticks from the third row would print: " " (nothing).

      I also have to see if I'm able to use Substrings, but I should be able to.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #33
        Originally posted by SuperDuper
        I also have to see if I'm able to use Substrings, but I should be able to.
        Yes you should; you're able to use System.out.prin tln() aren't you? Then you should
        be able/allowed to use String.substrin g() for the same reason.

        kind regards,

        Jos

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #34
          Originally posted by SuperDuper
          I'd rather it just simply ignore the choice, and remove the maximum from that row.. Ex: Removing five(5) sticks from the third row would print: " " (nothing).
          You're nowhere near are you; when/if a user types something be prepared to
          anticipate to all of the following:

          1) no input at all
          2) nonsense input (like 'fronobulax' when asked for a pile number)
          3) incorrect pile number (like -42)
          4) incorrect input for the number of sticks to remove (see 1, 2 and 3 again)

          and just because you're not allowed to implement your own little methods, nor
          are you allowed to use a simple array you have to repeat your solution over and
          over again for every single number; have fun.

          kind regards,

          Jos

          Comment

          • SuperDuper
            New Member
            • Oct 2007
            • 27

            #35
            Originally posted by JosAH
            You're nowhere near are you; when/if a user types something be prepared to
            anticipate to all of the following:

            1) no input at all
            2) nonsense input (like 'fronobulax' when asked for a pile number)
            3) incorrect pile number (like -42)
            4) incorrect input for the number of sticks to remove (see 1, 2 and 3 again)

            and just because you're not allowed to implement your own little methods, nor
            are you allowed to use a simple array you have to repeat your solution over and
            over again for every single number; have fun.

            kind regards,

            Jos

            It doesn't matter what they type.. if it is irrelevant to the actual purpose of the program.. it can simply be ignored. There does *not* need to be any code written for such as the 4 exceptions you've posted.

            It just simply needs to ignore it..and continue on.

            The program would ask: Player A Which row would you like to use? Answer: 7
            How many sticks would you like to remove? Answer: 62

            Then it should simply ignore this.. and ask again
            Player A Which row would you like to use?

            until they choose a number that *is* relevant.
            So I am pretty much finished the program..aren't I?

            How do I simply make it ignore this.. something like this maybe?

            if (x > 5 || y > 5 || y>x)
            {
            ignore/continue; <--- that's not right.. so what could I do to have this effect.
            }
            Thank you so much.
            My code is :

            [PHP]import java.util.Scann er;
            public class StickGame
            {
            public static void main (String[]args)
            {
            Scanner scan = new Scanner(System. in);
            {
            int Row1 = 1;
            int Row2 = 2;
            int Row3 = 3;
            int Row4 = 4;
            int Row5 = 5;

            //Initial Board
            System.out.prin tln(" 1: | ");
            System.out.prin tln(" 2: | |");
            System.out.prin tln(" 3: | | |");
            System.out.prin tln(" 4: | | | | ");
            System.out.prin tln(" 5: | | | | |");

            int count = 0;
            String Player = ("Player A");
            String PlayerA = ("Player A");
            String PlayerB = ("Player B");

            while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)
            {

            //Players choose their move.
            System.out.prin tln();
            System.out.prin tln(Player+ " please choose a row.");
            int x = scan.nextInt();
            System.out.prin tln("How many sticks would you like to remove?");
            int y = scan.nextInt();

            int a= Row1;
            int b= Row2;
            int c= Row3;
            int d= Row4;
            int e= Row5;
            switch(x)
            {
            case 1:
            Row1 -= y;
            break;
            case 2:
            Row2 -= y;
            break;
            case 3:
            Row3 -= y;
            break;
            case 4:
            Row4 -= y;
            break;
            case 5:
            Row5 -= y;
            }

            if(y <= Row1 || y <= Row2 || y <= Row3 || y <= Row4 || y <= Row5)
            {
            System.out.prin tln("|||||".sub string(0, Row1));

            System.out.prin tln("|||||".sub string(0, Row2));

            System.out.prin tln("|||||".sub string(0, Row3));

            System.out.prin tln("|||||".sub string(0, Row4));

            System.out.prin tln("|||||".sub string(0, Row5));
            }
            else
            {
            return;}

            if(Row1 == 0 && Row2 ==0 && Row3 ==0 && Row4 == 0 && Row5 == 0)
            {
            System.out.prin t(Player+ " wins!");
            }

            if (count % 2 == 0)
            {Player = PlayerB;
            }
            else
            {Player = PlayerA;
            }
            count++;

            }

            }
            }
            }
            [/PHP]

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #36
              The correct statement you want there is the "continue" statement.

              Comment

              • SuperDuper
                New Member
                • Oct 2007
                • 27

                #37
                Originally posted by Ganon11
                The correct statement you want there is the "continue" statement.
                Thank you so much, that basically did what I wanted it to..

                For example I choose the fifth row, to remove 6 sticks.. and it prints out "please choose a correct number"..

                although since my code is
                [PHP] if (x > 5 || y > 5 || y > x)
                {
                System.out.prin tln("Please enter a correct number");
                continue;
                }[/PHP]

                once you remove say 3 sticks.. from the fifth, you're left with two remaining.. and then that if statement doesn't apply anymore.. since 2 !> (not greater) than 5.. so it prints out this:


                [PHP]StringIndexOutO fBoundsExceptio n: String index out of range: -1
                at java.lang.Strin g.substring(Unk nown Source)
                at StickGame.main( StickGame.java: 64)
                at sun.reflect.Nat iveMethodAccess orImpl.invoke0( Native Method)
                at sun.reflect.Nat iveMethodAccess orImpl.invoke(U nknown Source)
                at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(Unknown Source)
                at java.lang.refle ct.Method.invok e(Unknown Source)[/PHP]

                where the out of range: -1 is the difference from the remaining sticks and the amount you wanted to remove.. ( in this case removing 3 sticks from the remaining two).

                Urgh, I truly appreciate all this help.. I would be NOWHERE near completion without all of you.. but I can't figure this out.

                Please report any suggestions.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #38
                  When you're checking if the amount to remove is greater than the amount of sticks in the pile, are you checking against a constant, hard-coded number, or against a variable?

                  Comment

                  • SuperDuper
                    New Member
                    • Oct 2007
                    • 27

                    #39
                    I'm using "5" .. so a hardcoded number.

                    I tested out using something like this.

                    [PHP]
                    int a = Row1;
                    int b = Row2;
                    int c = Row3;
                    int d = Row4;
                    int e = Row5;


                    if (x > a,b,c,d,e || y > a,b,c,d,e || y>x)
                    {
                    System.out.prin tln("Please enter a correct number");
                    continue;
                    }[/PHP]

                    I thought this was a good idea, but it gave me errors.
                    Is this the correct process, but I'm implementing it wrong?
                    If so, what's the correct way.
                    I'm close.. I can tell

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #40
                      Originally posted by SuperDuper
                      I'm using "5" .. so a hardcoded number.

                      I tested out using something like this.

                      [PHP]
                      int a = Row1;
                      int b = Row2;
                      int c = Row3;
                      int d = Row4;
                      int e = Row5;


                      if (x > a,b,c,d,e || y > a,b,c,d,e || y>x)
                      {
                      System.out.prin tln("Please enter a correct number");
                      continue;
                      }[/PHP]

                      I thought this was a good idea, but it gave me errors.
                      Is this the correct process, but I'm implementing it wrong?
                      If so, what's the correct way.
                      I'm close.. I can tell
                      No that is not correct Java syntax. What are you trying to do with those statements.

                      P.S It helps to go through a Java tutorial.

                      Comment

                      • SuperDuper
                        New Member
                        • Oct 2007
                        • 27

                        #41
                        Originally posted by r035198x
                        No that is not correct Java syntax. What are you trying to do with those statements.

                        P.S It helps to go through a Java tutorial.

                        I'm trying to make it so.. IF somebody inputs an incorrect number (irrelevant to the program's main purpose)

                        Ex: chose the 7th row.. or chose to remove 50 sticks..

                        I just want it to ignore all these cases and continue.. that's why I set each Row equal to a different variable.. so I thought I had the right process.. just obviously in an incorrect syntax.

                        Can somebody please post the correct way to do this.. so that it will work for my program?

                        I know you guys know how to do it... please please help me.
                        I know you have all helped a lot to this point, but I'm a beginner (big time) and just would really like to get this over with.
                        Thank you so much.

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #42
                          I would check each thing one at a time. For instance, right after the user enters the row number, check if it's valid. If not, use the continue statement. If it is valid, move on to check how many sticks they want to remove. Then check if it's valid - depending on the row variable entered, your check will vary.

                          Comment

                          • SuperDuper
                            New Member
                            • Oct 2007
                            • 27

                            #43
                            Originally posted by Ganon11
                            I would check each thing one at a time. For instance, right after the user enters the row number, check if it's valid. If not, use the continue statement. If it is valid, move on to check how many sticks they want to remove. Then check if it's valid - depending on the row variable entered, your check will vary.

                            Is it because I have

                            [PHP]System.out.prin tln("|||||".sub string(0, Row1));

                            System.out.prin tln("|||||".sub string(0, Row2));

                            System.out.prin tln("|||||".sub string(0, Row3));

                            System.out.prin tln("|||||".sub string(0, Row4));


                            that I get an error whenever I try to remove less than zero (0) sticks...
                            can simply placing a few if/else statements throughout the code bypass this?

                            I've been trying tediously since my program is suppose to be due tonight.. to get past this.. but I continuously get the same error:


                            [PHP]StringIndexOutO fBoundsExceptio n: String index out of range: 2
                            at java.lang.Strin g.substring(Unk nown Source)
                            at StickGame.main( StickGame.java: 73)
                            at sun.reflect.Nat iveMethodAccess orImpl.invoke0( Native Method)
                            at sun.reflect.Nat iveMethodAccess orImpl.invoke(U nknown Source)
                            at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(Unknown Source)
                            at java.lang.refle ct.Method.invok e(Unknown Source)
                            System.out.prin tln("|||||".sub string(0, Row5));[/PHP][/PHP]

                            could you perhaps give me an example of what I'd place inside the if statement.. should I be comparing x and y to a,b,c,d,e? (where those are equal to their corresponding row)

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #44
                              What you failed to realize from Jos' instructions above is that ignoring meaningless input and continuing actually is handling an exceptional condition. Now you are again faced with a problem of handling another "exceptiona l" condition.
                              Tip: Whenever you see the words "input from user", you'll always need to handle ridiculuos input because users input ridiculous things.

                              Comment

                              • SuperDuper
                                New Member
                                • Oct 2007
                                • 27

                                #45
                                Originally posted by r035198x
                                What you failed to realize from Jos' instructions above is that ignoring meaningless input and continuing actually is handling an exceptional condition. Now you are again faced with a problem of handling another "exceptiona l" condition.
                                Tip: Whenever you see the words "input from user", you'll always need to handle ridiculuos input because users input ridiculous things.

                                What do you mean by exceptional condition?

                                How do I solve this?
                                I keep being told what the problem is.. and then left to dwell over it..

                                should I just put in a for loop? if so.. what should be in the for loop?
                                I don't need to know why.. once I see the code.. or an example then I'll be able to figure it out.

                                anybody?

                                Comment

                                Working...