예제 #1
0
  /** Runs the game. */
  @Override
  public void run() {
    System.out.println("Run");
    /* listen and validate Navy objects*/
    while (!navyValid[PLAYER0] || !navyValid[PLAYER1]) {
      System.out.println("In Whileloop");
      if (!navyValid[PLAYER0]) {
        boolean isValid = readAndValidate(PLAYER0);
        navyValid[PLAYER0] = isValid;
        if (isValid) {
          System.out.println("validated navy 1");
        }
        player[PLAYER0].sendMessage(new ValidationMessage(isValid));
      }

      // only validate if the other player is real
      if (player[PLAYER1] == null) { // no real opponent
        navyValid[PLAYER1] = true; // skip validation of server Navy
        navy[PLAYER1] = serverAI.getNavy();
        System.out.println("server already valid");
      } else if (!navyValid[PLAYER1]) {
        System.out.println("validating navy 2");
        boolean isValid = readAndValidate(PLAYER1);
        navyValid[PLAYER1] = isValid;
        if (isValid) {
          System.out.println("validated navy 2");
        }
        player[PLAYER1].sendMessage(new ValidationMessage(isValid));
      }
    }

    System.out.println("left validation loop");

    // let first player shoot
    player[currentPlayer].sendMessage(new NavyMessage(navy[currentPlayer], true));

    enterGameLoop();
  } // run end
예제 #2
0
  /**
   * The game loop. Read messages/getshots and evaluate. Messages are sent to clients(not to the
   * ServerAI)
   */
  private void enterGameLoop() {
    boolean loop = true;
    Coordinate shotCoordinate = null;
    Ship hitShip = null;
    while (loop) {

      // reset
      isHit = false;
      isSunk = false;
      grantTurn = false;
      finished = false;
      hitShip = null;
      shotCoordinate = null;

      // Read message
      if (player[currentPlayer] != null) { // an actual player

        Message msg = readMessage(player[currentPlayer]);
        Shot shotMsg = null;
        if (msg.getType().equals("Shot")) {
          System.out.println("received Shot");
          shotMsg = (Shot) msg;
          shotCoordinate = shotMsg.getCoordinate();
        }

      } else { // get shot coordinate from ServerAI
        if (serverAI != null) {
          shotCoordinate = serverAI.shoot();
          System.out.println("server generated shot");
        }
      }

      // do we have a Coordinate?
      if (shotCoordinate != null) {
        System.out.println(
            "X: "
                + Integer.toString(shotCoordinate.getX())
                + " Y: "
                + Integer.toString(shotCoordinate.getY()));

        // shoot the other players navy
        hitShip = navy[otherPlayer].shot(shotCoordinate);

        // a hit
        if (hitShip != null) {
          isHit = true;
          System.out.println(hitShip.getName());
          if (!hitShip.isSunk()) {
            hitShip = null; // don´t send Ship unless sunk
          } else {
            System.out.println("sunk");
            isSunk = true;
            // check if won
            if (navy[otherPlayer].allGone()) {
              finished = true;
            }
          }
        }
        // no hit
        else {
          System.out.println("Let the other one fire");
          // if miss  let the other one fire
          grantTurn = true;
        }

        // Tell players if it´s a hit?

        // send hitMessage to currentPlayer
        if (player[currentPlayer] != null) {
          player[currentPlayer].sendMessage(new HitMessage(isHit, shotCoordinate, isSunk, hitShip));
        }

        // send NavyMessage to otherPlayer
        if (player[otherPlayer] != null) {
          player[otherPlayer].sendMessage(new NavyMessage(navy[otherPlayer], grantTurn));
        }

        if (finished) {
          if (player[currentPlayer] != null) {
            player[currentPlayer].sendMessage(new FinishedMessage(true, navy[currentPlayer]));
          }

          // send hitMessage to otherPlayer
          if (player[otherPlayer] != null) {
            player[otherPlayer].sendMessage(new FinishedMessage(false, navy[otherPlayer]));
          }

          loop = false;
        }

        // if otherPlayer was granted next turn, Switch player
        if (grantTurn) {
          System.out.println("Switching player");
          switchPlayer();
        }
      } else { // the message received was of wrong type
        if (player[currentPlayer] != null) {
          player[currentPlayer].sendMessage(new ValidationMessage(false));
        }
      }
    } // while end
  }