Labels, Textboxes and Buttons
You will construct a form-based program to play the ancient game of Nim. In the game of Nim, 2 players would remove objects from a pile in alternating moves. The player could remove 1, 2, or 3 objects at a time. The player who is forced to remove the last object from the pile loses the game.
The game that you will implement will have the players remove integer values of 1, 2, or 3 from a total value. Initially when the game is started, the total value will be 21. The player who is forced to remove the last value, making the total 0, loses the game.
Create the form shown below. Initially, the two textboxes for number entry, and the Play button, will be disabled.
When the Start button is pressed, the program will randomly select the player to make the first play. Enable the textbox for the selected player, and the set the focus (for input) to that textbox. The Start button will be disabled until the game is over. The selected player will enter an integer value of 1, 2 or 3. If a non-integer string was entered, or a value outside the range of 1, 2 or 3 was entered, a MessageBox will be used to display an error message. Also, if the value entered by the user exceeds the current total, an error message will be shown using a MessageBox. The value entered by the player will be deducted from the current total value. After each player has entered a value, that player's textbox will be cleared then disabled and the next player's textbox will be enabled and given the focus. After each move, the current total will be checked to determine if it has reached zero. When the current total reaches zero, the player who made the last move loses the game. A label will be used to display who is to make the next play, and the winner of game.
I know the instruction is very long, and i am sorry for that.
My problem in this programming is that i could not switch from player 1 to player 2.
i want to go randomly select either player 1 or 2 after pressing the "Start" Button, i just don't know how put in my code.
So far i have this:
namespace ICA2___Kristian_V
{
public partial class Form1 : Form
{
int i_Play = 0; //Player 1 or 2
int m_Total = 21; //Starting Number of the Game
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BTn_Play.Enabled = false;
TBx_P1.Enabled = false;
TBx_P2.Enabled = false;
}
private void BTn_Start_Click(object sender, EventArgs e)
{
BTn_Start.Enabled = false;
BTn_Play.Enabled = true;
TBx_P1.Enabled = true;
TBx_P2.Enabled = true;
}
private void BTn_Play_Click(object sender, EventArgs e)
{
i_Play = int.Parse(TBx_P1.Text);
m_Total = m_Total - i_Play;
LBl_CNum.Text = m_Total.ToString();
if ((i_Play < 1) || (i_Play > 3) || (i_Play > m_Total))
{
MessageBox.Show("Error: You must play 1, 2, 3 or less than the total.",
"ICA 2 - Nim", MessageBoxButtons.OK, MessageBoxIcon.Error);
TBx_P1.Enabled = true;
TBx_P1.Focus();
TBx_P2.Enabled = false;
}
else
{
TBx_P1.Enabled = false;
TBx_P1.Focus();
TBx_P2.Enabled = true;
}
}
}
}