Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i ham working on tic tac toe.i have a file menu in which if we select player one and player two it should take input from us and when we select player one and computer it should take one input from us and other fram the computer. but it is not doing that can anyon help.........


C#
private void playerOneToolStripMenuItem_Click(object sender, EventArgs e)
       {
           //twoPlayersPlayGame();
           playerOneToolStripMenuItem.Checked = true;
       }

       private void playerTwoToolStripMenuItem_Click(object sender, EventArgs e)
       {
           twoPlayersPlayGame();
       }

       private void computerToolStripMenuItem_Click(object sender, EventArgs e)
       {
           withComputer();
       }


       private bool twoPlayersPlayGame()
       {
           playerOneToolStripMenuItem.Checked = true;
           playerTwoToolStripMenuItem.Checked = true;
           computerToolStripMenuItem.Checked = false;
           return true;
       }

       private bool withComputer()
       {
           playerOneToolStripMenuItem.Checked = true;
           playerTwoToolStripMenuItem.Checked = false;
           computerToolStripMenuItem.Checked = true;
          // computerPlayer = true;
           return true;
       }




C#
private void Button_click(object sender, EventArgs e)
       {
           Button b = (Button)sender;
           if (twoPlayersPlayGame())
          {
               if (twoPlayersPlayGame() && playerTurn == true)
               {
                   b.Text = "X";
                   playerTurn = false;
                   b.Enabled = false;
                  computerPlayer = true;
               }
               else
               {
                   b.Text = "O";
                   playerTurn = true;
                   b.Enabled = false;
               }
               clicksCount++;
               checkWinner();
          }
           if (withComputer())
          {

               if (!playerTurn && computerPlayer)
               {
                   computerMove();
                   p2TextBox2.Text = "Computer";
               }
           }

       }
Posted
Comments
[no name] 20-Oct-14 21:39pm    
Since your button click is checking twoPlayersPlayGame and that method is always returning true, what would expect it to do? Then you check withComputer and that always returns true also....
Member 10740412 20-Oct-14 21:42pm    
but for checking to play with computer the withComputer() i have made payerone and only computer two true then why its doing this.
[no name] 20-Oct-14 21:47pm    
Because you are checking both methods and both methods are true. That's what "return true" means.
Member 10740412 20-Oct-14 22:01pm    
then how else i can do this????? please help.....
BillWoodruff 20-Oct-14 23:48pm    
Are both player one and two on the same machine, using the same mouse and/or keyboard ?

Or ?

1 solution

Setting values within a method and the value returned from the method are not the same:
if you have a method called nnPlayersGame and you write code to call it:
C#
if (nnPlayersGame())
   {
   // True path
   ...
   }
else
   {
   // False path
   ...
   }
then whether the code follows the "True path" or the "False path" depends only on the value specified in the return statement of the method:
C#
public bool nnPlayersGame()
   {
   return numberOfPlayers > 1;
   }

Depending on the value of numberOfPlayers the code will follow different paths:
0     False path
1     False path
2     True path
3     True path
So you need to make your methods return the correct value, or check the values that they set instead of calling them again:
C#
private void Button_click(object sender, EventArgs e)
       {
           Button b = (Button)sender;
           if (twoPlayersPlayGame())
          {
               if (playerTwoToolStripMenuItem.Checked && playerTurn == true)
               {
                   b.Text = "X";
                   playerTurn = false;
                   b.Enabled = false;
                  computerPlayer = true;
               }
               else
               {
                   b.Text = "O";
                   playerTurn = true;
                   b.Enabled = false;
               }
               clicksCount++;
               checkWinner();
          }
           if (computerToolStripMenuItem.Checked)
          {

               if (!playerTurn && computerPlayer)
               {
                   computerMove();
                   p2TextBox2.Text = "Computer";
               }
           }
       }
Should get you a little further.
 
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