static List<int> Greedy(Board board, int player) { List<int> greedyReturn = new List<int>(); // 0: points after move, 1: move X, 2: move Y, 3: helper X, 4: helper Y greedyReturn.Add(board.Evaluate(player)); greedyReturn.Add(-1); greedyReturn.Add(-1); greedyReturn.Add(-1); greedyReturn.Add(-1); List<List<int>> possibilities = board.GetPossibleMoves(player); if (possibilities.Count == 0) { return greedyReturn; } Board newBoard; foreach (List<int> move in board.GetPossibleMoves(player)) { // Get new board configuration newBoard = board.MakeMove(move, player); if (newBoard.Evaluate(player) > greedyReturn[0])//if better than last possible move { greedyReturn.Clear();//clear the last one greedyReturn.Add(newBoard.Evaluate(player)); foreach (int val in move) greedyReturn.Add(val); } } return greedyReturn; }
static List<int> Human(Board board, int player) { List<int> humanReturn = new List<int>(); humanReturn.Add(board.Evaluate(player)); humanReturn.Add(-1); humanReturn.Add(-1); humanReturn.Add(-1); humanReturn.Add(-1); if (board.GetPossibleMoves(player).Count == 0) { return humanReturn; } while (true) { Console.Write("enter x-position:"); humanReturn[1] = int.Parse(Console.ReadLine()); Console.WriteLine(); Console.Write("enter y-position:"); humanReturn[2] = int.Parse(Console.ReadLine()); Console.WriteLine(); foreach (List<int> move in board.GetPossibleMoves(player)) { if (humanReturn[1] == move[0] && humanReturn[2] == move[1]) { Board newBoard = board.MakeMove(move, player); humanReturn[3] = move[2]; humanReturn[4] = move[3]; return humanReturn; } } Console.WriteLine("invalid move!"); } }
//randomly choose a move static List<int> Unpredictable(Board board, int player) { List<int> UnpredictableReturn = new List<int>(); // 0: points after move, 1: move X, 2: move Y, 3: helper X, 4: helper Y List<List<int>> possibilities = board.GetPossibleMoves(player); if (possibilities.Count == 0) { UnpredictableReturn.Add(board.Evaluate(player)); UnpredictableReturn.Add(-1); UnpredictableReturn.Add(-1); UnpredictableReturn.Add(-1); UnpredictableReturn.Add(-1); return UnpredictableReturn; } Random random = new Random(); int randomMove = random.Next(0, possibilities.Count); List<int> move = possibilities[randomMove]; UnpredictableReturn.Add(board.MakeMove(move, player).Evaluate(player)); foreach (int val in move) UnpredictableReturn.Add(val); return UnpredictableReturn; }
//MiniMax base on number of moves static List<int> MiniMaxMove(Board board, int player, int callingPlayer, int maxDepth, int currentDepth) { // This array contains: bestScore, bestMoveX, bestMoveY in that order List<int> miniMaxReturn = new List<int>(); // This array contains: bestScore, bestMoveX, bestMoveY in that order List<int> currentReturn = new List<int>(); // Check for recursion end if (board.IsGameOver(player) || currentDepth == maxDepth) { miniMaxReturn.Add(board.GetPossibleMoves(callingPlayer).Count); miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); return miniMaxReturn; } // Depending on the player we choose a starting bestScore // Negative if currentPlayer so we always chose a higher scored board if (board.GetCurrentPlayer() == player) miniMaxReturn.Add(-int.MaxValue); // Positive for opponent so that they always choose a lower scored // board else miniMaxReturn.Add(int.MaxValue); // Contains default moveX, moveY miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); miniMaxReturn.Add(-1); Board newBoard; // Recurse for each move foreach (List<int> move in board.GetPossibleMoves(player)) { // Get new board configuration newBoard = board.MakeMove(move, player); // Recurse till return currentReturn = MiniMaxMove(newBoard, 1 - player, callingPlayer, maxDepth, currentDepth + 1); // Update the best score based on which player is playing if (board.GetCurrentPlayer() == player) { if (currentReturn[0] > miniMaxReturn[0]) { miniMaxReturn.Clear(); miniMaxReturn.Add(currentReturn[0]); //miniMaxReturn[0] = currentReturn[0]; foreach(int val in move) miniMaxReturn.Add(val); } } else { if (currentReturn[0] < miniMaxReturn[0]) { miniMaxReturn.Clear(); miniMaxReturn.Add(currentReturn[0]); //miniMaxReturn[0] = currentReturn[0]; foreach (int val in move) miniMaxReturn.Add(val); } } } return miniMaxReturn; }