Example #1
0
  public void go() throws Exception {
    String input, action;
    String[] tokens;

    while (true) {
      System.out.print("TTT > ");
      input = ParserUtils.getKeyInput();
      tokens = ParserUtils.getTokens(input);
      if (tokens.length == 0) {
        continue;
      }
      action = tokens[0];

      if (action.equalsIgnoreCase("exit")) System.exit(1);
      else if (action.equalsIgnoreCase("move")) {
        byte row, col;
        try {
          col = Byte.parseByte(tokens[1]);
          row = Byte.parseByte(tokens[2]);
        } catch (Exception e) {
          System.out.println(e.getMessage());
          System.out.println("usage: move row col");
          continue;
        }
        boolean verify = game.placeMark(col, row, playerID);
        System.out.println(verify);
        if (!verify) {
          System.out.println("error moving, try again");
        } else {
          System.out.println();
          System.out.println(game.showBoard());
          winner = game.checkWinner();
          if (winner) {
            System.out.println("WINNING MOVE: game over");
            System.out.println(game.showBoard());
            game.reset();
          }
        }

      } else if (action.equalsIgnoreCase("show")) {
        System.out.println(game.showBoard());
      } else if (action.equalsIgnoreCase("restart")) {
        game.reset();
      } else {
        System.out.println("invalid command");
        continue;
      }
    }
  }