/*Evaluate function used when the end game is not clear
         * Uses 5 heuristics found by researchers at the University of Kansas to be the best
         * Heuristic research found at https://fiasco.ittc.ku.edu/publications/documents/Gifford_ITTC-FY2009-TR-03050-03.pdf
         * h1 is my score - opponent's score
         * h2 is how close I am to winning
         * h3 is how close the opponent is to winning
         * h4 is the number of stones close to my home
         * h5 is the number of stones far from my home (on my side of the board)
         */
        public override int evaluate(Board b)
        {
            int h1, h2, h3, h4, h5, sum;

            if (us == Position.Top)
            {
                h2 = b.scoreTop();
                //h3 = b.scoreBot();
                h1 = h2 - b.scoreBot();
                //h4 = b.stonesAt(11) + b.stonesAt(12);
                //h5 = b.stonesAt(7) + b.stonesAt(8);
            }
            else
            {
                h2 = b.scoreBot();
                //h3 = b.scoreTop();
                h1 = h2 - b.scoreTop();
                //h4 = b.stonesAt(4) + b.stonesAt(5);
                //h5 = b.stonesAt(0) + b.stonesAt(1);
            }
            sum = h1 + h2;  // - h3 - h4 + h5;
            return(sum);

            //int score;
            //if (us == Position.Top)
            //    score = b.scoreTop() - b.scoreBot();
            //else
            //    score = b.scoreBot() - b.scoreTop();
            //return score;
        }
 public int endGameEval(Board b)
 {
     if (us == Position.Top)
     {
         return((b.scoreTop() - b.scoreBot()) * 1000);
     }
     else
     {
         return((b.scoreBot() - b.scoreTop()) * 1000);
     }
 }
Exemple #3
0
        /*Evaluate function used when the end game is not clear
         * Uses 5 heuristics found by researchers at the University of Kansas to be the best
         * Heuristic research found at https://fiasco.ittc.ku.edu/publications/documents/Gifford_ITTC-FY2009-TR-03050-03.pdf
         * h1 is my score - opponent's score
         * h2 is how close I am to winning
         * h3 is how close the opponent is to winning
         * h4 is the number of stones close to my home
         * h5 is the number of stones far from my home (on my side of the board)
         */
        public override int evaluate(Board b)
        {
            int h1, h2, h3, h4, h5, h6, target, sum;

            h6 = 0;
            if (us == Position.Top)
            {
                h2 = b.stonesAt(13);
                h3 = b.stonesAt(6);
                h1 = h2 - b.scoreBot();
                h4 = b.stonesAt(11) + b.stonesAt(12);
                h5 = b.stonesAt(7) + b.stonesAt(8);
                for (int i = 7; i < 13; i++)
                {
                    target = i + b.stonesAt(i);
                    if (target == 13)    //calculating go-agains
                    {
                        //if (b.stonesAt(target) == 0 && b.stonesAt(12 - target) != 0)
                        //    h6 += b.stonesAt(12 - target);
                        h6 = 100;
                    }
                }
            }
            else
            {
                h2 = b.stonesAt(6);
                h3 = b.stonesAt(13);
                h1 = h2 - b.scoreTop();
                h4 = b.stonesAt(4) + b.stonesAt(5);
                h5 = b.stonesAt(0) + b.stonesAt(1);
                for (int i = 0; i < 6; i++)
                {
                    target = i + b.stonesAt(i);
                    if (target == 6)    //calculating go-agains
                    {
                        //if (b.stonesAt(target) == 0 && b.stonesAt(12 - target) != 0)
                        //    h6 += b.stonesAt(12 - target);
                        h6 = 100;
                    }
                }
            }
            sum = (h1 + 10) + (h2 + 8) - (h3 + 8) - (h4 + 4) + (h5 + 4) + (h6 + 0);
            sum = (us == Position.Top) ? sum : -1 * sum;
            if (h2 > 24)    //If we have at least half of the stones
            {
                sum += 500;
            }
            else if (h3 > 24)   //If opponent has at least half of the stones
            {
                sum -= 500;
            }
            return(sum);

            //int score;
            //if (us == Position.Top)
            //    score = b.scoreTop() - b.scoreBot();
            //else
            //    score = b.scoreBot() - b.scoreTop();
            //return score;
        }
        /*Evaluate function used when the end game is not clear
         * Uses 5 heuristics found by researchers at the University of Kansas to be the best
         * Heuristic research found at https://fiasco.ittc.ku.edu/publications/documents/Gifford_ITTC-FY2009-TR-03050-03.pdf
         * h1 is my score - opponent's score
         * h2 is how close I am to winning
         * h3 is how close the opponent is to winning
         * h4 is the number of stones close to my home
         * h5 is the number of stones far from my home (on my side of the board)
         */
        public override int evaluate(Board b)
        {
            int h1, h2, h3, h4, h5, sum;

            if (us == Position.Top)
            {
                h2 = b.stonesAt(13);
                h3 = b.stonesAt(6);
                h1 = h2 - b.scoreBot();
                h4 = b.stonesAt(11) + b.stonesAt(12);
                h5 = b.stonesAt(7) + b.stonesAt(8);
            }
            else
            {
                h2 = b.stonesAt(6);
                h3 = b.stonesAt(13);
                h1 = h2 - b.scoreTop();
                h4 = b.stonesAt(4) + b.stonesAt(5);
                h5 = b.stonesAt(0) + b.stonesAt(1);
            }
            sum = (h1 + 10) + (h2 + 8) - (h3 + 8) - (h4 + 4) + (h5 + 4);
            sum = (us == Position.Top) ? sum : -1 * sum;
            if (h2 > 24)    //If we have at least half of the stones
            {
                sum += 500;
            }
            else if (h3 > 24)   //If opponent has at least half of the stones
            {
                sum -= 500;
            }
            return(sum);

            //int score;
            //if (us == Position.Top)
            //    score = b.scoreTop() - b.scoreBot();
            //else
            //    score = b.scoreBot() - b.scoreTop();
            //return score;
        }
Exemple #5
0
        /*
         * Evaluate: return a number saying how much we like this board.
         * h1 = the difference in score
         * h2 = stones in my home
         * h3 = stones in opponent's home
         * h4 = whether or not to go again
         * h5 = whether or not to capture opponent's pieces
         */
        public override int evaluate(Board b)
        {
            int h1;
            int h2;
            int h3;
            int h4 = 0;
            int h5 = 0;
            int target;
            int captureTarget;
            int score;

            if (me == Position.Top)
            {
                h2 = b.stonesAt(13);    // stones in my home
                h3 = b.stonesAt(6);     // stones in opponent's home
                h1 = h2 - b.scoreBot(); // my margin of winning/losing
                for (int i = 7; i < 13; i++)
                {
                    target        = (i + b.stonesAt(i)) % 12;
                    captureTarget = ((target - 12) + (2 * (12 - target))); // lookup the spot on the opposite of the board for a potential capture
                    if (target == 13 && b.whoseMove() == me)               // if the move will end in my home -- a go-again
                    {
                        h4 = 1;
                    }
                    else if (b.stonesAt(target) == 0 && target > 6 && b.whoseMove() == me) // if it is possible to get a capture
                    {
                        if (b.stonesAt(captureTarget) > 2)                                 // see if capture is worth it
                        {
                            h5 = 50;
                        }
                    }
                }
            }
            else
            { // if I'm on the bottom, do the same thing but changed for the bottom half of the board
                h2 = b.stonesAt(6);
                h3 = b.stonesAt(13);
                h1 = h2 - b.scoreTop();
                for (int i = 0; i < 6; i++)
                {
                    target        = (i + b.stonesAt(i)) % 12;
                    captureTarget = ((target + 12) - (2 * target));
                    if (target == 6 && b.whoseMove() == me)
                    {
                        h4 = 1;
                    }
                    else if (b.stonesAt(target) == 0 && target < 6 && b.whoseMove() == me) // if it is possible to get a capture
                    {
                        if (b.stonesAt(captureTarget) > 2)                                 // see if capture is worth it
                        {
                            h5 = 50;
                        }
                    }
                }
            }
            score = h1 + h2 + h5 + h4;
            score = (me == Position.Top) ? score : -1 * score;
            if (h2 > 24) // if we've won -- really want this
            {
                score += 500;
            }
            else if (h3 > 24) // if opponent has won -- really don't want this
            {
                score -= 500;
            }
            return(score);
        }
Exemple #6
0
        /*
         * 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());
        }