Nim program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SuperDuper
    New Member
    • Oct 2007
    • 27

    Nim program.

    hello there, im trying to create some java code that spits out a board of 5 rows of sticks, each one with it's corresponding number. (Row 1 = 1 Stick)
    Like so:
    |
    | |
    | | |
    | | | |
    | | | | |

    Then it needs to ask Player A to choose a row, and how many sticks to remove from that row ONLY. Then Player B takes a turn.

    You can only remove sticks from one row at a time, unlimited amount of sticks.
    The object of the game is to be the one to remove the LAST stick.
    Then spit out. "You win!"

    My code:

    import java.util.Scann er;
    public class StickGame
    {
    public static void main (String[]args)
    {
    Scanner scan = new Scanner(System. in);
    {
    String a = " | ";
    String Row1 = (a);
    String Row2 = (a+ " "+a+ " ");
    String Row3 = (a+ " "+a+ " "+a+ " ");
    String Row4 = (a+ " "+a+ " "+a+ " "+a+ " ");
    String Row5 = (a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

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

    System.out.prin tln();
    System.out.prin tln("Player A, Please choose a row.");
    int x = scan.nextInt();
    System.out.prin tln("How many sticks would you like to remove?");
    int y = scan.nextInt();

    }
    }
    }


    I don't know how to remove "n" number of sticks from Row "n", and then continue back and forth between players until its finished.

    thankz alot for any responses... im getting a headache just thinking about it

    PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
    (Hes my teacher.. im homeschooled) so he wont help me :[
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by SuperDuper
    hello there, im trying to create some java code that spits out a board of 5 rows of sticks, each one with it's corresponding number. (Row 1 = 1 Stick)
    Like so:
    |
    | |
    | | |
    | | | |
    | | | | |

    Then it needs to ask Player A to choose a row, and how many sticks to remove from that row ONLY. Then Player B takes a turn.

    You can only remove sticks from one row at a time, unlimited amount of sticks.
    The object of the game is to be the one to remove the LAST stick.
    Then spit out. "You win!"

    My code:

    import java.util.Scann er;
    public class StickGame
    {
    public static void main (String[]args)
    {
    Scanner scan = new Scanner(System. in);
    {
    String a = " | ";
    String Row1 = (a);
    String Row2 = (a+ " "+a+ " ");
    String Row3 = (a+ " "+a+ " "+a+ " ");
    String Row4 = (a+ " "+a+ " "+a+ " "+a+ " ");
    String Row5 = (a+ " "+a+ " "+a+ " "+a+ " "+a+ " ");

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

    System.out.prin tln();
    System.out.prin tln("Player A, Please choose a row.");
    int x = scan.nextInt();
    System.out.prin tln("How many sticks would you like to remove?");
    int y = scan.nextInt();

    }
    }
    }


    I don't know how to remove "n" number of sticks from Row "n", and then continue back and forth between players until its finished.

    thankz alot for any responses... im getting a headache just thinking about it

    PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
    (Hes my teacher.. im homeschooled) so he wont help me :[
    1.) Please use code tags (not bold tags) for posting code.
    2.) Where is your loop? You need some actions to be repeated.

    Comment

    • SuperDuper
      New Member
      • Oct 2007
      • 27

      #3
      I don't know what to do next..
      that's why I posted here.

      I was hoping somebody could either post some code, or describe in somewhat detail of the next steps.

      Thank you.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Instead of using strings to represent the rows, I'd use 5 int variables. row1 starts at 1, row2 starts at 2, etc. etc. When a player wishes to remove n sticks from one row, subtract n from the specific row (Make sure the player doesn't try to remove more sticks than are available in that row!)

        As for players, hmm...There are only two possibilities: either it's player 1's turn, or it's player 2's turn. Or, in other words, it's either Player 1;s turn, or it's NOT player 1's turn...Sound familiar?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by SuperDuper
          I don't know what to do next..
          that's why I posted here.

          I was hoping somebody could either post some code, or describe in somewhat detail of the next steps.

          Thank you.
          It's unlikely that someone will post some code.
          I think you should first decide on how you are going to represent your board.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            @OP: forget about that 'Nim' (that's its name) game for a moment; do you know
            how to print 'n' equal characters in a row? Hin: a for loop can do the job. You
            already discovered about System.out.prin tln; there's also a System.out.prin t
            method; go study.

            In other words: I give you 'n', a number and 'c', a character; you have to print 'n'
            of those 'c's in a row; use that loop and please use a separate little method to
            accomplish that little task.

            kind regards,

            Jos

            Comment

            • SuperDuper
              New Member
              • Oct 2007
              • 27

              #7
              Wow, these are some good ideas, haha

              Okay I'm setting int Row1=1, Row2=2, etc...
              and if for example they choose row 5.. and choose to remove 3 sticks.

              I don't see how I'm going to print out the actual two sticks " | | "
              that will remain in that fifth row.

              I know I'm a beginner, so please bare with me.

              Comment

              • SuperDuper
                New Member
                • Oct 2007
                • 27

                #8
                PS - I just verified I'm not able to use another method.
                Only the main method.

                As you can see it's quite basic, and yet I'm still having trouble.
                Any help at all is greatly appreciated, every bit counts.

                Comment

                • SuperDuper
                  New Member
                  • Oct 2007
                  • 27

                  #9
                  I've taken your advice and incorporated a println.. etc
                  though I can't figure out how to stop my while loop once it's printed out the required sticks.. and how to print out the board after EACH turn, incorporating all
                  past moves.


                  [PHP]import java.util.Scann er;
                  public class StickGameTest
                  {
                  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: | | | | |");




                  //Players choose their move.
                  System.out.prin tln();
                  System.out.prin tln("Player A, 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 difference = x-y;
                  int R = difference;
                  while(differenc e==R)
                  {
                  for (int P =0; P<=R; P++)
                  {System.out.pri nt("|" + " ");



                  }
                  }

                  }
                  }
                  }

                  [/PHP]

                  Comment

                  • kreagan
                    New Member
                    • Aug 2007
                    • 153

                    #10
                    Originally posted by SuperDuper
                    I've taken your advice and incorporated a println.. etc
                    though I can't figure out how to stop my while loop once it's printed out the required sticks.. and how to print out the board after EACH turn, incorporating all
                    past moves.


                    [PHP]import java.util.Scann er;
                    public class StickGameTest
                    {
                    public static void main (String[]args)
                    {
                    int difference = x-y;
                    int R = difference;
                    while(differenc e==R)
                    {
                    for (int P =0; P<=R; P++)
                    {System.out.pri nt("|" + " ");
                    [/PHP]
                    Difference is equal to R. R never changes nor does Difference. Therefore, the while loop will never break.

                    I would suggest using 2 nested for loops. 1 loop to iterate down and the other to iterate across.

                    To print out the moves each time, just put a loop that terminates when someone wins (there are no sticks).

                    And what do you mean incorporate all past moves? Are you saying print out all moves taken each time? Store all moves and print them out the same way you did with each single move. I would suggest just printing a statement instead of the sticks for previous moves because the images might get overwhelming.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by SuperDuper
                      PS -- urggh, my uncle just told me I cant use array?? I dont know what that means.. but I have to find a different method..
                      (Hes my teacher.. im homeschooled) so he wont help me :[
                      Would you be so kind to ask him *why* you're not allowed to use arrays? Maybe
                      he can give you a short list of *what* you are allowed to use?

                      kind regards,

                      Jos

                      Comment

                      • SuperDuper
                        New Member
                        • Oct 2007
                        • 27

                        #12
                        He said I can't use arrays since we haven't covered them yet..

                        basically I can use, for, while, if/else..
                        println, print... Strings, ints, doubles, etc.. the basics.

                        Kreagen, you said "I would suggest using 2 nested for loops. 1 loop to iterate down and the other to iterate across. "

                        What would I put inside the for loops.. because x = the row they want.
                        y = the # of sticks to remove..

                        would it be something like:
                        [PHP]for (x=1; x<=5, x++)
                        { }[/PHP]

                        and then same for y? I'm so confused...
                        Basically I want it to print the CURRENT board before each player takes a turn.
                        So if I remove 3 sticks from row 5.... row 5 should now have 2 sticks.
                        But I don't know how I'd print Row 1, 2,3, 4 unchanged.. and then 5 being changed.. and so on until somebody wins.

                        Are you not allowed to post the actual code to help me?
                        it's only my uncle and I only see him on Mondays, so he won't help.. nor care how I finish it. I do know that once I see the correct changes, I'll understand what I should have done.

                        Thanks everybody.. I appreciate you sharing your knowledge to this point,
                        if you're able.. please continue to help.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by SuperDuper
                          He said I can't use arrays since we haven't covered them yet..
                          Ask your uncle to come up with another assignment; he doesn't want you to talk
                          crippled and a much obfuscated subset of Java does he?

                          kimd regards,

                          Jos

                          Comment

                          • SuperDuper
                            New Member
                            • Oct 2007
                            • 27

                            #14
                            He said it's completely possible to do without using an array.

                            And it seems possible as you all have continued to support me this far..
                            I can't get a new assignment, especially seeing how I am almost finished this one.

                            I need to finish this one, if you don't want to help me, you don't have to.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by SuperDuper
                              He said it's completely possible to do without using an array.

                              And it seems possible as you all have continued to support me this far..
                              I can't get a new assignment, especially seeing how I am almost finished this one.

                              I need to finish this one, if you don't want to help me, you don't have to.
                              Wanting to help you or not is not the issue. Of course the entire program can
                              be implemented without arrays because arrays are just a figment of our own
                              imagination. If you want you can implemented the entire thing using just String
                              fiddling on one single String but that's not the issue either.

                              Your example problem is an ideal example of indexing values and then some.
                              Forbidding to use that indexing technique (read: arrays or Lists) forbids elegant
                              implementations for this exercise and cripples your programming style, the way
                              of thinking about problems; a programming style which you haven't developed
                              yet, so don't let it be crippled in the near future.

                              kind regards,

                              Jos

                              Comment

                              Working...