/// <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; }
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; }