示例#1
0
        public static bool CheckForWin(bool vert, bool playerPiece, BrainResult result, Board board)
        {
            int outMax = vert ? board.BoardWidth : board.BoardSize;
            int outStp = vert ? 1 : board.BoardWidth;
            int inMax = vert ? board.BoardSize : board.BoardWidth;
            int inStp = vert ? board.BoardWidth : 1;

            int size = vert ? board.BoardHeight : board.BoardWidth;

            BrainResult tempResult = new BrainResult();
            for (int i = 0; i < outMax; i += outStp)
            {
                for (int j = 0; j < inMax; j += inStp)
                {
                    if (board.IsSamePlayerPiece(playerPiece, i + j))
                    {
                        int x = vert ? j / board.BoardWidth : j;
                        int y = vert ? i : i / board.BoardWidth;
                        var move = vert ? new Move(y, x) : new Move(x, y);

                        tempResult.Moves.Add(move);
                    }
                }

                if (tempResult.Moves.Count == size)
                {
                    break;
                }
            }

            if (tempResult.Moves.Count == size)
            {
                foreach (var move in tempResult.Moves)
                {
                    result.Moves.Add(move);
                }

                return true;
            }

            return false;
        }
示例#2
0
        /// <summary>
        /// Find a place where, if the current player place his piece there, 
        /// will result in a win for that player.
        /// </summary>
        /// <param name="vert">
        /// True if we are searching vertically, otherwise False if we are 
        /// searching horizontaly.
        /// </param>
        /// <param name="playerPiece">
        /// Flag for representing the diferent players.
        /// </param>
        /// <param name="minResults">
        /// The number of wins that need to be found before returning.
        /// </param>
        /// <param name="result">
        /// The result set for storing the values.
        /// </param>
        /// <param name="board">
        /// The board to use.
        /// </param>
        /// <returns>
        /// True if at least <paramref name="minResults"/> possible wins were 
        /// found.
        /// </returns>
        public static bool GetStraightWinningMove(bool vert, bool playerPiece, int minResults, BrainResult result, Board board)
        {
            int outMax = vert ? board.BoardWidth : board.BoardSize;
            int outStp = vert ? 1 : board.BoardWidth;
            int inMax = vert ? board.BoardSize : board.BoardWidth;
            int inStp = vert ? board.BoardWidth : 1;

            int notPlayerPieceCount = 0;

            for (int i = 0; i < outMax; i += outStp)
            {
                int x = 0;
                for (int j = 0; j < inMax; j += inStp)
                {
                    if (!board.IsSamePlayerPiece(playerPiece, i + j))
                    {
                        notPlayerPieceCount++;

                        if (notPlayerPieceCount > 1)
                        {
                            notPlayerPieceCount = 0;
                            break;
                        }

                        x = vert ? j / board.BoardWidth : j;
                    }
                }

                int y = vert ? i : i / board.BoardWidth;
                var move = vert ? new Move(y, x) : new Move(x, y);
                if (notPlayerPieceCount == 1 && board.IsValidMove(move.X, move.Y))
                {
                    result.Moves.Add(move);

                    if (result.Moves.Count >= minResults)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
示例#3
0
        public static bool GetOppositeCornerMove(bool playerPiece, BrainResult result, Board board)
        {
            var corners = board.Corners.ToArray();
            for (int i = 0; i < corners.Count(); i++)
            {
                if (board.IsSamePlayerPiece(!playerPiece, corners[i]))
                {
                    var o = (i % 2 == 0) ? (1) : (-1); // either the one forward or backwards
                    var opp = corners[i + o];
                    if (board.BoardArray[board.GetIndex(opp.X, opp.Y)] == 0)
                    {
                        result.Moves.Add(opp);
                        return true;
                    }
                }
            }

            return false;
        }
示例#4
0
        public static bool GetDiagonalWinningMove(bool reverse, bool playerPiece, BrainResult result, Board board)
        {
            // setup
            int notPlayerPieceCount = 0;

            // get direction values
            int num = reverse ? board.BoardWidth - 1 : 0;
            int step = reverse ? -1 : 1;
            int x = 0, y = 0;
            // find lines
            for (int i = num; i < board.BoardSize - num; i += board.BoardWidth + step)
            {
                if (!board.IsSamePlayerPiece(playerPiece, i))
                {
                    notPlayerPieceCount++;
                    if (notPlayerPieceCount > 1)
                    {
                        notPlayerPieceCount = 0;
                        break;
                    }

                    // get the xy co-ords
                    x = reverse ? i % board.BoardWidth : i / board.BoardWidth;
                    y = i / board.BoardHeight;
                }
            }

            var move = new Move(x, y);
            if (notPlayerPieceCount == 1 && board.IsValidMove(x, y))
            {
                // we found a place
                result.Moves.Add(move);

                return true;
            }

            // no place
            return false;
        }