Example #1
0
        public void reset()
        {
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                TTTButton b = c as TTTButton;
                if (b == null)
                {
                    continue;
                }

                b.BackgroundImage = null;
                b.Enabled         = true;
                b.XO      = null;
                b.TabStop = false;
            }
            textBox.Show();
            label.Show();
            canClick     = true;
            player_count = 0;
            replayButton.Hide();
            continuePlaying.Hide();
            replay.Clear();
            whoWins        = 0;
            canChangeLevel = true;
        }
Example #2
0
 private void Play_Click(object sender, EventArgs e)
 {
     play.Hide();
     score.Hide();
     textBox.Show();
     label.Show();
     checkBox1.Show();
     canPlay = true;
     foreach (Control c in tableLayoutPanel1.Controls)
     {
         TTTButton b = c as TTTButton;
         if (b == null)
         {
             continue;
         }
         b.Show();
     }
 }
Example #3
0
        public TTTButton[] Butarr()
        {
            List <TTTButton> butlist = new List <TTTButton>();

            foreach (Control c in tableLayoutPanel1.Controls)
            {
                TTTButton b = c as TTTButton;
                if (b == null)
                {
                    continue;
                }
                if (b.XO == null)
                {
                    butlist.Add(b);
                }
            }
            TTTButton[] arr = butlist.ToArray();
            return(arr);
        }
Example #4
0
 private void Form1_Load(object sender, EventArgs e)
 {
     reset();
     canPlay = false;
     foreach (Control c in tableLayoutPanel1.Controls)
     {
         TTTButton b = c as TTTButton;
         if (b == null)
         {
             continue;
         }
         b.Hide();
     }
     textBox.Hide();
     label.Hide();
     checkBox1.Hide();
     if (!Directory.Exists(Root))
     {
         Directory.CreateDirectory(Root);
     }
 }
Example #5
0
        private void ReplayButton_Click(object sender, EventArgs e)
        {
            canPlay = false;
            checkBox1.Hide();
            continuePlaying.Hide();
            replayButton.Hide();
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                TTTButton b = c as TTTButton;
                if (b == null)
                {
                    continue;
                }

                b.BackgroundImage = null;
                b.Enabled         = true;
                b.XO = null;
            }
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                TTTButton b = c as TTTButton;
                if (b == null)
                {
                    continue;
                }
                b.Enabled = false;
            }
            TTTButton[] buttonArr = replay.ToArray();
            bool        turn      = true;

            for (int i = 0; i < buttonArr.Length; i++)
            {
                if (turn)
                {
                    Application.DoEvents();
                    buttonArr[i].BackgroundImage = Tic_Tac_Tor.Properties.Resources.redO;
                }
                else
                {
                    Application.DoEvents();
                    buttonArr[i].BackgroundImage = Tic_Tac_Tor.Properties.Resources.redX;
                }
                turn = !turn;
                System.Threading.Thread.Sleep(500);
            }
            if (whoWins == 1)
            {
                MessageBox.Show("Player O won", "O");
            }
            if (whoWins == 2)
            {
                MessageBox.Show("Draw", "Draw");
            }
            if (whoWins == 3)
            {
                MessageBox.Show("Player X won", "X");
            }
            replayButton.Show();
            continuePlaying.Show();
            checkBox1.Show();
            canPlay = true;
        }
Example #6
0
        private void button_click(object sender, EventArgs e)
        {
            canChangeLevel = false;
            if (canClick == false)
            {
                return;
            }
            if (textBox.Text == "")
            {
                MessageBox.Show("Enter your name first", "Enter you name");
                return;
            }
            textBox.Hide();
            label.Hide();
            TTTButton button = sender as TTTButton;

            player_count++;

            button.XO = "O";
            button.BackgroundImage = Tic_Tac_Tor.Properties.Resources.redO;
            button.Enabled         = false;
            replay.Add(button);

            if (checkForAWinner())
            {
                canClick = false;
                whoWins  = 1;
                writeAtFile(textBox.Text, 1);
                MessageBox.Show("Player O is the winner", "We have a winner");
                replayButton.Show();
                continuePlaying.Show();
                canChangeLevel = true;
                return;
            }

            if (player_count == 16 && checkForAWinner() == false)
            {
                canClick = false;
                whoWins  = 2;
                MessageBox.Show("It's a draw", "draw");
                writeAtFile(textBox.Text, 2);
                replayButton.Show();
                continuePlaying.Show();
                canChangeLevel = true;
                return;
            }

            if (level == 10)
            {
                level = 3;
            }
            TTTButton[]        bArr          = Butarr();
            List <TTTButton>[] buttonsScores = new List <TTTButton>[3] {
                new List <TTTButton>(), new List <TTTButton>(), new List <TTTButton>()
            };
            Random random = new Random();

            foreach (TTTButton button1 in bArr)
            {
                button1.XO = "X";
                int score = minMax(level, true);
                button1.XO = null;
                buttonsScores[score + 1].Add(button1);
            }
            for (int i = 0; i <= 2; i++)
            {
                int nScores = buttonsScores[i].Count;
                if (nScores > 0)
                {
                    int       iBtn = random.Next(0, nScores);
                    TTTButton b    = buttonsScores[i][iBtn];
                    b.BackgroundImage = Tic_Tac_Tor.Properties.Resources.redX;
                    b.Enabled         = false;
                    b.XO = "X";
                    player_count++;
                    replay.Add(b);
                    canClick = true;
                    canPlay  = true;
                    break;
                }
            }



            if (checkForAWinner())
            {
                canClick = false;
                whoWins  = 3;
                MessageBox.Show("Player X is the winner", "We have a winner");
                writeAtFile(textBox.Text, 3);
                replayButton.Show();
                continuePlaying.Show();
                canChangeLevel = true;
                return;
            }

            if (player_count == 16 && checkForAWinner() == false)
            {
                canClick = false;
                whoWins  = 2;
                MessageBox.Show("It's a draw", "draw");
                writeAtFile(textBox.Text, 2);
                replayButton.Show();
                continuePlaying.Show();
                canChangeLevel = true;
                return;
            }
        }