示例#1
0
        internal void EvaluateScan(
            ref EvaluationValue evaluationValue,
            int column,
            int row,
            int columnStep,
            int rowStep,
            Piece piece)
        {
            var targetColumn = column + (columnStep * StraightLength);

            if (targetColumn < 0)
            {
                targetColumn = -1;
            }
            else if (targetColumn > Width)
            {
                targetColumn = Width;
            }

            var targetRow = row + (rowStep * StraightLength);

            if (targetRow < 0)
            {
                targetRow = -1;
            }
            else if (targetRow > Height)
            {
                targetRow = Height;
            }

            column += columnStep;
            row    += rowStep;
            for (int c = column, r = row;
                 (c != targetColumn || r != targetRow) &&
                 (c >= 0 && c < Width && r >= 0 && r < Height);
                 c += columnStep, r += rowStep)
            {
                if (GetPiece(c, r) == piece)
                {
                    evaluationValue.Value += 1;
                    evaluationValue.Indexes.Add(c + (r * Width));
                }
                else
                {
                    return;
                }
            }
        }
示例#2
0
        internal EvaluationResult Evaluate(int column, int row)
        {
            var playerPiece      = GetPiece(column, row);
            var evaluationResult = new EvaluationResult(playerPiece);

            if (playerPiece == Piece.Empty)
            {
                return(evaluationResult);
            }

            var opponentPiece = playerPiece == Piece.X ? Piece.O : Piece.X;
            var moves         = new List <int[]>()
            {
                new int[] { 1, 0 },   // East
                new int[] { -1, 0 },  // West
                new int[] { 0, 1 },   // South
                new int[] { 0, -1 },  // North
                new int[] { 1, -1 },  // North-East
                new int[] { -1, 1 },  // South-West
                new int[] { -1, -1 }, // North-West
                new int[] { 1, 1 } // South-East
            };

            for (var i = 0; i < moves.Count; i += 2)
            {
                var evaluationValuePlayer = new EvaluationValue();
                evaluationValuePlayer.Indexes.Add(column + (row * Width));

                var evaluationValueOpponent = new EvaluationValue();
                evaluationValueOpponent.Indexes.Add(column + (row * Width));

                var move = moves[i];
                EvaluateScan(
                    ref evaluationValuePlayer,
                    column,
                    row,
                    move[0] /* columnStep */,
                    move[1] /* rowStep */,
                    playerPiece);

                EvaluateScan(
                    ref evaluationValueOpponent,
                    column,
                    row,
                    move[0] /* columnStep */,
                    move[1] /* rowStep */,
                    opponentPiece);

                move = moves[i + 1];
                EvaluateScan(
                    ref evaluationValuePlayer,
                    column,
                    row,
                    move[0] /* columnStep */,
                    move[1] /* rowStep */,
                    playerPiece);

                EvaluateScan(
                    ref evaluationValueOpponent,
                    column,
                    row,
                    move[0] /* columnStep */,
                    move[1] /* rowStep */,
                    opponentPiece);

                evaluationResult.Evaluations.Add(evaluationValuePlayer);

                evaluationValueOpponent.Value = -evaluationValueOpponent.Value;
                evaluationResult.Evaluations.Add(evaluationValueOpponent);
            }

            return(evaluationResult);
        }