The University of Melbourne School of Computing and Information Systems COMP90041 Programming and Software Development

International Students' community

The University of Melbourne School of Computing and Information Systems COMP90041 Programming and Software Development

1 Background
This project is the rst in a series of three, with the ultimate objective of designing and implementing
(in Java) a simple variant of the game of Nim. It is a two player game, and the rules of the version used
here are as follows:
 The game begins with a number of objects (e.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 other player is, of course, the winner.
 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.
Here is an example play-through of the game, using 12 initial stones, and an upper bound of 3 stones
removed per turn.
 There are 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.
c The University of Melbourne 1
 Player 1 removes 1 stone. 0 stones remain.
 Player 2 wins. Player 1 loses.
2 Your Task
For this rst project, the focus will be on creating two players and playing for multiple games:
1. Your program will begin by displaying a welcome message.
2. 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.
3. The program will then prompt for another string (no space in the string) to be entered – this will
be the name of Player 2.
4. The program will then prompt for an integer to be entered – this will be the upper bound on the
number of stones that can be removed in a single turn.
5. The program will then prompt for another integer to be entered – this will be the initial number
of stones.
6. The program will then print the number of stones, and will also display the stones, which will be
represented by asterisks `*’.
7. The program will then prompt for another integer to be entered – this time, a number of stones
to be removed. 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.
8. The program should then show an updated display of stones.
9. The previous two steps should then be repeated until there are no stones remaining. When this
occurs, the program should display `Game Over’, and the name of the winner.
10. The program should then ask user whether the players wanted a play again. The user is prompted
to 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.
Here is an example execution:
Welcome to Nim
Please enter Player 1 s name:
Luke
Please enter Player 2 s name:
Han
Please enter upper bound of stone removal:
3
Please enter initial number of stones:
12
12 stones left: * * * * * * * * * * * *
Luke s turn – remove how many?
3
c The University of Melbourne 2
9 stones left: * * * * * * * * *
Han s turn – remove how many?
1
8 stones left: * * * * * * * *
Luke s turn – remove how many?
1
7 stones left: * * * * * * *
Han s turn – remove how many?
2
5 stones left: * * * * *
Luke s turn – remove how many?
3
2 stones left: * *
Han s turn – remove how many?
1
1 stones left: *
Luke s 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: * * * * * * * * * * * * * * *
Luke s turn – remove how many?
1
14 stones left: * * * * * * * * * * * * * *
Han s turn – remove how many?
2
12 stones left: * * * * * * * * * * * *
Luke s turn – remove how many?
3
9 stones left: * * * * * * * * *
Han s turn – remove how many?
4
5 stones left: * * * * *
Luke s turn – remove how many?
5
Game Over
c The University of Melbourne 3
Han wins!
Do you want to play again (Y/N):N
Please note that:
 You need to create a class called Nimsys with a main() method to manage the above game playing
process.
 When a player’s 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 re ected by the code written.
 There is NO blank line before the rst line, i.e., no println() before `Welcome to Nim’.
 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.
c The University of Melbourne 4
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 di erence 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 les. 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 les 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.
3 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 imple-
mentation. See LMS for a detailed marking scheme.
4 Testing Before Submission
You will nd the sample input le and the expected ouput le in the Projects page on LMS for you to
use on your own. You should use them to test your code on your own rst, 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 5.
To test your code by yourself:
1. Upload to the server your Java code les named \Nimsys.java” and \NimPlayer.java”, and the
test input data les \test0.txt”.
2. Run command: javac *.java (this command will compile all your java le in the current folder)
3. Run command: java Nimsys < test0.txt > output.txt (this command will run the Nimsys
using contents in `test0.txt” as input and write the output in output.txt)
4. Run command: more output.txt (this command will show the your program’s output)
5. Compare your result with the provided output le test0-output.txt. Fix your code if they are
di erent.
6. When you are satis ed with your project, submit and verify your project using the instructions
given in the Section 5.
NOTE: The test cases used to mark your submissions will be di erent 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.
c The University of Melbourne 5
5 Submission
Your submission should have two Java source code les. You must name them Nimsys.java and
NimPlayer.java, and store them in a directory under your home directory on the Engineering School
student server. Then, you can submit your work using the following command:
submit COMP90041 projA *.java
You should then verify your submission using the following command. This will store the veri cation
information in the le `feedback.txt’, which you can then view:
verify COMP90041 projA > feedback.txt
You should issue the above commands from within the same directory as where the le is stored (to get
there you may need to use the cd `Change Directory’ command). Note that you can submit as many
times as you like before the deadline.
How you edit, compile and run your Java program is up to you. You are free to use any editor or
development environment. However, you need to ensure that your program compiles and runs
correctly on the Engineering School student server, using build 1.8.0 of Oracle’s (as Sun Mi-
crosystems has been acquired by Oracle in 2010) Java Compiler and Runtime Environment, i.e. javac
and java programs.
Submit your program to the Engineering School student servers a couple of days before the deadline
to ensure that they work (you can still improve your program and submit it again). \I can’t get my
code to work on the student server but it worked on my Windows machine” is not an
acceptable excuse for late submissions. Email submissions will not be accepted either.
The deadline for the project is 4pm, Thursday 4 April, 2019. The allowed time is more than enough
for completing the project. There is a 20% penalty per day for late submissions. Suppose your
project gets a mark of 5 but is submitted within 1 day after the deadline, then you get
20% penalty and the mark will be 4 after penalty. There will be no mark for submissions
after 4pm 8 April.
6 Individual Work
Note well that this project is part of your nal assessment, so cheating is not acceptable. Any form of
material exchange, whether written, electronic or any other medium is considered cheating, and so is
copying from any online sources in case anyone shares it. Providing undue assistance is considered as
serious as receiving it, and in the case of similarities that indicate exchange of more than basic ideas,
formal disciplinary action will be taken for all involved parties. A sophisticated program that undertakes
deep structural analysis of Java code identifying regions of similarity will be run over all submissions in
\compare every pair” mode.

Leave a Reply

Your email address will not be published.

Registration

Forgotten Password?