Question:
Write a detailed algorithm and a program that plays the ancient Chinese game of NIM. Actually, this is a simplified version of the game. In this game, the person who runs your program will be one of the two human players, your program will be a simple Artificial Intelligence (AI) that will serve as the other player, as well as provide the narrative for the game and keep score, invite a friend to be the third player. Your program must keep score impartially while participating in the game, but this is not a difficult task for the computer.
Rules of the Game:
- Number of participating Players: 2 students and 1 computer
- The player who goes first shall define the number of stones in the pile. The number must be between 30 to 50.
- Each player then removes some number (between 1 to 3) of stones from the pile in turn until one player removes the final stone.
- The player who goes first:
- Provides the number of stones to be placed in the pile,
- Removes the first set of 1 to 3 stones
- Other player removes a set of 1 to 3 stones
- The players then take turns(iteration) until the final stone is removed.
- The player who removes the final stone is the winner (student player 1, student player 2 and the computer).
The program must perform the following:
- Use the System I/O (print (), or println()) to introduce and describe the game, Computer shall introduce itself as the AI player, and prompt the human players for his or her names along with MIT Ids. 2 marks
- The program must use a Scanner object to receive the inputted response. 2 marks
- The response must be assigned to a properly typed variable. 2 marks
- Uses printf() to display a greeting to the human player that incorporates the name provided in response to the prompt: Welcome <user’s name> to the game of Taking Stones.4 marks
- The user’s name must be output in proper name case (i.e., mixed case starting with upper case for the first letter) regardless of the case the user types in. 4 marks
- The users shall input the number of stones to be picked either by using a standard random number generator function or may input according to their own strategy but the number should be less than or equal to three (3). 3 marks
- Java has two ways to generate random numbers
- Random method from the Java API Math
- util.random Class and the appropriate method from that class, to generate this number for the human player
- The program must reject and display a valid message if the number of stones to be removed are either less than one(1) or more than three(3) students and remind them the game constrains. 2 marks
- Prompt that player to re-enter an appropriate number. 2 marks
- Your program should have the computer use the optimal playing strategy. The optimal strategy is as follows: Divide the remaining number of stones by three. If the remainder is zero, then two stones are removed, or else one stone is removed. For example, if the remaining number of stones is nine or fifteen, then two stones are removed; if the remaining number of stones is eight or ten, then one stone is removed. 4 marks
- Both the human players shall have their own strategies to play. 4+4 marks
- When one of the player has won a game, the program must output a congratulatory message naming the winner along with ID. 4 marks
- Your program should allow the users to play additional games of NIM as long as he/she enters a “y” or “yes” (lowercase or uppercase) in response to a “Do you want to play again?” prompt. 5 marks
Marking criteria:
Section to be included in the report
|
Description of the section
|
Marks
|
|
|
|
Algorithm
Program
|
All the minute details have to be addressed in the algorithm
Error free, well commented java program reflecting each line of the algorithm.
|
42
42
|
|
Following points to be considered:
Include appropriate import statements
Include a comment at the beginning of your program with basic information and a description of the program
Your code should be properly indented
Give meaningful names to variables and classes in your code.
Correct use of programming structures
Your program compiles successfully without any errors
Your program is interactive and gives correct output.
Your program should follow all the rules mentioned above.
Sufficient test cases considering all the requirements must be provided with valid and invalid inputs.
|
|
Draft copy submission in week 10
|
Students who fail to submit draft copies of both the algorithm and the program shall lose marks.
|
6
|
Demonstration and Viva
|
Tutor will see the outputs and assess your understanding of work done for this assignment in the laboratory class. This will be done during Week-11 laboratory class.
Please Note: In case the student remains absent during week 11 for demonstration and viva without prior permission for special consideration through AMS, he /she may lose Demo and Viva marks.
|
10
|
|
Total
|
100
|
Grades
|
HD>80%
|
DI (70-79%)
|
CR (60-60%)
|
P (50% - 59%)
|
F (<50%)
|
Task
|
Optimal Algorithm devised (logic used) taking into considerations all possible end conditions as defined.
Flawless Code implemented for the devised algorithm without and gap between the algorithm and the program.
|
Relevant Algorithm devised (logic used) taking into considerations all possible end conditions.
Flawless Code implemented
|
Generally relevant Algorithm devised (logic used) taking into considerations most of the end conditions.
Flawless Code implemented.
|
Some relevance in the algorithm and few conditions are considered.
|
The logic used is not relevant.
|
Demonstration
|
Logic is clear and easy to follow with strong arguments
|
Consistency logical and convincing
|
Mostly consistent logical and convincing
|
Adequate cohesion and conviction
|
Not clear with the logic and could not answer simple and basic questions.
|
Answer:
ALGORITHM
DEFINE validValue( val ):
return val < 1 or > 3:
END
DEFINE inputOption():
Display options
Input option
CHECK if option is 1:
INPUT stones to pick
while not validValue(Stones):
Input stones to pick
OTHERWISE:
Generate random number 1-3 to pick stones
return stones
END
DEFINE playGamePlayer( player, total ):
Display player details
stones = inputOption()
CHECK if total <= stones:
return 0
OTHERWISE:
return total – count;
END
DEFINE playGameAI(total):
CHECK if total is divisible by 3:
stones = 2
OTHERWISE:
stones = 1
CHECK if total <= stones:
return 0
OTHERWISE:
return total – count;
END
DEFINE main():
INPUT player1
INPUT player2
INPUT total number of stones
WHILE total != 0:
total = playGame(player1, total)
CHECK if total is 0:
winner is player 1
break
total = playGame(player2, total)
CHECK if total is 0:
winner is player 2
break
total = playGameAI( total)
CHECK if total is 0:
winner is AI player
break
DISPLAY the winner