Question
Write a program to play the game of Nim with the computer. To play Nim, start with 15 tokens. Each player takes from one up
Write a program to play the game of Nim with the computer. To play Nim, start with 15 tokens. Each player takes from one up to a maximum of three tokens at each turn, and the player taking the last token loses. Randomly choose whether the user or the computer moves first. At start up, display all the tokens. When it is users turn, display a prompt message to ask the user to select his/her tokens. Let the user select a token with a mouse click, which should cause the token to be erased. When finished selecting tokens, the user should press the D key. Make sure the user never selecting more than three tokens. The computer should print a message stating how many tokens it chooses, and these tokens should be erased. When the game is over, display a message announcing the winner. User may press the N key to start a new Game.
You may implement an easy version. Instead of playing against the computer, two players play each other.
Help:
Using an array of bool type called cleared to hold 15 bool value and all initialized to false. Using a bool variable called turn. If turn is true means, it is first players turn otherwise it is second players turn. Every time there is a mouse click, locate the index where he/she clicked. If it is a painted square and the user has not clicked more than 3 times, set the cleared array at the index to true, call invalidate to repaint then the square will be cleared. Every time, a D key is pressed, check for winner. If there is one token left, whoever has the turn loses. If there is no token left, whoever has the turn wins. When the user pressed N key, reset all variables.
Must be written in C#
Must be modified from the exsisting program
Keep is very simple and do the 2 player version
using System using System.Drawing 10 using System.Windows. Forms; 11 12 E public class ColorChips Form f private bool clicked new bool [10,10] 13 14 public Color Chips f Size (300,200) 15 Size new Text Color Chips 16 for (int i 0; i 10 it 17 j++) for (int j 0; 10 18 clicked i, j] false 19 20 21 E protected override void OnPaint (PaintEventArgs e Graphics g e Graphics 22 int h Display Rectangle. Height int Display Rectangle. Width 24 Random r new Random for (int i 0; i 10 it 26 for (int j 0; j 10 j++) 27 if clicked i, j]) 28 29 Color color 30 Color.FromArgb 31 (r. Next (256), r.Next (256) r. Next (256) 32 Brush brush new SolidBrush (color) g Fill Rectangle(brush, i W 10 j h 10, w 10, h 10); 34 using System using System.Drawing 10 using System.Windows. Forms; 11 12 E public class ColorChips Form f private bool clicked new bool [10,10] 13 14 public Color Chips f Size (300,200) 15 Size new Text Color Chips 16 for (int i 0; i 10 it 17 j++) for (int j 0; 10 18 clicked i, j] false 19 20 21 E protected override void OnPaint (PaintEventArgs e Graphics g e Graphics 22 int h Display Rectangle. Height int Display Rectangle. Width 24 Random r new Random for (int i 0; i 10 it 26 for (int j 0; j 10 j++) 27 if clicked i, j]) 28 29 Color color 30 Color.FromArgb 31 (r. Next (256), r.Next (256) r. Next (256) 32 Brush brush new SolidBrush (color) g Fill Rectangle(brush, i W 10 j h 10, w 10, h 10); 34Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started