class TicTacToe
{

  /* 
     Declare one integer for each sqaure of the game.
     The first square is 1 and the last 9

     s1  | s2  | s3
     ----+-----+----
     s4  | s5  | s6
     ----+-----+----
     s7  | s8  | s9

     Values for a square:
       X = 1
       O = 2
       default = 0 (square has no mark yet)
  */
 
  private int s1,
              s2,
              s3,
              s4,
              s5,
              s6,
              s7,
              s8,
              s9;

  /*
    TicTacToe constructor
    Purpose: Initialize all squares to default value
  */

  public TicTacToe() 
  {
    /* initialize all squares to default value of 0 */
    s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = 0;
  }

  /*
    Method: getVal(int square)
    Parameters: square; the number of a square
    Return value: int; the value of that square (-1 if error)
  */

  public int getVal(int square)
  {

    /* 
      note: a return statement exits the method, so no
            break statements are needed.
    */

    switch(square)
    {  
      case 1: return s1;
      case 2: return s2;
      case 3: return s3;
      case 4: return s4;
      case 5: return s5;
      case 6: return s6;
      case 7: return s7;
      case 8: return s8;
      case 9: return s9;

      // if they give an invalid square number, return an error code of -1
      default: return -1; 
   }
  }
    
  /*
    Method: setVal(int square, int value)
    Parameters: square; square number, value; value to set that square
    Return: boolean; whether the square was set correctly
  */

  public boolean setVal(int square, int value)
  {
   
    // if they give an invalid value, then return an error
    if(value < 1 || value > 2)
      return false;

    /* 
      switch requires a variable of type char, byte, short, or int as
      input.
    */

    switch(square)
    {
      /*
        check each case below to see if it is the variable
        sent to us in square. if so, check to see if the square is
        already set. if not, set it's value.

      */

      case 1: if(s1 < 1) { s1 = value; return true; } break;
      case 2: if(s2 < 1) { s2 = value; return true; } break; 
      case 3: if(s3 < 1) { s3 = value; return true; } break;
      case 4: if(s4 < 1) { s4 = value; return true; } break;
      case 5: if(s5 < 1) { s5 = value; return true; } break;
      case 6: if(s6 < 1) { s6 = value; return true; } break;
      case 7: if(s7 < 1) { s7 = value; return true; } break;
      case 8: if(s8 < 1) { s8 = value; return true; } break;
      case 9: if(s9 < 1) { s9 = value; return true; } break;
    }


    /*
      if we haven't set a value, the user has either given an
      invalid square number of the value of that square is already
      set. return false (ie. no we didn't set the square's value)
    */

    return false;
  }

  /*
   Method: resetGame()
   Parameters: none
   Return: none
   Purpose: reset the game to it's initial state
  */

  public void resetGame()
  {
    s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = 0;
  }


  /*
   Method: winner()
   Parameters: none
   Return: int; value of winner
   Purpose: 
     this function determines the winner of the tic tac toe game, if any
     and returns 1 if it's X and 2 if it's O and 0 if no winner.

   You would not be required to write this.
  */

  public int winner()
  {
    private int check = 0;

    // check if game is done, return error if games is not done
    // (can't determine winner).
    check = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
    if(!(check == 13 || check == 14))
      return -1;

    // check rows
    check = s1 + s2 + s3;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;
 
    check = s4 + s5 + s6;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;
    
    check = s7 + s8 + s9;
    if(check == 6)   
      return 2;
    else if(check == 3)
      return 1;

    // check columns

    check = s1 + s4 + s7;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;

    check = s2 + s5 + s8;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;

    check = s3 + s6 + s9;
    if(check == 6)
      return 2;   
    else if(check == 3)
      return 1;


    // check diagonals
    check = s1 + s5 + s9;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;
 
    check = s3 + s5 + s7;
    if(check == 6)
      return 2;
    else if(check == 3)
      return 1;

    // if we've made it this far, then there is no winner
    return 0;
  }

  /*
   Method: showGame()
   Parameters: none
   Return: none
   Purpose: display the state of the game

   note, don't worry about this one, you would not be expected to write
   it.
  */

  public void showGame()
  {
    System.out.println("");
    System.out.println(" " + toPiece(s1) + " | " + toPiece(s2) + " | " +
                       toPiece(s3));
    System.out.println("---+---+---");
    System.out.println(" " + toPiece(s4) + " | " + toPiece(s5) + " | " +
                       toPiece(s6));
    System.out.println("---+---+---");
    System.out.println(" " + toPiece(s7) + " | " + toPiece(s8) + " | " +
                       toPiece(s9));
    System.out.println("");
  }

  /*
   Method: toPiece
   Parameters: piece; an integer representing an X or O
   Return: String; the corresponding piece
  */

  private String toPiece(int piece)
  {
    if(piece == 1)
      return "X";
    else if(piece == 2)
      return "O";
    else
      return " ";
  }

}
