All exercises should be completed. Some will be submitted (see below). We will try to give feedback on those that are submitted. Only a subset of those you submit will be graded and count towards the CA mark.
Submissions have strict due dates. Late work will not be accepted unless under extreme and verifiable circumstances.
CS2514 Lab 1
Exercise A
In a file called Nim.java
, write a main
method to play the following
version of the game of Nim:
- The user will play against the computer. A random number decides who takes first turn.
- A random number between 10 and 100 inclusive represents the number of matchsticks on the table.
- The players take turns to pick up a number of matchsticks:
- When it is the user's turn, the programs asks the user how many matchsticks s/he wishes to pick up.
- When it is the computer's turn, a random number determines how many matchsticks it will pick up.
- The player who picks up the last remaining matchstick loses.
Include enough print
/println
statements so that the user knows
what's going on.
Exercise B
Take a copy of the following file (right-click on the link and choose 'Save link as…'):
Based on NameTester.java
, write a class definition for Name
.
Submission
Deadline: 4pm, Friday 29th January 2016.
Submit your solution to Exercise A only. Open a console; use the cd
command to
move to where your solution is and type:
submit-2514 Nim.java
Challenge exercise
Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further. You may need to use things not covered so far in lectures.
When you are convinced that your Nim program is superb (correct, efficient, elegant), make a copy
of it in NimChallenge.java
. Modify it as follows so that the computer now plays
'smart'.
The computer is still subject to the rule that it must pick up at least one and at most half of the matchsticks but now...
- If the number of matchstics is equal to a power of 2 less 1, then the computer picks up a random number of matchsticks as before.
- Otherwise, the computer picks up a number of matchsticks that leaves a power of 2 less 1 on the table.
Call me over to look over your challenge solution.