public Minimax(GameEngine game, int depth) { this.game = game; this.definedDepth = depth; this.current = new State(game.currentState.Grid, GameEngine.PLAYER); this.deck = new Deck(); }
public MCTS(GameEngine gameEngine) { this.gameEngine = gameEngine; this.deck = new Deck(); this.currentState = new State(BoardHelper.CloneGrid(this.gameEngine.currentState.Grid), GameEngine.PLAYER); this.random = new Random(); }
// Starts a game for the user to play private static void StartGame() { GameEngine game = new GameEngine(); bool gameOver = false; CleanConsole(); // main game loop while (!gameOver) { CleanConsole(); String nextCard = ""; if (game.nextCard == -1) nextCard = "BONUS CARD"; else nextCard = game.nextCard.ToString(); Console.WriteLine("Next card: " + nextCard); Console.WriteLine(BoardHelper.ToString(game.currentState.Grid)); DIRECTION action = GetUserInput(); PlayerMove move = new PlayerMove(action); gameOver = game.SendUserAction(move); } CleanConsole(); Console.WriteLine("Next card: "); Console.WriteLine(BoardHelper.ToString(game.currentState.Grid)); Console.WriteLine("GAME OVER! Final score: " + game.currentState.CalculateFinalScore()); Console.ReadLine(); // to avoid console closing immediately }
// Runs a number of games with the given AI agents private static void RunMultipleTests(TYPE AItype) { if (AItype == TYPE.MINIMAX || AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { int runs = GetRuns(); int depth = 0; int timeLimit = 0; if (AItype == TYPE.MINIMAX) { depth = GetDepth(); } else { timeLimit = GetChoice("Time limit?"); } StreamWriter writer = new StreamWriter(MINIMAX_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); Minimax minimax = new Minimax(game, depth); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.MINIMAX) endState = minimax.Run(false); else if (AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING) endState = minimax.RunParallelIterativeDeepening(false, timeLimit); else if (AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) endState = minimax.RunIterativeDeepening(false, timeLimit); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + depth + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } else if (AItype == TYPE.EXPECTIMAX_CLASSIC || AItype == TYPE.EXPECTIMAX_PARALLEL || AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int runs = GetRuns(); int depth = 0; int timeLimit = 0; if (AItype != TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING && AItype != TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { depth = GetDepth(); } else { timeLimit = GetChoice("Time limit?"); } StreamWriter writer = new StreamWriter(EXPECTIMAX_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); Expectimax expectimax = new Expectimax(game, depth); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.EXPECTIMAX_CLASSIC) { endState = expectimax.Run(false); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL) { endState = expectimax.RunParallelExpectimax(false); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) { endState = expectimax.RunParallelIterativeDeepeningExpectimax(false, timeLimit); } else if (AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { endState = expectimax.RunIterativeDeepening(false, timeLimit); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + depth + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } else if (AItype == TYPE.MCTS_CLASSIC || AItype == TYPE.MCTS_PARALLEL) { int runs = GetRuns(); int timeLimit = GetChoice("Time limit?"); StreamWriter writer = new StreamWriter(MCTS_LOG_FILE_NAME, true); Dictionary<int, int> highCardCount = new Dictionary<int, int>() { { 192, 0 }, { 384, 0 }, { 768, 0 }, { 1536, 0 }, { 3072, 0 }, { 6144, 0 } }; for (int i = 0; i < runs; i++) { GameEngine game = new GameEngine(); MCTS mcts = new MCTS(game); var watch = Stopwatch.StartNew(); State endState = null; if (AItype == TYPE.MCTS_CLASSIC) { endState = mcts.RunTimeLimitedMCTS(false, timeLimit); } else if (AItype == TYPE.MCTS_PARALLEL) { endState = mcts.RunParallelTimeLimitedMCTS(false, timeLimit); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; int highestTile = BoardHelper.GetHighestCard(endState.Grid); int score = game.CalculateFinalScore(); String stats = i + "\t" + highestTile + "\t" + score + "\t" + elapsedMs; Console.WriteLine(stats); writer.WriteLine(stats); List<int> keys = new List<int>(highCardCount.Keys); for (int j = 0; j < keys.Count; j++) { if (highestTile >= keys[j]) highCardCount[keys[j]]++; } } writer.Close(); Console.WriteLine(GetStatistics(highCardCount, runs)); } }
// Runs a game by given AI showing it in the console private static void RunGraphicAIGame(TYPE AItype) { if (AItype == TYPE.MINIMAX || AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { int depth = 0; int timeLimit = 0; GameEngine game = new GameEngine(); Minimax minimax = new Minimax(game, depth); if (AItype == TYPE.MINIMAX_PARALLEL_ITERATIVE_DEEPENING) { timeLimit = GetChoice("Time limit?"); minimax.RunParallelIterativeDeepening(true, timeLimit); } else if (AItype == TYPE.MINIMAX_ITERATIVE_DEEPENING) { timeLimit = GetChoice("Time limit?"); minimax.RunIterativeDeepening(true, timeLimit); } else { depth = GetDepth(); minimax.Run(true); } } else if (AItype == TYPE.EXPECTIMAX_CLASSIC || AItype == TYPE.EXPECTIMAX_PARALLEL || AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING || AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int depth = 0; if (AItype != TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) depth = GetDepth(); GameEngine game = new GameEngine(); Expectimax expectimax = new Expectimax(game, depth); if (AItype == TYPE.EXPECTIMAX_CLASSIC) { expectimax.Run(true); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL) { int timeLimit = GetChoice("Time limit?"); expectimax.RunParallelExpectimax(true); } else if (AItype == TYPE.EXPECTIMAX_PARALLEL_ITERATIVE_DEEPENING) { int timeLimit = GetChoice("Time limit?"); expectimax.RunParallelIterativeDeepeningExpectimax(true, timeLimit); } else if (AItype == TYPE.EXPECTIMAX_ITERATIVE_DEEPENING) { int timeLimit = GetChoice("Time limit?"); expectimax.RunIterativeDeepening(true, timeLimit); } } else if (AItype == TYPE.MCTS_CLASSIC || AItype == TYPE.MCTS_PARALLEL) { int timeLimit = GetChoice("Time limit?"); GameEngine game = new GameEngine(); MCTS mcts = new MCTS(game); if (AItype == TYPE.MCTS_CLASSIC) mcts.RunTimeLimitedMCTS(true, timeLimit); else if (AItype == TYPE.MCTS_PARALLEL) mcts.RunParallelTimeLimitedMCTS(true, timeLimit); } }