CST8107 – Introduction to Programming and Problem Solving Assignment #2
DUE: Dec 3rd at 11:59 PM – see submission instructions below.
Problem Description:
Using the steps for Problem Solving – generate for the following problem:
test plan AND
write and test the program code in Java.
This problem will simulate a game of misère Nim(a mathematical game of strategy in which two players take turns removing objects from distinct piles (also called heaps). On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same pile. The goal of the game is to force your opponent to remove the last object.
This version will have three piles, each starting with 10 objects.
In each play of the game,
the player will select a pile (i.e. a, b, or c) and the amount that they want to remove
The game will display the current numbers
The game will then pick a pile and a number from that pile (simulate this using the Random class in Java – in non-bonus version)
The game will display the current numbers
The game will continue until the player wins or loses
The game must:
Not let a player select a pile that is not a, A, b, B, c, or C
Not let a player select a number to remove that is greater than the number in that pile or less than one
Not let a player remove from an empty pile
Randomly(non-bonus)select a non-empty pile
Randomly(non-bonus)select a number from that pile that is in the range of 1 to the pile size
Detect if all three piles are empty and declare if the player is the winner or loser correctly
The java files must be:
Assign2.java – the program main (only calls Nim class)
Nim.java – class for the game (containing three Pile classes)
Pile.java – class to handle ONE pile
Bonus opportunity (see marking guide):
Randomly assign 10-20 initial objects to each pile,
implement the nim-sum winning strategy based on the sample output 2
Sample Output 1 – non-bonus: (green indicates user entered information)
Welcome to the NIM game
We play by the misère rules
A B C
10 10 10
Select a pile: a
How many do you want to remove? 5
A B C
5 10 10
Computer takes 10 from pile C
A B C
5 10 0
Select a pile: C
Pile C is empty, pick another
A B C
5 10 0
Select a pile: b
How many do you want to remove? 11
Pick a number between 1 and 10
A B C
5 10 0
Select a pile: b
How many do you want to remove? 5
A B C
5 5 0
Computer takes 5 from pile B
A B C
5 0 0
Select a pile: a
How many do you want to remove? 4
A B C
1 0 0
Computer takes 1 from pile A
Congrats: you win
Sample Output 2: – bonus
Welcome to the NIM game
We play by the misère rules
A B C
16 19 12
Select a pile: H
Invalid pile letter, select a, b or c
A B C
16 19 12
Select a pile: A
How many do you want to remove? 17
Pick a number between 1 and 16
A B C
16 19 12
Select a pile: a
How many do you want to remove? -1
Pick a number between 1 and 16
A B C
16 19 12
Select a pile: a
How many do you want to remove? 0
Pick a number between 1 and 16
A B C
16 19 12
Select a pile: a
How many do you want to remove? 11
A B C
5 19 12
Computer takes 10 from pile B
A B C
5 9 12
Select a pile: a
How many do you want to remove? 1
A B C
4 9 12
Computer takes 1 from pile B
A B C
4 8 12
Select a pile: a
How many do you want to remove? 1
A B C
3 8 12
Computer takes 1 from pile C
A B C
3 8 11
Select a pile: c
How many do you want to remove? 2
A B C
3 8 9
Computer takes 2 from pile A
A B C
1 8 9
Select a pile: c
How many do you want to remove? 1
A B C
1 8 8
Computer takes 1 from pile A
A B C
0 8 8
Select a pile: b
How many do you want to remove? 7
A B C
0 1 8
Computer takes 8 from pile C
A B C
0 1 0
Select a pile: a
Pile a is empty, pick another
A B C
0 1 0
Select a pile: c
Pile c is empty, pick another
A B C
0 1 0
Select a pile: b
How many do you want to remove? 1
Sorry: you lose
Submission Requirements:
You must create a .zip file that contains ONLY the following:
Your program code – 3 java files – (with your name, section, lab teacher listed in comments in the header of each class)
A document created with either Notepad, Wordpad or Word named Assign2.docx or Assign2.txt with your test plan – note this should contain your name, section and lab teacher listed at the top.
The .zip file must have the following as its name
Your last name, your first name, the word assign and the assign number ….
Example ChowdhuryRejaulAssign2.zip
Submit the .zip file through the Assignment feature which has been enabled in the CST8107 BrightSpace course. This should be directly under the Assignment description.
Marks will be given for correct submission (i.e., marks will be deducted for incorrect submission!
Notes on Using the Random class in Java
The Random class is found in library java.util.
ie … we need to import java.util.Random into a java program to be able to use it
You need to declare an object of the class first:
ie … declare once in your program, at the beginning:
Random randomNumbers = new Random ();
Then, you can call a method called nextInt on your object of Random class with an integer parameter which will return a random number between 0 and the integer parameter – 1. In other words, the nextInt method returns a random number % the parameter you sent to it. In our case, for the:
Pile selection, we want to generate a random number between 0 and 2, so we can execute nextInt(3)
Remove selection, we want to generate a random number between 1 and the size of that pile, so we can execute nextInt(pile.getSize())+1. This gives us a number between 1 and the pile size.
The post CST8107 – Introduction to Programming and Problem Solving Assignment #2 DUE: Dec appeared first on PapersSpot.