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 = "{ }"; }
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; } }