void button_click_human(object sender, EventArgs e) { Button button = sender as Button; if (human_Players) //2 player button clicks { if (button.Text != "") { return; } button.Text = X_O_display_button.Text; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (buttons[i, j].Text != "") { evaluation_Board[i, j] = buttons[i, j].Text; } } } switchPlayer(); if (gameover == true) { gameover = false; } return; } //human clicks versus AI if (X_O_display_button.Text == human) { if (button.Text == "") { button.Text = human; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (buttons[i, j].Text == human && evaluation_Board[i, j] == "") { evaluation_Board[i, j] = human; } } } switchPlayer(); if (ai_Player && X_O_display_button.Text == ai) { Minimax ai_minimax = new Minimax(); int[] ai_move = ai_minimax.Ai_bestMove(evaluation_Board, buttons, X_O_display_button.Text); evaluation_Board[ai_move[0], ai_move[1]] = ai; buttons[ai_move[0], ai_move[1]].Text = ai; switchPlayer(); } } } }
public void switchPlayer() { Minimax ai_minimax = new Minimax(); if (ai_Player) { ai_minimax.CheckIfgameEnds(buttons, X_O_display_button.Text, evaluation_Board, ai_Player, playerNumber.Text); } else { ai_minimax.CheckIfgameEnds(buttons, X_O_display_button.Text, evaluation_Board, human_Players, playerNumber.Text); } if (ai_minimax.gameover) { Win_counter(ai_minimax.winnersPiece); Reset(); ai_minimax.gameover = false; return; } if (X_O_display_button.Text == ai && gameover == false) { X_O_display_button.Text = human; playerNumber.Text = "Player 1"; } else if (X_O_display_button.Text != ai && gameover == false) { playerNumber.Text = "Player 2"; X_O_display_button.Text = ai; } if (X_O_display_button.Text == ai) { label2.Text = ai + "'s turn"; } else { label2.Text = human + "'s turn"; } }
private void Ai_RandomMove() { int random_Num1 = random_Move.Next(0, 3); //random first move after activation or reset int random_Num2 = random_Move.Next(0, 3); foreach (var button in buttons) { if (button.Text != "") { Minimax ai_Minimax = new Minimax(); int[] ai_move = ai_Minimax.Ai_bestMove(evaluation_Board, buttons, X_O_display_button.Text); evaluation_Board[ai_move[0], ai_move[1]] = ai; buttons[ai_move[0], ai_move[1]].Text = ai; switchPlayer(); return; } } buttons[random_Num1, random_Num2].Text = ai; evaluation_Board[random_Num1, random_Num2] = ai; switchPlayer(); }