TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jonathan Crispe
NA
46
48.4k
Windows forms using C# - Game of Nim
Sep 5 2011 10:02 PM
Instruction:
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 need help: i could not switch from player 1 to player 2 and
i don't know how to put the
"invalid value was entered" in code. Thank you for your help...
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;
}
}
}
}
Reply
Answers (
2
)
Help me to make the following label control
Error getting while emdedding the font in the application.