import chapman.io.*;

// Allow the user to play Reverse It.
public class ReverseIt {
  final static char UNFILLED = '.';
  final static char WHITE = 'O';
  final static char BLACK = '#';
  final static int BOARD_SIZE = 8;
  static char[][] board;    // current move
  static char current_side; // the side which is going to make a move
  static StdIn in = new StdIn();

  // Main game logic
  public static void main(String[] args) {
    createBoard();
    current_side = WHITE;
    while (!endGame()) {
      allowMove();
      if (current_side == WHITE) {
        current_side = BLACK;
      } else {
        current_side = WHITE;
      }
    }
    printWinner();
  }

  // Check whether game is ended
  public static boolean endGame() {
    System.out.println("Should check for end game.");
    return false;
  }

  // Allow the user to choose a move
  public static void allowMove() {
    // Missing: should check for no move
    for (;;) {
      printBoard();
      System.out.println("Next move by " + current_side);
      System.out.print("  Y? ");
      int y = in.readInt();
      System.out.print("  X? ");
      int x = in.readInt();
      if (makeMove(y-1, x-1))
        break;
      System.out.println("Invalid move.");
    }
    System.out.println("Should allow a move by " + current_side);
  }
  
  // Actually trys to make a move at (y, x)
  public static boolean makeMove(int y, int x) {
    if (offBoard(y, x) || board[y][x] != UNFILLED)
      return false;
    // Try flipping at all directions.
    boolean succeed = false;
    succeed = succeed | tryFlip(y, x, -1, -1);
    succeed = succeed | tryFlip(y, x, -1,  0);
    succeed = succeed | tryFlip(y, x, -1,  1);
    succeed = succeed | tryFlip(y, x,  0, -1);
    succeed = succeed | tryFlip(y, x,  0,  1);
    succeed = succeed | tryFlip(y, x,  1, -1);
    succeed = succeed | tryFlip(y, x,  1,  0);
    succeed = succeed | tryFlip(y, x,  1,  1);
    // If any succeed, procced to place our own piece
    if (!succeed)
      return false;
    board[y][x] = current_side;
    return true;
  }

  // Check whether a position is off-board
  public static boolean offBoard(int y, int x) {
    if (y<0 || y>=BOARD_SIZE)
      return true;
    if (x<0 || x>=BOARD_SIZE)
      return true;
    return false;
  }

  // try to flip at (y, x) towards (dy, dx)
  public static boolean tryFlip(int y, int x, int dy, int dx) {
    if (!checkFlip(y, x, dy, dx, current_side))
      return false;
    // Flip is known to succeed, just flip until we see a piece of current_side
    y += dy;
    x += dx;
    while (board[y][x] != current_side) {
      board[y][x] = current_side;
      y += dy;
      x += dx;
    }
    return true;
  }

  // Check whether it is possible to flip any piece to side
  // by a move at (y, x) towards (dy, dx)
  // Algorithm: try to search a piece of side before getting off the board
  //   or seeing an empty cell.  If succeed, we return whether we had seen
  //   a cell of the other side during the search.
  public static boolean checkFlip(int y, int x, int dy, int dx, char side) {
    boolean found_other = false;
    for (;;) {
      y += dy;
      x += dx;
      if (offBoard(y, x) || board[y][x] == UNFILLED)
        return false;
      if (board[y][x] == side)
        break;
      found_other = true;
    }
    return found_other;
  }

  // Check which side is the winner
  public static void printWinner() {
    System.out.println("Should print winner.");
  }
  
  // Make a reverse-it board
  public static void createBoard() {
    board = new char[BOARD_SIZE][BOARD_SIZE];
    for (int i = 0; i < BOARD_SIZE; ++i)
      for (int j = 0; j < BOARD_SIZE; ++j)
        board[i][j] = UNFILLED;
    board[3][4] = board[4][3] = WHITE;
    board[3][3] = board[4][4] = BLACK;
  }
  
  // Print the board
  public static void printBoard() {
    System.out.print("  ");
    for (int i=0; i<BOARD_SIZE; ++i) {
      System.out.print(i+1+" ");
    }
    System.out.println();
    for (int i=0; i<BOARD_SIZE; ++i) {
      System.out.print(i+1+" ");
      for (int j=0; j<BOARD_SIZE; ++j)
        System.out.print(board[i][j] + " ");
      System.out.println();
    }
  }
}

