1 Nim: A Game of Strategy
This project is the first in a series of three, with the ultimate objective of designing and implementing a simple variant of the game of Nim in Java. Nim is a two-player game, and the rules of the version used here are as follows:
- The game begins with a number of objects (g., stones placed on a table).
- Each player takes turns removing stones from the table.
- On each turn, a player must remove at least one stone. In addition, there is an upper bound on the number of stones that can be removed in a single turn. For example, if this upper bound is 3, a player has the choice of removing 1, 2 or 3 stones on each turn.
- The game ends when there are no more stones remaining. The player who removes the last stone, loses. The remaining player wins.
- Both the initial number of stones, and the upper bound on the number that can be removed, can be varied from game to game, and must be chosen before a game commences.
1.1 Example Gameplay
Here is an example play-through of the game, using 12 initial stones, and an upper bound of 3 stones removed per turn:
- 12 stones on the table.
- Player 1 removes 3 stones. 9 stones remain.
- Player 2 removes 1 stone. 8 stones remain.
- Player 1 removes 1 stone. 7 stones remain.
- Player 2 removes 2 stones. 5 stones remain.
- Player 1 removes 3 stones. 2 stones remain.
- Player 2 removes 1 stone. 1 stone remains.
- Player 1 removes 1 stone. 0 stones remain.
- Player 2 wins.
2 Assignment: Implement Nims Basic Game Mechanics
For this first project, the focus will be on creating two players and playing for multiple games:
- Your program will begin by displaying a welcome message.
- The program will then prompt for a string (no space in the string) to be entered via standard input(the keyboard) this will be the name of Player 1. You may assume that all inputs to the program will be valid.
- The program will then prompt for another string (no space in the string) to be entered this will bethe name of Player 2.
- The program will then prompt for an integer to be entered this will be the upper bound on thenumber of stones that can be removed in a single turn.
- The program will then prompt for another integer to be entered this will be the initial number ofstones.
- The program will then print the number of stones, and will also display the stones, which will berepresented by asterisks *.
- The program will then prompt for another integer to be entered this time, a number of stones to beremoved. Again, you may assume this input will be valid and will not exceed the number of stones remaining or the upper bound on the number of stones that can be removed.
- The program should then show an updated display of stones.
- The previous two steps should then be repeated until there are no stones remaining. When thisoccurs, the program should display Game Over, and the name of the winner.
- The program should then ask user whether the players wanted a play again. The user is promptedto enter Y for yes or N for no. If the user enters Y, that is, the user wants to play another game, repeat steps 4-10. Otherwise, the program should terminate. Note, any input apart from Y should terminate the game.
2.1 Example Execution
After executing your program, the flow should be something like this:
Welcome to Nim
Please enter Player 1s name:
Luke
Please enter Player 2s name: Han
Please enter upper bound of stone removal:
3
Please enter initial number of stones:
12
12 stones left: * * * * * * * * * * * *
Lukes turn remove how many? 3
9 stones left: * * * * * * * * *
Hans turn remove how many? 1
8 stones left: * * * * * * * *
Lukes turn remove how many? 1
7 stones left: * * * * * * *
Hans turn remove how many? 2
5 stones left: * * * * *
Lukes turn remove how many? 3
2 stones left: * *
Hans turn remove how many? 1
1 stones left: *
Lukes turn remove how many? 1
Game Over Han wins!
Do you want to play again (Y/N):
Y
Please enter upper bound of stone removal: 5
Please enter initial number of stones: 15
15 stones left: * * * * * * * * * * * * * * *
Lukes turn remove how many? 1
14 stones left: * * * * * * * * * * * * * *
Hans turn remove how many?
2
12 stones left: * * * * * * * * * * * *
Lukes turn remove how many? 3
9 stones left: * * * * * * * * *
Hans turn remove how many? 4
5 stones left: * * * * *
Lukes turn remove how many? 5
Game Over Han wins!
Do you want to play again (Y/N):
N
2.2 Some Hints
Please note that:
- You need to create a class called Nimsys with a main() method to manage the above game playing process.
- When a players name is entered, a new object of the NimPlayer class should be created. You need to create the class NimPlayer. Player 1 and Player 2 are two instances of this class. This class should have a String typed instance variable representing the player name. This class should also have a removeStone() method that returns the number of stones the player wants to remove in his/her turn. You will lose marks if you fail to create two instances of NimPlayer.
- Add other variables and methods where appropriate such that the concept of information hiding and encapsulation is reflected by the code written.
- There is NO blank line before the first line, i.e., no println() before Welcome to Nim.
- There is a withespace between stones left : sentence and * * *, and also between all * * *, but NO withespace at the end of * * *.
- The line that prints the winners name should be a full line, i.e., Han wins! is printed out using println().
- And there are NO blanks after the last stone in lines displaying asterisks.
- Keep a good coding style.
You do not need to worry about changing your output for singular/plural entities, i.e., you should output 1 stones, etc.
3 Important Notes
Computer automatic test will be conducted on your program by automatically compiling, running, and comparing your outputs for several test cases with generated expected outputs. The automatic test will deem your output wrong if your output does not match the expected output, even if the difference is just having an extra space or missing a colon. Therefore it is crucial that your output follows exactly the same format shown in the examples above.
The syntax import is available for you to use standard java packages. However, please DO NOT use the package syntax to customize your source files. The automatic test system cannot deal with customized packages. If you are using Netbeans as the IDE, please be aware that the project name may automatically be used as the package name. Please remove the line like package ProjA; at the beginning of the source files before you submit them to the system.
Please use ONLY ONE Scanner object throughout your program. Otherwise the automatic test will cause your program to generate exceptions and terminate. The reason is that in the automatic test, multiple lines of test inputs are sent all together to the program. As the program receives the inputs, it will pass them all to the currently active Scanner object, leaving the rest Scanner objects nothing to read and hence cause run-time exception. Therefore it is crucial that your program has only one Scanner object. Arguments such as It runs correctly when I do manual test, but fails under automatic test will not be accepted.
4 Assessment
This project is worth 10% of the total marks for the subject.
Your Java program will be assessed based on correctness of the output as well as quality of code implementation. See Canvas for a detailed marking scheme.
5 Test Before Submission
You will find the sample input file and the expected ouput file in the Projects page on LMS for you to use on your own. You should use them to test your code on your own first, and then submit your code. Note that you must submit your code through the submit system in order to make a valid submission of the project, details of submit system can be found in Section 6. To test your code by yourself:
- Upload to the server your Java code files named java and NimPlayer.java, and the test input data files test0.txt.
- Run command: javac *.java (this command will compile all your java file in the current folder)
- Run command: java Nimsys < test0.txt > output.txt (this command will run the Nimsys using contents in txt as input and write the output in output.txt)
- Run command: more output.txt (this command will show the your programs output)
- Compare your result with the provided output file test0-output.txt. Fix your code if they are different.
- When you are satisfied with your project, submit and verify your project using the instructions givenin the Section 6.
NOTE: The test cases used to mark your submissions will be different from the sample tests given. You should test your program extensively to ensure it is correct for other input values with the same format as the sample tests.
Reviews
There are no reviews yet.