//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Test to see that heuristic evaluation function In jj47Player works as intended. /// </summary> /// <param name="debugHeuristicEvaluationFunctionTOP">enable or disable for TOP player</param> /// <param name="debugHeuristicEvaluationFunctionBOTTOM">enable or disable for BOTTOM player</param> private static void DebugHeuristicEvaluationFunction(bool debugHeuristicEvaluationFunctionTOP, bool debugHeuristicEvaluationFunctionBOTTOM) { // Test for TOP player. if (debugHeuristicEvaluationFunctionTOP == true) { // Create new game board. Board testBoardHeuristicTop = new Board(Position.Top); testBoardHeuristicTop.Display(); Console.WriteLine("\n\n"); // Debug - test setter for number of stones at specified position. testBoardHeuristicTop.SetStonesAt(2, 0); testBoardHeuristicTop.Display(); Console.WriteLine("\n\n"); if (testBoardHeuristicTop.StonesAt(2) != 0) { Console.WriteLine("Method setStonesAt(position) not functioning properly"); } // Store the board value the AI determined. int moveTopValue = -1; //Test as TOP Player. jj47Player testTopPlayer = new jj47Player(Position.Top, 100000); moveTopValue = testTopPlayer.HeuristicEvaluation(testBoardHeuristicTop); Console.WriteLine("TOP Player AI determined optimal board value was: {0}", moveTopValue); } // Test for BOTTOM player. if (debugHeuristicEvaluationFunctionBOTTOM == true) { // Create new game board. Board testBoardHeuristicBottom = new Board(Position.Bottom); testBoardHeuristicBottom.Display(); Console.WriteLine("\n\n"); // Debug - test setter for number of stones at specified position. testBoardHeuristicBottom.SetStonesAt(10, 0); testBoardHeuristicBottom.Display(); Console.WriteLine("\n\n"); if (testBoardHeuristicBottom.StonesAt(10) != 0) { Console.WriteLine("Method setStonesAt(position) not functioning properly"); } // Store the board value the AI determined. int moveBottomValue = -1; // Test as BOTTOM Player. jj47Player testBottomPlayer = new jj47Player(Position.Bottom, 100000); moveBottomValue = testBottomPlayer.HeuristicEvaluation(testBoardHeuristicBottom); Console.WriteLine("BOTTOM Player AI determined optimal board value was: {0}", moveBottomValue); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Play a Mankalah game with the two given players, with the firstPlayer starting. /// Returns TOP's score. /// </summary> /// <param name="playerTop">the player on the top</param> /// <param name="playerBottom">the player on the bottom</param> /// <param name="firstPlayer">the player who is starting the game</param> /// <returns>the score of the TOP player</returns> public static int PlayGame(Player playerTop, Player playerBottom, Position firstPlayer) { // Create a new game board. _board = new Board(firstPlayer); // Determine the player who starts. if (firstPlayer == Position.Top) { Console.WriteLine("Player " + playerTop.GetName() + " starts."); } else { Console.WriteLine("Player " + playerBottom.GetName() + " starts."); } // Display the current state of the game board. _board.Display(); // Continue rotating turns till the game is over. while (!_board.GameOver()) { Console.WriteLine(); // Get the player's move and output what move the player made. if (_board.WhoseMove() == Position.Top) { _move = playerTop.ChooseMove(_board); Console.WriteLine(playerTop.GetName() + " chooses move " + _move); } else { _move = playerBottom.ChooseMove(_board); Console.WriteLine(playerBottom.GetName() + " chooses move " + _move); } // Commit the move to the game state. (true = verbose, false = non-verbose) _board.MakeMove(_move, true); // Display the new state of the game board. _board.Display(); // Game is over, determine final results. if (_board.GameOver()) { if (_board.Winner() == Position.Top) { Console.WriteLine("Player " + playerTop.GetName() + " (TOP) wins " + _board.ScoreTopPlayer() + " to " + _board.ScoreBottomPlayer()); } else if (_board.Winner() == Position.Bottom) { Console.WriteLine("Player " + playerBottom.GetName() + " (BOTTOM) wins " + _board.ScoreBottomPlayer() + " to " + _board.ScoreTopPlayer()); } else { Console.WriteLine("A tie!"); } } // Game is not over, ask player to make their move. else if (_board.WhoseMove() == Position.Top) { Console.WriteLine(playerTop.GetName() + " to move."); } else { Console.WriteLine(playerBottom.GetName() + " to move."); } } // Return the final score for the match. return(_board.ScoreTopPlayer()); }
/* * Play one Kalah game with the two given players, with firstPlayer * starting. This function returns TOP's score. */ public static int playGame(Player pTop, Player pBot, Position firstPlayer) { b = new Board(firstPlayer); if (firstPlayer == Position.Top) { Console.WriteLine("Player " + pTop.getName() + " starts."); } else { Console.WriteLine("Player " + pBot.getName() + " starts."); } b.Display(); while (!b.GameOver()) { Console.WriteLine(); if (b.WhoseMove() == Position.Top) { move = pTop.chooseMove(b); Console.WriteLine(pTop.getName() + " chooses move " + move); } else { move = pBot.chooseMove(b); Console.WriteLine(pBot.getName() + " chooses move " + move); } b.MakeMove(move, true); // last parameter says to be chatty b.Display(); if (b.GameOver()) { if (b.Winner() == Position.Top) { Console.WriteLine("Player " + pTop.getName() + " (TOP) wins " + b.ScoreTop() + " to " + b.ScoreBot()); } else if (b.Winner() == Position.Bottom) { Console.WriteLine("Player " + pBot.getName() + " (BOTTOM) wins " + b.ScoreBot() + " to " + b.ScoreTop()); } else { Console.WriteLine("A tie!"); } } else if (b.WhoseMove() == Position.Top) { Console.WriteLine(pTop.getName() + " to move."); } else { Console.WriteLine(pBot.getName() + " to move."); } } return(b.ScoreTop()); }