// ship should be 'A', 'B', 'C', or 'D'
  // loc should be validated
  // Orientation should be determined by the view/controller from user input
  public boolean placeShip(boolean isPlayer1, char ship, String loc, Orientation o) {
    Ship shipToPlace = getShip(ship);
    int startRow = getRow(loc);
    int startCol = getCol(loc);
    int dx = o.dx;
    int dy = o.dy;

    // attempt to place the ship in each BoardSquare
    for (int i = 0; i < shipToPlace.getLength(); i++) {
      int row = startRow + i * dy;
      int col = startCol + i * dx;
      Ship toChange = (isPlayer1) ? board[row][col].P1Ship : board[row][col].P2Ship;

      if (row < 0 || row > 9 || col < 0 || col > 9 || toChange != null) {
        shipToPlace = null;
        return false;
      } else {
        toChange = shipToPlace;
      }
    }
    // incrementShipCount()
    if (isPlayer1) {
      player1ShipCount++;
    } else {
      player2ShipCount++;
    }
    return true;
  }
  // Return the array of offensive grid values for player based on passed param
  public char[] getDefensiveGrid(boolean isPlayer1) {
    char[] defenseGridVals = new char[BOARD_WIDTH * BOARD_HEIGHT];

    for (int row = 0; row < BOARD_WIDTH; row++) {
      for (int col = 0; col < BOARD_HEIGHT; col++) {
        // value begins as empty
        char gridVal = ' ';

        // get and return proper defensive grid values
        BoardSquare current = board[row][col];
        Ship ship = (isPlayer1) ? current.P1Ship : current.P2Ship;

        if (ship != null) {
          gridVal = ship.getReference();
        }

        defenseGridVals[row * BOARD_WIDTH + col] = gridVal;
      }
    }
    return defenseGridVals;
  }
  // returns "Hit", "Miss", "Hit and sunk <ship_name>", or "Unsuccessful"
  public String makeShot(boolean isPlayer1, String loc) {
    int row = getRow(loc);
    int col = getCol(loc);
    BoardSquare current = board[row][col];

    boolean isShot = (isPlayer1) ? current.P1Offensive : current.P2Offensive;
    if (isShot) {
      return "Unsuccessful";
    } else {
      // loc has not been shot by player, set it as shot
      if (isPlayer1) {
        current.P1Offensive = true;
      } else {
        current.P2Offensive = true;
      }

      Ship target = (isPlayer1) ? board[row][col].P2Ship : board[row][col].P1Ship;
      if (target == null) {
        return "Miss";
      } else {
        target.damage++;
        if (target.damage == target.getLength()) {
          // ship is destroyed, decrement ship count
          if (isPlayer1) {
            player2ShipCount--;
          } else {
            player2ShipCount--;
          }
          isGameOver = player1ShipCount <= 0 || player2ShipCount <= 0;

          String playerName = (isPlayer1) ? player1Name : player2Name;
          return "Hit and sunk " + playerName + "'s " + target.getReference() + "!";
        }
      }
      return "Hit";
    }
  }
Пример #4
0
  void turnOnTurretReceiver() throws IOException {
    Global.log("Turning on turret receiver...");
    while (true) {
      if (isDisposed()) throw new IOException("Client game disposed");
      Global.log("Connecting to port " + Global.turretPort() + "...");
      try {
        turretStream = new ClientByteStream(ip, Global.turretPort(), Ship.bufferSize());
        break;
      } catch (IOException ex) {
      }
    }
    Global.log("Connected!");
    timers.add(
        new FixedTimer(
            new FixedTask() {
              public boolean fixedRate() {
                return false;
              }

              public float FPS() {
                return Global.ReceiveFPS;
              }

              public void run() {
                byte[] data = null;
                String name = null;
                try {
                  data = turretStream.read();
                  name = turretStream.readLine();
                } catch (IOException ex) {
                  System.err.println("Cannot read info from turret stream");
                  Global.onException();
                  stop();
                  return;
                }
                if (data == null) return;
                Ship s = new Ship(name, Global.transparent);
                s.setDesign(new Design.Turret(s));
                s.fromBytes(data, false);
                addShip(s);
              }
            }));
  }
Пример #5
0
  void createBytePorts() throws IOException {

    Global.log("Creating byte ports...");

    int n = numPlayers();
    playerByteStreams = new ClientByteStream[n];

    for (int i = 0; i < n; ++i) {

      ClientByteStream stream = null;

      while (true) {

        if (isDisposed()) throw new IOException("Client game disposed");
        Global.log("Connecting to port " + Global.playerPort(i) + "...");
        try {
          stream =
              playerByteStreams[i] =
                  new ClientByteStream(ip, Global.playerPort(i), Ship.bufferSize());
          break;
        } catch (IOException ex) {
        }
      }

      Global.log("Connected!");
      Global.log("Sending client information...");

      // first byte is player ID

      stream.out.write((byte) playerID);
      stream.out.flush();
      Global.log("Done!");
    }

    Global.log("Done creating byte ports!");
  }