Exemple #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Method calculates the move the AI Player should choose.
        /// </summary>
        /// <param name="b">Game Board object</param>
        ///
        /// <returns>the move the AI chose to make</returns>
        public override int ChooseMove(Board b)
        {
            // If AI Player is Position TOP
            if (b.WhoseMove() == Position.Top)
            {
                // Try first go-again.
                for (int i = 12; i >= 7; i--)
                {
                    if (b.StonesAt(i) == 13 - i)
                    {
                        return(i);
                    }
                }
                // Otherwise, choose first available move.
                for (int i = 12; i >= 7; i--)
                {
                    if (b.StonesAt(i) > 0)
                    {
                        return(i);
                    }
                }
            }
            // If AI Player is Position  BOTTOM
            else
            {
                // Try first go-again.
                for (int i = 5; i >= 0; i--)
                {
                    if (b.StonesAt(i) == 6 - i)
                    {
                        return(i);
                    }
                }
                // Otherwise, choose first available move.
                for (int i = 5; i >= 0; i--)
                {
                    if (b.StonesAt(i) > 0)
                    {
                        return(i);
                    }
                }
            }
            // Return illegal move, if no legal moves are possible. (only if game is over)
            return(-1);
        }
Exemple #2
0
 public override int chooseMove(Board b)
 {
     if (b.WhoseMove() == Position.Top)
     {
         for (int i = 12; i >= 7; i--)                       // try first go-again
         {
             if (b.StonesAt(i) == 13 - i)
             {
                 return(i);
             }
         }
         for (int i = 12; i >= 7; i--)                   // otherwise, first
         {
             if (b.StonesAt(i) > 0)
             {
                 return(i);                              // available move
             }
         }
     }
     else
     {
         for (int i = 5; i >= 0; i--)
         {
             if (b.StonesAt(i) == 6 - i)
             {
                 return(i);
             }
         }
         for (int i = 5; i >= 0; i--)
         {
             if (b.StonesAt(i) > 0)
             {
                 return(i);
             }
         }
     }
     return(-1);                 // an illegal move if there aren't any legal ones
 }                               // this can't happen unless game is over
Exemple #3
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 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);
            }
        }
Exemple #4
0
 /*
  * Evaluate: return a number saying how much we like this board.
  * TOP is MAX, so positive scores should be better for TOP.
  * This default just counts the score so far. Override to improve!
  */
 public virtual int evaluate(Board b)
 {
     return(b.StonesAt(13) - b.StonesAt(6));
 }
Exemple #5
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR METHOD SEPARATOR
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Method to determine the optimality of the state of the game board.
        ///
        /// Note: TOP player = MAX --> most positive scores are better.
        /// Note: BOTTOM player = MIN --> most negative scores are better
        ///
        /// Override and implement heuristic evaluation algorithm to determine optimal move.
        /// (by default, just counts the score).
        /// </summary>
        ///
        /// <param name="b">Game Board object</param>
        /// <returns>how optimal the state of the game board is</returns>
        public virtual int HeuristicEvaluation(Board b)
        {
            return(b.StonesAt(13) - b.StonesAt(6));
        }
Exemple #6
0
        // this is a method that evaluates the current board using hueristics to find the best possible move
        public override int evaluate(Board board)
        {
            // declaring the evaulate hueristics
            int captures    = 0;
            int extraMoves  = 0;
            int finalState  = 0;
            int score       = 0;
            int upperStones = 0;
            int lowerStones = 0;

            if (board.WhoseMove() == Position.Top)
            {
                // score is increased or decreased by the current score (it's added both times because the score can be negative)
                if (position == Position.Top)
                {
                    score += board.StonesAt(13) - board.StonesAt(6);
                }
                else
                {
                    score += board.StonesAt(6) - board.StonesAt(13);
                }

                // extraMoves is increased or decreased by two because the player is being rewarded for both increasing their own score and getting to go again
                for (int i = 12; i >= 7; i--)
                {
                    if (board.StonesAt(i) == 13 - i)
                    {
                        if (position == Position.Top)
                        {
                            extraMoves += 2;
                        }
                        else
                        {
                            extraMoves -= 2;
                        }
                    }
                }

                // capture is increased or decreased by the number of stones that are captured if the move is executed
                for (int i = 12; i >= 7; i--)                                         // check for captures
                {
                    if (board.StonesAt((board.StonesAt(i) + i) % 14) == 0 &&          // check for an empty bowl
                        ((board.StonesAt(i) + i) % 14) <= 12 &&
                        ((board.StonesAt(i) + i) % 14) >= 7 &&                        // check that it is not landing on the other side
                        board.StonesAt(6 - (((board.StonesAt(i) + i) % 14) - 6)) > 0) // check that we aren't capturing zero
                    {
                        if (position == Position.Top)
                        {
                            captures += board.StonesAt(6 - (((board.StonesAt(i) + i) % 14) - 6));
                        }
                        else
                        {
                            captures -= board.StonesAt(6 - (((board.StonesAt(i) + i) % 14) - 6));
                        }
                    }
                }

                // finalState looks for moves that cause one side of the board to be empty and increases or decreases the score according to how many stones each player would capture
                for (int i = 12; i >= 7; i--)
                {
                    upperStones += board.StonesAt(i);
                }
                for (int i = 5; i >= 0; i--)
                {
                    lowerStones += board.StonesAt(i);
                }
                if (lowerStones == 0 && upperStones > 0)
                {
                    finalState += upperStones;
                }
                if (upperStones == 0 && lowerStones > 0)
                {
                    finalState -= lowerStones;
                }
            }
            else    // These are all the same methods with slight variation that takes into account if the Player is on the bottom
            {
                // score hueristic
                if (position == Position.Bottom)
                {
                    score += board.StonesAt(6) - board.StonesAt(13);
                }
                else
                {
                    score += board.StonesAt(13) - board.StonesAt(6);
                }

                // extraMoves hueristic
                for (int i = 5; i >= 0; i--)
                {
                    if (board.StonesAt(i) == 6 - i)
                    {
                        if (position == Position.Bottom)
                        {
                            extraMoves += 2;
                        }
                        else
                        {
                            extraMoves -= 2;
                        }
                    }
                }

                // captures hueristic
                for (int i = 5; i >= 0; i--)
                {
                    if (board.StonesAt((board.StonesAt(i) + i) % 14) == 0 &&
                        ((board.StonesAt(i) + i) % 14) >= 0 &&
                        ((board.StonesAt(i) + i) % 14) <= 5 &&
                        board.StonesAt(6 + (((board.StonesAt(i) + i) % 14) - 6)) > 0)
                    {
                        if (position == Position.Bottom)
                        {
                            captures += board.StonesAt(6 + (((board.StonesAt(i) + i) % 14) - 6));
                        }
                        else
                        {
                            captures -= board.StonesAt(6 + (((board.StonesAt(i) + i) % 14) - 6));
                        }
                    }
                }

                // finalState hueristic
                for (int i = 12; i >= 7; i--)
                {
                    upperStones += board.StonesAt(i);
                }
                for (int i = 5; i >= 0; i--)
                {
                    lowerStones += board.StonesAt(i);
                }
                if (lowerStones == 0 && upperStones > 0)
                {
                    finalState -= upperStones;
                }
                if (upperStones == 0 && lowerStones > 0)
                {
                    finalState += lowerStones;
                }
            }
            upperStones = lowerStones = 0;                      // reset the upper and lower stone values
            return(score + captures + extraMoves + finalState); // output the board quality
        }