public void Compute(String difficulty) { if (difficulty.Equals("Hard")) { minimax(0, 1); }else if(difficulty.Equals("Medium")) { computersMove = win(); if (computersMove == null) { computersMove = block(); if (computersMove == null) { computersMove = Fork_block(); if (computersMove == null) { computersMove = mark_center(); if (computersMove == null) { computersMove = mark_coner(); if (computersMove == null) { computersMove = random_play(); } } } } } }else{ computersMove=win(); if (computersMove == null) { computersMove = block(); if (computersMove == null) { computersMove = random_play(); } } } }
public void set_play(Square spot) { if (spot.get_x() == 0) { if (spot.get_y() == 0) { A1.Text = "O"; A1.Enabled = false; } if (spot.get_y() == 1) { A2.Text = "O"; A2.Enabled = false; } if (spot.get_y() == 2) { A3.Text = "O"; A3.Enabled = false; } } if (spot.get_x() == 1) { if (spot.get_y() == 0) { B1.Text = "O"; B1.Enabled = false; } if (spot.get_y() == 1) { B2.Text = "O"; B2.Enabled = false; } if (spot.get_y() == 2) { B3.Text = "O"; B3.Enabled = false; } } if (spot.get_x() == 2) { if (spot.get_y() == 0) { C1.Text = "O"; C1.Enabled = false; } if (spot.get_y() == 1) { C2.Text = "O"; C2.Enabled = false; } if (spot.get_y() == 2) { C3.Text = "O"; C3.Enabled = false; } } }
private int minimax(int depth, int turn) { if (hasXWon()) return +1; if (hasOWon()) return -1; List<Square> pointsAvailable = getAvailableStates(); if (pointsAvailable.Count() == 0) return 0; int min = 1000, max = -1000; for (int i = 0; i < pointsAvailable.Count(); ++i) { Square point = pointsAvailable.ElementAt(i); if (turn == 1) { play(point, 1); int currentScore = minimax(depth + 1, 2); max = Math.Max(currentScore, max); //if(depth == 0)System.out.println("Score for position "+(i+1)+" = "+currentScore); if (currentScore >= 0) { if (depth == 0) computersMove = point; } if (currentScore == 1) { board[point.get_x(), point.get_y()] = 0; break; } if (i == pointsAvailable.Count() - 1 && max < 0) { if (depth == 0)computersMove = point; } } else if (turn == 2) { play(point, 2); int currentScore = minimax(depth + 1, 1); min = Math.Min(currentScore, min); if (min == -1) { board[point.get_x(), point.get_y()] = 0; break; } } board[point.get_x(), point.get_y()] = 0; //Reset this point } return turn == 1 ? max : min; }
public void play(Square spot, int player) { this.board[spot.get_x(), spot.get_y()] = player; }