Home c# Noliki crossbars on C #

Noliki crossbars on C #

Author

Date

Category

How to make the program not to put ticks in those fields that are already filled?

using system;
Using System.Collections.Genic;
Using System.comPonentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
using System.Windows.Forms;
Namespace windowsFormsApplication1
{
  Public Partial Class Form1: Form
  {
    String Name;
    BOOL PC = FALSE;
    int hod = 1;
    BOOL TURN = TRUE; // True = X TURN, FALSE = Y TURN
    INT TURN_COUNT = 0;
    Public Form1 ()
    {
      Initializecomponent ();
    }
    Private Void Form1_Load (Object Sender, Eventargs E)
    {
    }
    Public Void Check ()
    {
      {
        Random RND = New Random ();
        int p = rnd.next (0, 8);
        if ((pc == true) || (Name! = A1.Text) || (Name! = A2.Text) || (Name! = A3.TEXT)
          || (Name! = B1.Text) || (Name! = B2.Text) || (Name! = B3.TEXT) || (Name! = C1.TEXT)
          || (Name! = C2.Text) || (Name! = C3TEXT))
        {
          Switch (P)
          {
            Case 0: A1.Text = "O";
              A1.Enabled = false;
              TURN_COUNT ++;
              Break;
            Case 1: A2.Text = "O";
              A2.Enabled = false;
              TURN_COUNT ++;
              Break;
            CASE 2: A3.TEXT = "O";
              A3.Enabled = false;
              TURN_COUNT ++;
              Break;
            CASE 3: B1.TEXT = "O";
              B1.Enabled = false;
              TURN_COUNT ++;
              Break;
            CASE 4: B2.TEXT = "O";
              B2.Enabled = false;
              TURN_COUNT ++;
              Break;
            CASE 5: B3.TEXT = "O";
              B3.Enabled = false;
              TURN_COUNT ++;
              Break;
            Case 6: C1.Text = "O";
              C1.Enabled = false;
              TURN_COUNT ++;
              Break;
            CASE 7: C2.TEXT = "O";
              C2.Enabled = false;
              TURN_COUNT ++;
              Break;
            Case 8: C3.Text = "O";
              C3.Enabled = false;
              TURN_COUNT ++;
              Break;
          }
        }
      }
    }
    Private Void Button_Click (Object Sender, Eventargs E)
    {
      Button B = (Button) Sender;
      if (Turn)
        B.Text = "X";
      if (! Turn)
        B.Text = "O";
      PC = TRUE;
      Name = (Sender As Button) .name;
      label1.text = Name + Label1.Text;
      b.Enabled = false;
      TURN_COUNT ++;
      label1.text = turn_count.tostring ();
      checkwinner ();
      check ();
    }
    Private Void Checkwinner ()
    {
      BOOL IT_IS_WIN = FALSE;
      Foreach (Control C In Controls)
      {
        if (((A1.Text == B1.TEXT) & amp; & amp; (b1.text == C1.Text) & amp; & amp; (! A1.Enabled))
          IT_IS_WIN = TRUE;
        if ((B1.Text == B2.Text) & amp; & amp; (b2.text == b3.text) & amp; & amp; (! B1.Enabled))
          IT_IS_WIN = TRUE;
        If ((C1.Text == C2.Text) & amp; & amp; (c2.text == C3TEXT) & amp; & amp; (! C1.Enabled))
          IT_IS_WIN = TRUE;
         if (((A1.Text == B1.TEXT) & amp; & amp; (b1.text == C1.Text) & amp; & amp; (! A1.Enabled))
          IT_IS_WIN = TRUE;
        ELSE if ((a2.text == b2.text) & amp; & amp; (b2.text == C2.Text) & amp; & amp; (! A2.Enabled))
          IT_IS_WIN = TRUE; 
ELSE if ((a3.text == B3.Text) & amp; & amp; (b3.text == C3TEXT) & amp; & amp; (! A3.Enabled))
           IT_IS_WIN = TRUE;
         ELSE If ((A1.Text == B2.Text) & amp; & amp; (B2.Text == C3TEXT) & amp; & amp; (! A1.Enabled))
           IT_IS_WIN = TRUE;
         ELSE If ((A3.Text == B2.Text) & amp; & amp; (B2.Text == C1.Text) & amp; & amp; (! C1.Enabled))
           IT_IS_WIN = TRUE;
        if (IT_IS_WIN)
        {
          disableButton ();
          String Winner = "";
          if (Turn)
            Winner = "X";
          if (! Turn)
            Winner = "O";
          MessageBox.Show (WINNER + "WINS");
        }
        ELSE.
        {
          if (Turn_Count == 9)
            MessageBox.Show ("Draw");
        }
      }
    }
    Private Void DisableButton ()
    {
      Try.
      {
        Foreach (Control C In Controls)
        {
          Button B = (Button) C;
          b.Enabled = false;
        }
      }
      Catch {}
    }
    Private Void New AigritoolStripmenuItem_Click (Object Sender, Eventargs E)
    {
      TURN = TRUE;
      Turn_Count = 0;
      Try.
      {
        Foreach (Control C In Controls)
        {
          Button B = (Button) C;
          b.Enabled = true;
          B.Text = "";
        }
      }
      Catch {}
    }
  }
}

There is a code. I thought to add to the variable the names pressed buttons and compare with the name of the button where the computer placed the zerolik, but something is not so hot.


Answer 1, Authority 100%

If you briefly, you just need to put the talkers, first check if there is no other (milf simple).
And if a long time, it would be possible to solve this problem more clear and correct way to solve this task with dimensions in the fields in your game (the easiest option 3×3, for example).
That is, at the start of the game, we set all the values ​​in the Masiva to some number, for example 5 (this will mean that no one has yet done a move and 5 means an empty cell)

| 5 | 5 | 5 |
| 5 | 5 | 5 |
| 5 | 5 | 5 |

then suppose the computer makes the first move – it is indicating its number 1 (this is as a cross).

| 5 | 5 | 5 |
| 5 | 1 | 5 |
| 5 | 5 | 5 |

the player answers him and puts somewhere 0 (you need to check first that in this cell costs 5 (then it is empty)):

| 5 | 5 | 5 |
| 5 | 1 | 5 |
| 5 | 5 | 0 |

and so on ..
After each move, we launch a function that checks if the player did not win (there is no vertical, horizontally, the diagonal of the row 1 or 0).
And the task of drawing will simply come down to the case of the form:

if 0: draw 0,
If 1: Draw +,
5: Do not draw anything

Such a branch of the logic of the game from her draw will make the spelling of the game at times easier and clearer, and get confused here practically nowhere.


Answer 2, Authority 67%

Private Void Button_Click (Object Sender, Eventargs E)
{
 Button B = (Button) Sender;
 If (! String.ISNULLOREMPTY (B.Text))
  Return;
 ...
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions