Okay, we are on the third assignment in Java, Java, Java.
I do not understand the book that well and while I have been searching google for something explained in simpler terms it hasn't came up.

We have two files: OneRowNim2.java and Assign3.java

The specifications were:
You are to create 2 java files: OneRowNim2.java and Assign3.java. The OneRowNim2.java file is based on the code in the book that is in page 131 (Figure 3-16) of Chapter 3. Remember that the code in the book is for a class named OneRowNim and you need to change the class name to OneRowNim2. (You need also to change the 3 constructors to follow the name of the class so the code would not cause any errors.). Also we need to allow up to a maximum of "4" sticks to be taken not just the mximum of 3 sticks like the code does in page 131. Make sure you change that accordingly in the code. Once the changes are done the code is ready to be compiled.
Create a separate java program which you will name Assign3.java. Write a main method that will create a game which you will name game1 and which starts out with "9" sticks. Remember to use the code of page 133 (figure 3-17). We need to modify the code so that we will read the numbers "582423264" one digit at a time. We also need to declare an int "Counter" before the while loop that will count how many numbers from the above digits were smaller or equal to 4. Then you will primt the value of the Counter after the loop ends.
Do not forget to drop both the OneRowNim2.java and the Assign3.java in the dropbox. Click on the next link to do so: DropBox for Assignment3
Do not forget to compile and run to verify that the class Assign3 is running.
PS. Output of Execution :

Number of Sticks left: 9
Next turn by player 1
input the digits '582423264' one digit at a time and hit return 5
Number of Sticks left: 9
Next turn by player 1
input the digits '582423264' one digit at a time and hit return 8
Number of Sticks left: 9
Next turn by player 1
input the digits '582423264' one digit at a time and hit return 2
Number of Sticks left: 7
Next turn by player 2
input the digits '582423264' one digit at a time and hit return 4
Number of Sticks left: 3
Next turn by player 1
input the digits '582423264' one digit at a time and hit return 2
Number of Sticks left: 1
Next turn by player 2
input the digits '582423264' one digit at a time and hit return 3
Number of Sticks left: -2
Next turn by player 1
Counter = 4
Game won by by player 1 

Now I have OneRowNim2.java as:

/*
* File: OneRowNim2.java
* Description: One Row Nim2
*/

public class OneRowNim2
{ private int nSticks = 9;
   private int player = 1;

   public OneRowNim2()
   {
   }

   public OneRowNim2(int sticks)
   {  nSticks = sticks;
   }

   public OneRowNim2(int sticks, int starter)
   {  nSticks = sticks;
      player = starter;
   }

   public boolean takeSticks(int num)
   {  if (num < 1) return false;
      else if ( num > 4) return false;
      else
      {  nSticks = nSticks - num;
         player = 4 - player;
         return true;
      }
    }

    public int getSticks()
    {  return nSticks;
    }

    public int getPlayer()
    {  return player;
    }

    public boolean gameOver()
    {  return (nSticks <= 0);
    }

    public int getWinner()
    {  if (nSticks < 1) return getPlayer();
       else return 0;
    }

    public void report()
    {  System.out.println("Number of sticks left:" + getSticks());
       System.out.println("Next turn by player" + getPlayer());
    }
}

And Assign3.java as:

/*
* File: Assign2.java
* Author: Leah Stevens
* Description: Assignment3
*/

import java.util.Scanner;

public class Assign3 
{
   public static void main (String [ ] args)
   { Scanner sc = Scanner.create(System.in);
     OneRowNim game1 = new OneRowNim(9);
     while(game1.gameOver() == false)
     {  game1.report();
        System.out.print('Input 1, 2, 3, 0r 4: ")
        int sticks = sc.nextInt();
        game1.takeSticks(sticks);
        System.out.println();
     }
    game1.report();
    System.out.print("Game won by player ");
    System.out.println(game1.getwinner());
   }
}

I am having problems with the Assign3.java
I believe that OneRowNim.java is correct but if someone wants to check it I dont mind ^.^

Im not asking for anyone to do this for me, but if anyone has any sites that could explain how to go about it I would appreciate it.
The main thing I dont understand, is in what order would I go about placing the counter, loop and such. The book is very unclear on what exactly the code does, and the instructor says first we learn how to program then we understand what it means.
It is hard for me to learn that way, so if anyone can direct me to something that would explain what the different codes affect it would be much appreciated.

Recommended Answers

All 2 Replies

I believe that OneRowNim.java is correct

Why do you believe that, when you have created a new class named OneRowNim2 and in your main you call the old one?

Typo, everything with OneRowNim is suppose to have a 2 after it. The other document I forgot to add it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.