Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two player and they can only enter interger 1-3.the rest is error.
Always player 1 enter a number first.
Number 1 problem
At beginning of the game after player 1 entered a value its gives me error message "Error: An invalid was entered." even if its 1 - 3 value but it allow to subtract the value(1 to 3) to the starting value.
C#
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;
        LBl_msg.Text = string.Format("Press start to play");
    }
    private void BTn_Start_Click(object sender, EventArgs e)
    {
        BTn_Start.Enabled = false;
        BTn_Play.Enabled = true;
        TBx_P1.Enabled = !TBx_P1.Enabled;
        TBx_P2.Enabled = !TBx_P1.Enabled;
        if (TBx_P1.Enabled)
        {
            TBx_P1.Select();
            LBl_msg.Text = string.Format("Player number 1 plays.");
        }
        else if (TBx_P2.Enabled)
        {
            TBx_P2.Select();
            LBl_msg.Text = string.Format("Player number 2 plays.");
        }
    }
    private void BTn_Play_Click(object sender, EventArgs e)
    {
        try
        {
            if (TBx_P1.Enabled == true)
            {
                i_Play = int.Parse(TBx_P1.Text);

                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;
                    LBl_msg.Text = string.Format("Player number 1 plays.");
                }
                else
                {
                    TBx_P1.Enabled = false;
                    TBx_P1.Focus();
                    TBx_P2.Enabled = true;
                    LBl_msg.Text = string.Format("Player number 2 plays.");
                    m_Total = m_Total - i_Play;
                    LBl_CNum.Text = m_Total.ToString();
                }

                if (m_Total == 0)
                {
                    LBl_msg.Text = string.Format("Player number 1 wins");
                }
            }
            if (TBx_P2.Enabled == true)
            {
                i_Play = int.Parse(TBx_P2.Text);

                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_P2.Enabled = true;
                    TBx_P2.Focus();
                    TBx_P1.Enabled = false;
                    LBl_msg.Text = string.Format("Player number 2 plays.");
                }
                else
                {
                    TBx_P2.Enabled = false;
                    TBx_P2.Focus();
                    TBx_P1.Enabled = true;
                    LBl_msg.Text = string.Format("Player number 1 plays.");
                    m_Total = m_Total - i_Play;
                    LBl_CNum.Text = m_Total.ToString();
                }
                if (m_Total == 0)
                {
                    LBl_msg.Text = string.Format("Player number 2 wins");
                }
            }
        }
        catch (FormatException)
        {
            MessageBox.Show("Error: An invalid was entered.", "ICA 2 - Nim",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

i don't know why its gives me error....
Posted
Comments
[no name] 27-Oct-11 21:14pm    
Number2 problem:
The starting value is 21
Player1 enter a value (3), current value (18). Next
Player2 enter a value(3), current value (15). Next
this where the problem is:
Player1 enter a value (3), current value becomes 9 and it won't switch to player 2 after this play...

i don't know how to fix it...
Sergey Alexandrovich Kryukov 27-Oct-11 21:33pm    
Did you run it under debugger?
--SA

1 solution

use the following code. It would work.

I have added an else for the textbox enabled check condition in BTn_Play_Click handler.

The problem was after player 1 plays you were setting the textbox for player 2 as enabled. which resulted in control going into the next "if" and the text box being empty threw a format exception.

C#
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;
            LBl_msg.Text = string.Format("Press start to play");
        }
        private void BTn_Start_Click(object sender, EventArgs e)
        {
            BTn_Start.Enabled = false;
            BTn_Play.Enabled = true;
            TBx_P1.Enabled = !TBx_P1.Enabled;
            TBx_P2.Enabled = !TBx_P1.Enabled;
            if (TBx_P1.Enabled)
            {
                TBx_P1.Select();
                LBl_msg.Text = string.Format("Player number 1 plays.");
            }
            else if (TBx_P2.Enabled)
            {
                TBx_P2.Select();
                LBl_msg.Text = string.Format("Player number 2 plays.");
            }            
        }
        private void BTn_Play_Click(object sender, EventArgs e)
        {
            try
            {
                if (TBx_P1.Enabled == true)
                {
                    i_Play = int.Parse(TBx_P1.Text);

                    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;
                        LBl_msg.Text = string.Format("Player number 1 plays.");
                    }
                    else
                    {
                        TBx_P1.Enabled = false;
                        TBx_P1.Focus();
                        TBx_P2.Enabled = true;
                        LBl_msg.Text = string.Format("Player number 2 plays.");
                        m_Total = m_Total - i_Play;
                        LBl_CNum.Text = m_Total.ToString();
                    }

                    if (m_Total == 0)
                    {
                        LBl_msg.Text = string.Format("Player number 1 wins");
                    }
                }
                else if (TBx_P2.Enabled == true)
                {
                    i_Play = int.Parse(TBx_P2.Text);

                    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_P2.Enabled = true;
                        TBx_P2.Focus();
                        TBx_P1.Enabled = false;
                        LBl_msg.Text = string.Format("Player number 2 plays.");
                    }
                    else
                    {
                        TBx_P2.Enabled = false;
                        TBx_P2.Focus();
                        TBx_P1.Enabled = true;
                        LBl_msg.Text = string.Format("Player number 1 plays.");
                        m_Total = m_Total - i_Play;
                        LBl_CNum.Text = m_Total.ToString();
                    }
                    if (m_Total == 0)
                    {
                        LBl_msg.Text = string.Format("Player number 2 wins");
                    }
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("Error: An invalid was entered.", "ICA 2 - Nim",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900