public static int Minimax(Board board, BoardCell targetedCell, int depth, bool maximisingPlayer) { Board evolvedBoard = (Board)board.Clone(); evolvedBoard.Cells[targetedCell.X][targetedCell.Y] = targetedCell; if (depth == 0 || evolvedBoard.FreeCells == 0) { return(DecisionManager.Heuristic(evolvedBoard, targetedCell)); } Polygon assessablePolygon = DecisionManager.TraceAssessablePolygon(evolvedBoard); int bestValue; if (maximisingPlayer) { bestValue = -1000; foreach (BoardCell cell in evolvedBoard.CellList) { if (cell.State == BoardCell.CellState.Free && assessablePolygon.Contains(new Point { X = cell.X, Y = cell.Y })) { int result = DecisionManager.Minimax(evolvedBoard, cell, depth - 1, false); bestValue = Math.Max(bestValue, result); } } return(bestValue); } bestValue = 1000; foreach (BoardCell cell in evolvedBoard.CellList) { if (cell.State == BoardCell.CellState.Free && assessablePolygon.Contains(new Point { X = cell.X, Y = cell.Y })) { int result = DecisionManager.Minimax(evolvedBoard, cell, depth - 1, true); bestValue = Math.Min(bestValue, result); } } return(bestValue); }
public async Task <GameState.LastPlayed> TakeDecision(Game game) { if (game.GameState.Turn == 0) { return(new GameState.LastPlayed(9, 9)); } GameState.LastPlayed decision = new GameState.LastPlayed(0, 0); Polygon assessablePolygon = DecisionManager.TraceAssessablePolygon(game.Board); foreach (BoardCell cell in game.Board.CellList) { if (cell.State == BoardCell.CellState.Free && assessablePolygon.Contains(new Point { X = cell.X, Y = cell.Y })) { DecisionManager.Minimax(game.Board, cell, 1, true); } } return(decision); }
public AI() { this._game = new Game(); this._decisionManager = new DecisionManager(); }