/**
   * React only to MouseClicked Events (where user presses and releases).
   *
   * <p>Can't be called until after the mousePlayer has been properly initialized.
   *
   * @param e the MouseEvent created by user.
   */
  public void mouseClicked(MouseEvent e) {
    // no game set up yet.
    if (controller == null) {
      return;
    }

    // prevent any action for a game that isn't in progress.
    if (controller.getCurrentState() != GameController.IN_PROGRESS) {
      return;
    }

    // If no human player (i.e., computer vs. computer) do nothing
    if (humanPlayer == null) {
      return;
    }

    // leave if it is not OUR turn.
    if ((controller.getCurrentTurn() == GameController.XTURN)
        && (humanPlayer.getMark() != Player.XMARK)) {
      return;
    }
    if ((controller.getCurrentTurn() == GameController.OTURN)
        && (humanPlayer.getMark() != Player.OMARK)) {
      return;
    }

    int x = e.getX();
    int y = e.getY();

    // invert the calculations from drawSpot()
    Cell cell = controller.interpretXY(x, y);
    Move m = controller.interpretMove(cell, humanPlayer);

    if (m == null) {
      applet.output("Invalid move. Try again");
      return;
    }

    // once we set the move for the player, it will be retrieved
    // by the controller who then asks the player for his next
    // move. Pretty tricky, huh?
    humanPlayer.setMove(m);
    int rc = controller.playTurn();

    // refresh to ensure that move is visible again.
    applet.repaint();

    if (rc != GameController.IN_PROGRESS) {
      if (rc == GameController.DRAW) {
        applet.output("Game is drawn");
      } else {
        applet.output("Game over. You Win!");
      }
    } else {
      // update humanPlayer and deal with auto-play
      Player currentPlayer;
      int turn = controller.getCurrentTurn();
      if (turn == GameController.XTURN) {
        currentPlayer = (Player) controller.getXPlayer();
      } else {
        currentPlayer = (Player) controller.getOPlayer();
      }

      if (currentPlayer instanceof MousePlayer) {
        humanPlayer = (MousePlayer) currentPlayer;
        return;
      }

      // since the current player is NOT human, we auto play.
      rc = controller.playTurn();
      if (rc != GameController.IN_PROGRESS) {
        if (rc == GameController.DRAW) {
          applet.output("Game is drawn");
        } else {
          applet.output("Game over. You Lose!!!!!");
        }
      }
    }
  }