Nim program.

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

    #16
    Alright, I appreciate your clarification,

    basically I don't expect to take any other courses in Programming, so I'm just wanting to finish this up.. It doesn't matter if it's the most efficient method at all, just needs to work.

    Here's my current code, can somebody post the remaining pieces that I need, either the code.. or a description of what I need to do.. I'm getting quite desperate, i'll admit.. so I really appreciate it.

    [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();

    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;
    break;
    }

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

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #17
      I thought nim ended when all of the sticks were taken?
      while (Row1 != 0 && Row2 !=0 && Row3 !=0 && Row4 != 0 && Row5 != 0)

      This should probably be an OR.
      while (Row1 != 0 || Row2 !=0 || Row3 !=0 || Row4 != 0 || Row5 != 0)

      Are you allowed other functions?
      I would probably create a display row function: eg

      [code=java]private static void printRow(int row, int matches){
      System.out.prin t(row+":");
      for (int i = 0; i < matches; i++){
      System.out.prin t(" |");
      }
      System.out.prin tln("");
      }[/code]

      Comment

      • SuperDuper
        New Member
        • Oct 2007
        • 27

        #18
        Thanks a lot for your suggestion,
        would that be considered a second method though?

        The requirements state I can only use a single method (main).. no arrays..

        no only the basics.. int, double, String, for, while, if/else, println, print, etc.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #19
          The OP has already stated no other functions are allowed.

          In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            Originally posted by Ganon11
            The OP has already stated no other functions are allowed.

            In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.
            I'd go for a single initial String "|@||@|||@||||@ ||||@", forget about those five single
            int variables and fiddle diddle with the String operators and regexps quite a bit.

            kind regards,

            Jos

            ps, the '@' can be any non-'|' character.

            Comment

            • SuperDuper
              New Member
              • Oct 2007
              • 27

              #21
              what do you mean by "a single initial String "|@||@|||@||||@ ||||@"

              I'm pretty much a novice in the field of programming..
              I felt as if I was nearing completion of this program, but I don't know which suggestion to follow through with.

              Right now I have my Rows as follows:
              [PHP] int Row1 = 1;
              int Row2 = 2;
              int Row3 = 3;
              int Row4 = 4;
              int Row5 = 5;[/PHP]

              and I thought I could simply subtract the number of sticks from the associated row.. and then print out the result.

              The result has to be printed after EACH turn.. and by result I mean the complete board.
              |
              ||
              |||
              ||||
              ||||| except with the chosen sticks missing each time this is printed after each
              turn. Am I simply overlooking an easy approach?
              Is anybody willing to finish my code.. I think I'm pretty close to completion, so you'd only be applying your suggestions in the form of code, as opposed to words.

              If nobody is willing, then I'll have to keep trying each suggestion 1 by 1. It just seems everyone has a different view on this.. and I don't know what to do.
              Thanks to everyone who has stuck around this far, it means a lot to me and has relieved some much unneeded stress..

              Comment

              • SuperDuper
                New Member
                • Oct 2007
                • 27

                #22
                PS - Why would I want
                while (Row1 != 0 && Row2 !=0 && Row3 !=0 && Row4 != 0 && Row5 != 0)

                to be "|| " (Or) instead?

                wouldn't that only run the loop until ONE row equals zero?
                Even though I want ALL rows to equal zero before the loop stops, and then print "You Win!"

                Comment

                • SuperDuper
                  New Member
                  • Oct 2007
                  • 27

                  #23
                  Okay I've made progress..

                  I'm now able for PlayerA and PlayerB to choose any row, and any amount of sticks to remove.

                  It prints out the board after each turn, except in the form of numbers..
                  how do I make it so it prints out the sticks?

                  That may be the end of it, if I can finish that. :)

                  [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();

                  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;
                  }

                  System.out.prin tln(Row1);
                  System.out.prin tln(Row2);
                  System.out.prin tln(Row3);
                  System.out.prin tln(Row4);
                  System.out.prin tln(Row5);

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

                  [/PHP]

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #24
                    Originally posted by Ganon11
                    In order to print out the rows, you will need 1 for loop for each row variable. For example, print 1 "|" for every time the loop executes in Row1, one "|" for every time the loop executes in Row2, etc. This is going to get messy because you will have 5 for...loops at the beginning of your while...loop.
                    Like I suggested here :D

                    Comment

                    • SuperDuper
                      New Member
                      • Oct 2007
                      • 27

                      #25
                      Originally posted by Ganon11
                      Like I suggested here :D
                      I changed it to be

                      .....
                      [PHP]int difference = x-y;
                      switch (x)
                      {
                      case 5:
                      Row5 -= y;
                      for (Row5=1; Row5 <= difference; Row5++)
                      {
                      System.out.prin t(" | " + " ");

                      }
                      [/PHP]
                      This prints out the right amount of sticks the first time.. for example.
                      x=5 (Row5) and y=3 (remove 3 sticks)

                      so it prints out " | | "

                      but then for Player B's turn.. if they choose Row5.. and to remove only one(1) stick.. it will print out "| | | |" four (4) sticks... not the one stick which I want it to.

                      I've only tried this for loop on one variable so far (Row5) just to see if it works, then i'll implement it for the other 4 rows.. so what am I doing wrong?
                      how can I make it "save" the amount of sticks currently in a row, as opposed to resetting it.

                      Thanks a lot.

                      My entire 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 difference = x-y;
                      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;
                      for (Row5=1; Row5 <= difference; Row5++)
                      {
                      System.out.prin t(" | " + " ");

                      }
                      }

                      //System.out.prin tln(Row1);
                      //System.out.prin tln(Row2);
                      //System.out.prin tln(Row3);
                      //System.out.prin tln(Row4);
                      // System.out.prin tln(Row5);

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

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #26
                        No, no, no, your for...loop should not change the Row5 variable! Use a temporary variable called i only inside the for loop, and go from 1 to <= Row5.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #27
                          This is all so bad; even old Fortran/4 or 77 knows about subroutines and arrays.
                          It shouldn't be implemented this way ... and I'm not even talking fancy OO things here ...

                          kind regards,

                          Jos

                          Comment

                          • SuperDuper
                            New Member
                            • Oct 2007
                            • 27

                            #28
                            Originally posted by JosAH
                            This is all so bad; even old Fortran/4 or 77 knows about subroutines and arrays.
                            It shouldn't be implemented this way ... and I'm not even talking fancy OO things here ...

                            kind regards,

                            Jos
                            My main objective is to solve the program, I do not care which way it is implemented as long as it completes the required task.

                            Efficiency is not an issue, so more beneficial methods are irrelevant. Reading the previous posts would support this.

                            Thank you to everyone who posted solutions, i'll try them out and report back.

                            Comment

                            • SuperDuper
                              New Member
                              • Oct 2007
                              • 27

                              #29
                              Originally posted by Ganon11
                              No, no, no, your for...loop should not change the Row5 variable! Use a temporary variable called i only inside the for loop, and go from 1 to <= Row5.

                              Okay, that makes sense.. I've applied the necessary changes and it seems to do what I want.. although when it prints out the line.. since each one is a print(blah blah)

                              as opposed to a println( blah blah) .. then the WHOLE board (all rows) are printed onto the same row... but if I switch it to be println(blah blah) then each time it runs through each individual for loop it puts a new stick on a new line..

                              how do I solve this??
                              Thanks a lot.

                              Comment

                              • Ganon11
                                Recognized Expert Specialist
                                • Oct 2006
                                • 3651

                                #30
                                After the row ends, use another output statement to either output a newline character '\n' (if you use print()), or simply put a blank println() statement.

                                Comment

                                Working...