Esempio n. 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();
      }
    }
  }
Esempio n. 2
0
  private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {

    timePassed += deltaTime;

    for (TouchEvent event : touchEvents) {
      if (event.type == TouchEvent.TOUCH_DOWN) {

        if (event.x < buttonWidth) {
          player.moveLeft();
        } else if (event.x > buttonWidth + game.getGraphics().getHeight()) {
          player.moveRight();
        }
      }
    }

    if (!player.isAlive) {
      connection.write("LOST".getBytes());
      opponent.scored();
      if (opponent.getScore() == goal) {
        ((TronGame) game).saveLose();
        state = GameState.Loser;
      } else {
        countdown = 300.0f;
        state = GameState.Lose;
      }
    } else if (timePassed > 10.0) {
      player.update();
      grid.update(player);
      timePassed -= 10.0f;
    }
  }
Esempio n. 3
0
  /** @param args */
  public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    TronGame g = new TronGame();
    int turn = 0;

    g.init("empty-room");
    g.addPlayer("alice");
    g.addPlayer("bob");

    try {
      while (true) {
        String line = br.readLine();
        if (line.compareTo("quit") == 0) {
          break;
        } else if (line.compareTo("print") == 0) {
          System.out.println(g.getHumanReadableState(g.getPlayers().get(turn)));
        } else {
          if (g.isWellFormedMove(line)) {
            g.makeMove(g.getPlayers().get(turn), line);
            turn = 1 - turn;
            System.out.println(g.getHumanReadableState(g.getPlayers().get(turn)));
            System.out.println(g.getPlayers().get(turn) + "'s turn");

            if (g.isGameOver()) {
              System.out.println("Game over");
              System.out.println(g.getResultString());
            }
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }