public bool ContainsMove(Board board, int i) { return this.Moves.Any(x => board.GetIndex(x.X, x.Y) == i); }
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; }
/// <summary> /// Fork by creating an opportunity where you can win in two ways. /// </summary> /// <param name="playerPiece"> /// Flag for representing the diferent players. /// </param> /// <param name="result"> /// The result set for storing the values. /// </param> /// <param name="board"> /// The board to use. /// </param> /// <param name="getAllForks"> /// Are we to get all the forks or stop after the first one is found. /// </param> /// <returns> /// True if at least 1 possible forking move was found. /// </returns> public static bool GetForkingMove(bool playerPiece, BrainResult result, Board board, bool getAllForks = false) { var tempBoard = board.Clone(); for (int i = 0; i < board.BoardSize; i++) { if (tempBoard.BoardArray[i] == 0) { var piece = playerPiece ? 1 : -1; tempBoard.BoardArray[i] = piece; var outerResult = new BrainResult(); if (GetWinningMove(playerPiece, 2, outerResult, tempBoard)) { var m = board.GetMove(i); var innerResult = new BrainResult(); var notWin = !GetWinningMove(!playerPiece, 1, innerResult, tempBoard); // switch last to the other player var index = board.GetIndex(m.X, m.Y); tempBoard.BoardArray[index] = piece * -1; var notFork = !GetOppositionForkingMove(playerPiece, tempBoard); if (notWin && notFork) { result.Moves.Add(m); if (!getAllForks) { return true; } } // switch back tempBoard.BoardArray[index] = piece; } tempBoard.BoardArray[i] = 0; } } return getAllForks && result.Moves.Count > 0; }
public static bool GetEmptySideMove(BrainResult result, Board board) { for (int i = 0; i < board.BoardSize;i++) { // is empty and is not corner if (board.BoardArray[i] == 0 && board.Corners.All(c => board.GetIndex(c) != i)) { result.Moves.Add(board.GetMove(i)); return true; } } return false; }
public static bool GetEmptyCornerMove(BrainResult result, Board board) { var move = board.Corners.FirstOrDefault(m => board.BoardArray[board.GetIndex(m)] == 0); if (move != null) { result.Moves.Add(move); } return move != null; }
/// <summary> /// Play the center. /// </summary> /// <remarks> /// If it is the first move of the game, playing on a corner gives the /// other player more opportunities to make a mistake and may therefore /// be the better choice. /// However, it makes no difference between perfect players. /// </remarks> public static bool GetCentreMove(BrainResult result, Board board) { // only if the board has odd dimensions if (board.BoardWidth % 2 != 0 && board.BoardHeight % 2 != 0) { var x = board.BoardWidth / 2; var y = board.BoardHeight / 2; var i = board.GetIndex(x, y); // and if the place is empty if (board.BoardArray[i] == 0) { result.Moves.Add(board.GetMove(i)); return true; } } return false; }