static bool ValidateLine(int x, int y, Direction direction, int step, Brick[,] board, Othello.PlayerColor currentPlayer) { //Here we want to get a complete Othello Line. Where int max = board.GetLength(0) - 1; //if outside the board return false if (x < 0 || x > max || y < 0 || y > max) { // print (direction + " Next brick is OUTSIDE!"); return false; } //if empty return false else if (IsEmpty(board[x, y])) { //print (direction + " Next brick is EMPTY!"); return false; } //if has stepped over atleast 1 of opponents bricks and now finds your own color. Returns true and validates the move as a valid move. else if (step > 1 && ((currentPlayer == Othello.PlayerColor.Black && board[x, y].brickColor == BrickColor.Black) || (currentPlayer == Othello.PlayerColor.White && board[x, y].brickColor == BrickColor.White))) { // print (direction + " Next brick makes the line VALID!"); return true; } //if first checked brick is the same color return false else if (step == 1 && ((currentPlayer == Othello.PlayerColor.Black && board[x, y].brickColor == BrickColor.Black) || (currentPlayer == Othello.PlayerColor.White && board[x, y].brickColor == BrickColor.White))) { // print (direction + " Next brick on the first step is the same color!"); return false; } else { // print (direction + " CONTINUING for next Validation"); Brick? tempBrick = NextBrickInDirection(x, y, direction, board); if (tempBrick.HasValue) { Brick brick = tempBrick.Value; var brickPos = brick.position; step += 1; return ValidateLine(brickPos.x, brickPos.y, direction, step, board, currentPlayer); } else { return false; } } }
public static Brick? NextBrickInDirection(int x, int y, Direction dir, Brick[,] board) { int max = board.GetLength(0) - 1; if ((dir == Direction.NW) && x > 0 && y < max && !IsEmpty(board[x - 1, y + 1])) { //Found something in NW return board[x - 1, y + 1]; } else if ((dir == Direction.N) && y < max && !IsEmpty(board[x, y + 1])) { //Found something in N return board[x, y + 1]; } else if ((dir == Direction.NE) && x < max && y < max && !IsEmpty(board[x + 1, y + 1])) { //Found something in NE return board[x + 1, y + 1]; } else if ((dir == Direction.W) && x > 0 && !IsEmpty(board[x - 1, y])) { //Found something in W return board[x - 1, y]; } else if ((dir == Direction.E) && x < max && !IsEmpty(board[x + 1, y])) { //Found something in E return board[x + 1, y]; } else if ((dir == Direction.SW) && x > 0 && y > 0 && !IsEmpty(board[x - 1, y - 1])) { //Found something in SW return board[x - 1, y - 1]; } else if ((dir == Direction.S) && y > 0 && !IsEmpty(board[x, y - 1])) { //Found something in S" return board[x, y - 1]; } else if ((dir == Direction.SE) && y > 0 && x < max && !IsEmpty(board[x + 1, y - 1])) { //Found something in SE" return board[x + 1, y - 1]; } return null; }