示例#1
0
        public IGameState MakeMove(IGameMove gameMove, IGameState gameState)
        {
            TicTacToeMove move = (TicTacToeMove)gameMove;

            TicTacToeState state = (TicTacToeState)gameState;

            if (move.X < 0 || move.X > 2)
            {
                throw new ArgumentOutOfRangeException("X");
            }

            if (move.Y < 0 || move.Y > 2)
            {
                throw new ArgumentOutOfRangeException("Y");
            }

            if (move.Symbol != TicTacToeFieldState.Circle && move.Symbol != TicTacToeFieldState.Cross)
            {
                throw new ArgumentOutOfRangeException("Symbol");
            }

            if (state.Fields[move.X][move.Y] != TicTacToeFieldState.Empty)
            {
                throw new InvalidOperationException(String.Format("Field is not empty {0} {1} {2}", move.X, move.Y, state.Fields[move.X][move.Y]));
            }

            TicTacToeState newState = (TicTacToeState)state.Clone();

            newState.Fields[move.X][move.Y] = move.Symbol;

            return(newState);
        }
示例#2
0
        public Int32 Evaluate(IGameState gameState, IGameMove gameMove, IGameState newGameState)
        {
            TicTacToeState state = (TicTacToeState)newGameState;

            TicTacToeMove move = (TicTacToeMove)gameMove;

            Int32 sign = GetSign(move.Symbol);

            #region horizontal lines

            Int32 verticalCounter = 0;

            Int32 verticalOponentCounter = 0;

            for (Int32 q = 0; q < 3; q++)
            {
                TicTacToeFieldState field = state.Fields[q][move.Y];

                if (field == move.Symbol)
                {
                    verticalCounter++;
                }
                else if (field != TicTacToeFieldState.Empty)
                {
                    verticalOponentCounter++;
                }
            }

            if (verticalCounter >= 3)
            {
                return(WinValue * sign);
            }

            if (verticalOponentCounter >= 3)
            {
                return(WinValue * (-sign));
            }

            #endregion horizontal lines

            #region vertical lines

            Int32 horizontalCounter = 0;

            Int32 horizontalOponentCounter = 0;

            for (Int32 q = 0; q < 3; q++)
            {
                TicTacToeFieldState field = state.Fields[move.X][q];

                if (field == move.Symbol)
                {
                    horizontalCounter++;
                }
                else if (field != TicTacToeFieldState.Empty)
                {
                    horizontalOponentCounter++;
                }
            }

            if (horizontalCounter >= 3)
            {
                return(WinValue * sign);
            }

            if (horizontalOponentCounter >= 3)
            {
                return(WinValue * -sign);
            }

            #endregion vertical lines

            #region diagonal lines

            Int32 diagonalCounterA = 0;

            Int32 oponentDiagonalCounterA = 0;

            if (move.X == move.Y)             //// check diagonal A
            {
                Coords[] diagonal = Lines[0];

                for (Int32 q = 0; q < 3; q++)
                {
                    Coords coords = diagonal[q];

                    TicTacToeFieldState field = state.Fields[coords.X][coords.Y];

                    if (field == move.Symbol)
                    {
                        diagonalCounterA++;
                    }
                    else if (field != TicTacToeFieldState.Empty)
                    {
                        oponentDiagonalCounterA++;
                    }
                }
            }

            if (diagonalCounterA >= 3)
            {
                return(WinValue * sign);
            }

            if (oponentDiagonalCounterA >= 3)
            {
                return(WinValue * -sign);
            }

            Int32 diagonalCounterB = 0;

            Int32 oponentDiagonalCounterB = 0;

            if (move.X + move.Y == 2)             //// check diagonal B
            {
                Coords[] diagonal = Lines[1];

                for (Int32 q = 0; q < 3; q++)
                {
                    Coords coords = diagonal[q];

                    TicTacToeFieldState field = state.Fields[coords.X][coords.Y];

                    if (field == move.Symbol)
                    {
                        diagonalCounterB++;
                    }
                    else if (field != TicTacToeFieldState.Empty)
                    {
                        oponentDiagonalCounterB++;
                    }
                }
            }

            if (diagonalCounterB >= 3)
            {
                return(WinValue * sign);
            }

            if (oponentDiagonalCounterB >= 3)
            {
                return(WinValue * -sign);
            }

            #endregion diagonal lines

            if (horizontalOponentCounter == 2 && horizontalCounter == 1)
            {
                return(DefendingBonus * sign);;
            }

            if (verticalOponentCounter == 2 && verticalCounter == 1)
            {
                return(DefendingBonus * sign);
            }

            if (oponentDiagonalCounterB == 2 && diagonalCounterB == 1)
            {
                return(DefendingBonus * sign);
            }

            if (oponentDiagonalCounterA == 2 && diagonalCounterA == 1)
            {
                return(DefendingBonus * sign);
            }

            Int32 linesWithTwo = 0;

            if (horizontalCounter == 2 && horizontalOponentCounter == 0)
            {
                linesWithTwo++;
            }

            if (verticalCounter == 2 && verticalOponentCounter == 0)
            {
                linesWithTwo++;
            }

            if (diagonalCounterB == 2 && oponentDiagonalCounterB == 0)
            {
                linesWithTwo++;
            }

            if (diagonalCounterA == 2 && oponentDiagonalCounterA == 0)
            {
                linesWithTwo++;
            }

            Int32 resultValue = 0;

            if (linesWithTwo > 0)
            {
                resultValue = linesWithTwo * LineWithTwoBonus * sign;
            }

            if (move.X == 1 && move.Y == 1)
            {
                resultValue = MiddleFieldBonus * sign;
            }

            return(resultValue);
        }