//--------------------------------------------------------------
//| Stratagem.java
//| Skeleton for Stratagem game
//| Author: M.B. Feldman, The George Washington University
//| Last Modified: January 11, 2004
//--------------------------------------------------------------
import cs1.Keyboard;
import csci53.Screen;
import java.util.Random;
public class Stratagem
{
  static final int SIZE = 4;           // could configure a
                                       // larger board
  static Random generator = new Random();

  // simple example uses a global variable for the board
  // a SIZE x SIZE 2-d array represents the game board
  // -1 means the cell is occupied at initialization
  //  0 means the cell is unoccupied (the piece was removed
  //    or jumped from this cell)
  //  k means a piece jumped into the cell on move k

  static int[][] board = new int[SIZE][SIZE];

  //-----------------------------------------------------------
  // void method sets up a game by first initializing all array
  // cells to -1, then choosing one cell at random and removing
  // that piece
  //-----------------------------------------------------------
  static void setUpGame()
  {
    for (int row = 0; row <= SIZE-1; row++)
    {
      for (int column = 0; column <= SIZE-1; column++)
      {
        board[row][column] = -1;
      }
    }

    // pick two random integers in the row and column range
    // and set that cell as indicated
    board[generator.nextInt(SIZE)][generator.nextInt(SIZE)] = 0;
  }

  //-----------------------------------------------------------
  // void method uses Screen commands to display the board in the
  // center of the screen.
  //-----------------------------------------------------------
  static void displayBoard()
  {
    int screenRow = 5;   // starting row

    // top caption
    Screen.moveCursor(screenRow, 35);
    System.out.print("       ");
    for (int column = 0; column <= 3; column++)
    {
      System.out.print(column + "   ");
    }
    screenRow += 2;

    // paint rows from top to bottom of screen
    for (int row = 0; row <= 3; row++)
    {
      // side caption
      Screen.moveCursor(screenRow, 35);
      System.out.print("  " + row + "   ");

      // display all columns for one row
      for (int column = 0; column <= 3; column++)
      {
        switch (board[row][column])
        {
          case -1:
            System.out.print("**  ");
            break;
          case  0:
            System.out.print("--  ");
            break;
          default:
            if (board[row][column] <= 9)  // 1 digit
            {
              System.out.print(" ");     // need leading blank
            }
            System.out.print(board[row][column] + "  ");
        }
      }
      screenRow += 2;                  // use alternate lines
    }

  } // end of displayBoard

  // skeletal main method, just to demonstrate

  public static void main (String[] args)
  {
    // declare variables

    int fromRow, fromColumn;           // input -- starting position
    int jumpedRow, jumpedColumn;       // input -- jumped position

    int moveNumber;                    // program variable -- counts moves
    int toRow, toColumn;               // program variables - ending cell
    
    Screen.clearScreen();
    setUpGame();
    displayBoard();

    moveNumber = 1;

    // now read a single move; assume it's valid, and execute it

    Screen.moveCursor(16, 25);
    System.out.print("Move number " + moveNumber + ":");
    Screen.moveCursor(17, 25);
    System.out.print("From position (row first, then column) >");
    fromRow    = Keyboard.readInt();
    fromColumn = Keyboard.readInt();
    Screen.moveCursor(18, 25);
    System.out.print("Over position (row first, then column) >");
    jumpedRow    = Keyboard.readInt();
    jumpedColumn = Keyboard.readInt();

    // since we're not validating in this simple example, do the move
    // first compute destination cell
    if (fromRow == jumpedRow)           // horizontal move
    {
      toRow = fromRow;
      if (fromColumn < jumpedColumn)    // left to right
      {
        toColumn = fromColumn + 2;
      }
      else                              // right to left
      {
        toColumn = fromColumn - 2;
      }
    }
    else                                // vertical move
    {
      toColumn = fromColumn;
      if (fromRow < jumpedRow)          // downward
      {
        toRow = fromRow + 2;
      }
      else                              // upward
      {
        toRow = fromRow -2;
      }
    }

    // update cells in board array
    board[toRow][toColumn] = moveNumber;       // end position
    board[fromRow][fromColumn] = 0;            // start position
    board[jumpedRow][jumpedColumn] = 0;        // jumped position

    displayBoard();
    moveNumber++;
    
    Screen.moveCursor(22,1);
  }
}