Beispiel #1
0
        public int ComputerMove(int player, int level)
        {
            int bestCol = -1;
            int best    = -30000;

            // Simulate a drop in each of the columns
            for (int currentCol = 0; currentCol < 7; currentCol++)
            {
                C4Engine tempState = Copy();

                // If column is full, try a different column
                if (tempState.DropPiece(player, currentCol) < 0)
                {
                    continue;
                }

                // If this drop wins the game, then go here
                if (tempState.IsWinner() == player)
                {
                    bestCol = currentCol;
                    break;
                }

                // Otherwise, evaluate this move
                var goodness = tempState.Evaluate(player, level, 1);

                // If this move looks better than previous moves, remember it
                if (goodness > best)
                {
                    best    = goodness;
                    bestCol = currentCol;
                }

                //If the move is equally good, make a random decision
                if (goodness == best)
                {
                    if (Rand() % 2 == 0)
                    {
                        bestCol = currentCol;
                    }
                }
            }

            // Drop the piece in the best column
            if (bestCol >= 0)
            {
                var row = DropPiece(player, bestCol);
                if (row >= 0)
                {
                    return(bestCol);
                }
            }
            return(-1);
        }
Beispiel #2
0
        int Evaluate(int player, int level, int depth)
        {
            int best = -30000;

            if (depth <= level)
            {
                for (int currentCol = 0; currentCol < 7; currentCol++)
                {
                    C4Engine tempState = Copy();

                    if (tempState.DropPiece(OtherPlayer(player), currentCol) < 0)
                    {
                        continue;
                    }

                    int goodness;
                    if (tempState.IsWinner() == OtherPlayer(player))
                    {
                        goodness = 25000 - depth;
                    }
                    else
                    {
                        goodness = tempState.Evaluate(OtherPlayer(player), level, depth + 1);
                    }

                    if (goodness > best)
                    {
                        best = goodness;
                    }
                }

                // What's good for the other player is bad for this one
                return(-best);
            }
            return(CalcScore(player) - CalcScore(OtherPlayer(player)));
        }