Example #1
0
 private void RestartGame()
 {
     allButtons.ForEach(btn =>
     {
         btn.Text      = "";
         btn.BackColor = darkBlue;
         btn.Enabled   = true;
     });
     PCtimer.Stop();
     availableButtons = panel1.Controls.OfType <Button>().ToList();
     playing          = false;
     lbPlayFirst.Text = "Prvi igra -> ";
     lbTurn.Text      = "{ }";
 }
Example #2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!playing)
            {
                RestartGame();
                first            = r.Next(0, 2);
                lbPlayFirst.Text = "Prvi igra -> " + first;
                playing          = true;
                if (first == 0)
                {
                    PCplay = true;
                    PCtimer.Start();
                }
                else
                {
                    PCplay = false;
                }

                lbTurn.Text = "{ " + ((first == 0) ? "Računar" : "Čovek") + " igra prvi }";
            }
        }
Example #3
0
        private void XorO_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;

            if (b.Text == "" && playing && !PCplay)
            {
                b.Text      = (first == 0) ? "O" : "X";
                lbTurn.Text = "{ " + ((!PCplay) ? "Računar" : "Čovek") + " je na potezu }";
                availableButtons.Remove(b);
                if (CheckForWin(matrixOfButtons, false))
                {
                    playing     = false;
                    lbTurn.Text = "{ " + ((PCplay) ? "Računar" : "Čovek") + " je pobedio }";
                    wait(waitTime);
                    if (new CustomDialog("Pobedio je " + ((PCplay) ? "Računar" : "Čovek") + "\n\nRestartovati igru?", "Pobeda").ShowDialog() == DialogResult.Yes)
                    {
                        RestartGame();
                    }
                    return;
                }
                if (availableButtons.Count == 0)
                {
                    lbTurn.Text = "{ Nerešeno }";
                    playing     = false;
                    allButtons.ForEach(btn => btn.Enabled = false);
                    wait(waitTime);
                    if (new CustomDialog("Nerešeno\n\nRestartovati igru?", "Nerešeno").ShowDialog() == DialogResult.Yes)
                    {
                        RestartGame();
                    }
                    return;
                }
                PCplay = true;
                PCtimer.Start();
            }
        }
Example #4
0
        private void PCtimer_Tick(object sender, EventArgs e)
        {
            if (PCplay && availableButtons.Count > 0)
            {
                if (true)
                {
                    Button[,] availableMoves = matrixOfButtons.Clone() as Button[, ];

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            if (availableMoves[i, j].Text == "")
                            {
                                availableMoves[i, j].Text = (first == 0) ? "X" : "O";
                                if (CheckForWin(availableMoves, true))
                                {
                                    availableMoves[i, j].Text  = "";
                                    matrixOfButtons[i, j].Text = (first == 0) ? "X" : "O";
                                    goto CHECK;
                                }
                                availableMoves[i, j].Text = "";
                            }
                        }
                    }

                    for (int k = 0; k < n; k++)
                    {
                        for (int l = 0; l < n; l++)
                        {
                            if (availableMoves[k, l].Text == "")
                            {
                                availableMoves[k, l].Text = (first == 0) ? "O" : "X";
                                if (CheckForWin(availableMoves, true))
                                {
                                    availableMoves[k, l].Text  = "";
                                    matrixOfButtons[k, l].Text = (first == 0) ? "X" : "O";
                                    for (int x = 0; x < availableButtons.Count; x++)
                                    {
                                        if (availableButtons[x].Name == "b_" + k + "_" + l)
                                        {
                                            availableButtons.RemoveAt(x);
                                        }
                                    }
                                    goto CHECK;
                                }
                                availableMoves[k, l].Text = "";
                            }
                        }
                    }
                }
                int index = r.Next(availableButtons.Count);
                availableButtons[index].Text = (first == 0) ? "X" : "O";
                availableButtons.RemoveAt(index);
CHECK:
                PCtimer.Stop();
                if (CheckForWin(matrixOfButtons, false))
                {
                    playing     = false;
                    lbTurn.Text = "{ " + ((PCplay) ? "Računar" : "Čovek") + " je pobedio }";
                    wait(waitTime);
                    if (new CustomDialog("Pobedio je " + ((PCplay) ? "Računar" : "Čovek") + "\n\nRestartovati igru?", "Pobeda").ShowDialog() == DialogResult.Yes)
                    {
                        RestartGame();
                    }
                    return;
                }
                if (availableButtons.Count == 0)
                {
                    lbTurn.Text = "{ Nerešeno }";
                    playing     = false;
                    allButtons.ForEach(btn => btn.Enabled = false);
                    wait(waitTime);
                    if (new CustomDialog("Nerešeno\n\nRestartovati igru?", "Nerešeno").ShowDialog() == DialogResult.Yes)
                    {
                        RestartGame();
                    }
                    return;
                }
                lbTurn.Text = "{ " + ((!PCplay) ? "Računar" : "Čovek") + " je na potezu }";
                PCplay      = false;
            }
        }