/**-- TIC TAC TOE ------------------------------------------------------**\
 This Processing program implements a basic TIC-TAC-TOE game.
 So far, it only recognizes a winning position if
 there are 3-in-a-row in a VERTICAL column.
 Try these practice exercises:
 (1) Play a game where X wins.
 (2) Play a game which ends in a DRAW.
 (3) Play a game where O wins.
 (4) Add more "else if" conditions for other winning positions.
 (5) Find an ERROR in the program, and add code (if..)
     to prevent the error from occuring.
 (6) Repeat #5 until perfect.
\**---------------------------------------------------------------------**/


String
[][] board = {
                     {"#","#","#"},
                     {"#","#","#"},
                     {"#","#","#"}
                   };
 
void setup()
{
    showBoard();
    do
    {
      inputMove();
      showBoard();
    } while(winner() == -1);

    if (winner() == 0)
    { output("Draw"); }
    else if (winner() == 1)
    { output("X wins"); }
    else if (winner() == 2)
    { output("O wins"); }

    input("---- press [enter] ----");
}

/**-- Show Board -------------------------------------------------------**\
   Displays the board after each move.
   This could have been written with loops.
\**---------------------------------------------------------------------**/

  void showBoard()
  { output(board[0][0] + "   " + board[0][1] + "   " + board[0][2] + "\n"
         + board[1][0] + "   " + board[1][1] + "   " + board[1][2] + "\n"
         + board[2][0] + "   " + board[2][1] + "   " + board[2][2]);
  }

/**-- Input Move -------------------------------------------------------**\
   User types two numbers to input a move.     0,0 | 0,1 | 0,2
   Here are the coordinates:                  -----+-----+-----
                                               1,0 | 1,1 | 1,2
                                              -----+-----+-----
                                               2,0 | 2,1 | 2,2
\**---------------------------------------------------------------------**/

  void inputMove()
  { String player = input("Player (X or O)?");
    int row = int(input("Row?"));
    int col = int(input("Col?"));
    board[row][col] = player;
  }

/**-- Winner -----------------------------------------------------------**\
   Checks whether the game is over,
   Returns a number indicating the winner:
      -1 --> game is not over
       0 --> draw
       1 --> X wins
       2 --> O wins
\**---------------------------------------------------------------------**/

  int winner()
  { if (!board[0][0].equals("#") &&
        !board[0][1].equals("#") &&
        !board[0][2].equals("#") &&
        !board[1][0].equals("#") &&
        !board[1][1].equals("#") &&
        !board[1][2].equals("#") &&
        !board[2][0].equals("#") &&
        !board[2][1].equals("#") &&
        !board[2][2].equals("#")   )
    { return 0; }
    else if(board[0][0].equals("x") &&
            board[1][0].equals("x") &&
            board[2][0].equals("x")
        ||  board[0][1].equals("x") &&
            board[1][1].equals("x") &&
            board[2][1].equals("x")
        ||  board[0][2].equals("x") &&
            board[1][2].equals("x") &&
            board[2][2].equals("x")
           )
    { return 1; }
    else if(board[0][0].equals("o") &&
            board[1][0].equals("o") &&
            board[2][0].equals("o")
        ||  board[0][1].equals("o") &&
            board[1][1].equals("o") &&
            board[2][1].equals("o")
        ||  board[0][2].equals("o") &&
            board[1][2].equals("o") &&
            board[2][2].equals("o")
           )
    { return 2; }
    else
    { return -1; }
  }

   public void output(String message)
   {  javax.swing.JOptionPane.showMessageDialog(this,message);  }
  
   public String input(String prompt)
   {  return javax.swing.JOptionPane.showInputDialog(this,prompt);  }