Example #1
0
  /**
   * Parse incoming message.
   *
   * @param bytes byte array of received message
   * @param num number of bytes received
   */
  public void parse(byte[] bytes, int num) {
    String msg = new String(bytes);
    if (num > 0) {
      if (msg.charAt(0) == 'X' && state == GameState.Running) {
        Log.d("**TRON**", "receiving coords...");
        Scanner scanner = new Scanner(msg);
        int x = scanner.useDelimiter("[^\\d]+").nextInt();
        int y = scanner.useDelimiter("[^\\d]+").nextInt();
        grid.setOpponent(x, y);
        Log.d("**TRON**", "received coords: " + x + " " + y);
      }
      if (msg.startsWith("LOST")) {
        player.scored();
        if (player.getScore() >= goal) {
          ((TronGame) game).saveWin();
          state = GameState.Winner;
        } else {
          state = GameState.Win;
        }
      }

      if (msg.startsWith("WIN")) {
        if (state == GameState.Running) {
          connection.write("LOST".getBytes());
          opponent.scored();
          if (opponent.getScore() == goal) {
            ((TronGame) game).saveLose();
            state = GameState.Loser;
          } else {
            countdown = 300.0f;
            state = GameState.Lose;
          }
        }
      }

      if (msg.startsWith("READY")) {
        connection.write("START".getBytes());
        countdown = 300.0f;
        state = GameState.Ready;
        restart();
      }
      if (msg.startsWith("START")) {
        countdown = 300.0f;
        state = GameState.Ready;
        restart();
      }
      if (msg.startsWith("EXIT")) {
        countdown = 300.0f;
        state = GameState.Error;
        restart();
      }
    }
  }