Exemplo n.º 1
0
        private IList <Move> GetValidMoves(int color, int[][] state)
        {
            var answer = new List <Move>();

            for (int i = 0; i < Board.ROWS; i++)
            {
                for (int j = 0; j < Board.COLS; j++)
                {
                    if (state[i][j] == Reversi.UNDEFINED)
                    {
                        var positions = Reversi.GetPositionsToFlip(i, j, color, state);
                        if (positions.Count > 0)
                        {
                            answer.Add(new Move {
                                Row = i, Col = j, Color = color, PositionsToFlip = positions
                            });
                        }
                    }
                }
            }
            return(answer);
        }
Exemplo n.º 2
0
        private static int Score(int[][] cells,
                                 int d,
                                 int color,
                                 int depth,
                                 bool pass                           // true means our opponent did not have any move to play; he passed
                                 )
        {
            int other = -color;

            if (d > depth)
            {
                // time to stop recursion
                int s = 0;
                for (int i = 0; i < _corners.Length; i++)
                {
                    var corner  = _corners[i];
                    var xsquare = _xsquares[i];
                    var q       = cells[corner.Row][corner.Col];
                    if (q == color)
                    {
                        s += 300;
                    }
                    else if (q == other)
                    {
                        s -= 300;
                    }
                    else
                    {
                        q = cells[xsquare.Row][xsquare.Col];
                        if (q == color)
                        {
                            s -= 50;
                        }
                        else if (q == other)
                        {
                            s += 50;
                        }
                    }
                }
                return(s);
            }
            int bestScore = int.MinValue;
            int n         = 0;     // will store the # of valid moves we can make

            for (int row = 0; row < Board.ROWS; row++)
            {
                for (int col = 0; col < Board.COLS; col++)
                {
                    var copy = Reversi.Clone(cells);
                    var move = new Move {
                        Row = row, Col = col, Color = color, PositionsToFlip = Reversi.GetPositionsToFlip(row, col, color, copy)
                    };
                    if (Reversi.TryPlay(copy, move))
                    {
                        n++;
                        // find the score our opponent can make.
                        int ss = -Score(copy, d + 1, other, depth, false);
                        if (ss > bestScore)
                        {
                            bestScore = ss;
                        }
                    }
                }
            }
            if (n == 0)
            {
                // there is no valid move that we can play. we can't set bestPos to any meaningful value.
                if (pass)
                {
                    // opponent also had no move to play
                    // this means game will end
                    for (int i = 0; i < Board.ROWS; i++)
                    {
                        for (int j = 0; j < Board.COLS; j++)
                        {
                            if (cells[i][j] == color)
                            {
                                n++;
                            }
                            else if (cells[i][j] == other)
                            {
                                n--;
                            }
                        }
                    }
                    if (n > 0)
                    {
                        // we will win.
                        return(n + 8000);
                    }
                    else
                    {
                        // we will lose.
                        return(n - 8000);
                    }
                }
                else
                {
                    bestScore = -Score(cells, d + 1, other, depth, true);
                }
            }
            if (d >= depth - 1)
            {
                return(bestScore + (n << 3));
            }
            else
            {
                return(bestScore);
            }
        }