Exemplo n.º 1
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);
              }
            }));
  }
Exemplo n.º 2
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!");
  }
Exemplo n.º 3
0
  private void majgrid1(Player player) {
    gridtest = new Grid();
    gridtest = player.getGridJoueur();

    ShipType type = ShipType.AIRCRAFT_CARRIER;
    Ship ship = new Ship(type);
    Orientation orient = Orientation.HORIZONTAL;
    for (int i = 0; i < 10; i++) // ligne
    {
      for (int j = 0; j < 10; j++) // colonne
      {
        if (gridtest.getCell(i, j) instanceof OceanCell) {
          tabbout1[i][j].setIcon(iconeau);
        }
        //                else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
        //                {
        //                    tabbout1[i][j].setIcon(iconeautouch);
        //                }
        //                else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
        //                {
        //                    tabbout1[i][j].setIcon(icondebristouch);
        //                }
        else if (gridtest.getCell(i, j) instanceof ShipCell) {
          ship = ((ShipCell) gridtest.getCell(i, j)).getShip();
          int size = ship.getSize();
          orient = ((ShipCell) gridtest.getCell(i, j)).getOrient();

          if (orient.isHorizontal()) {
            if (ship.getSize() == 2) {
              tabbout1[i][j].setIcon(iconsub1h);
              tabbout1[i][j + 1].setIcon(iconsub2h);
              tabbout1[i][j + 2].setIcon(iconsub3h);
              j = j + 2;

            } else if (ship.getSize() == 3) // battleship
            {
              tabbout1[i][j].setIcon(iconbattle1h);
              tabbout1[i][j + 1].setIcon(iconbattle2h);
              tabbout1[i][j + 2].setIcon(iconbattle3h);
              tabbout1[i][j + 3].setIcon(iconbattle4h);
              j = j + 3;
            } else {
              tabbout1[i][j].setIcon(iconair1h);
              tabbout1[i][j + 1].setIcon(iconair2h);
              tabbout1[i][j + 2].setIcon(iconair3h);
              tabbout1[i][j + 3].setIcon(iconair4h);
              tabbout1[i][j + 4].setIcon(iconair5h);
              j = j + 4;
            }
          }
        }
      }

      for (int i2 = 0; i2 < 10; i2++) // ligne
      {
        for (int j2 = 0; j2 < 10; j2++) // colonne
        {
          if (gridtest.getCell(i2, j2) instanceof ShipCell) {
            ship = ((ShipCell) gridtest.getCell(i2, j2)).getShip();
            orient = ((ShipCell) gridtest.getCell(i2, j2)).getOrient();

            if (!orient.isHorizontal()) {
              if (ship.getSize() == 2) {
                tabbout1[i2][j2].setIcon(iconsub1v);
                tabbout1[i2 + 1][j2].setIcon(iconsub2v);
                tabbout1[i2 + 2][j2].setIcon(iconsub3v);
                i2 = i2 + 2;
              } else if (ship.getSize() == 3) // battleship
              {
                tabbout1[i2][j2].setIcon(iconbattle1v);
                tabbout1[i2 + 1][j2].setIcon(iconbattle2v);
                tabbout1[i2 + 2][j2].setIcon(iconbattle3v);
                tabbout1[i2 + 3][j2].setIcon(iconbattle4v);
                i2 = i2 + 3;
              } else {
                tabbout1[i2][j2].setIcon(iconair1v);
                tabbout1[i2 + 1][j2].setIcon(iconair2v);
                tabbout1[i2 + 2][j2].setIcon(iconair3v);
                tabbout1[i2 + 3][j2].setIcon(iconair4v);
                tabbout1[i2 + 4][j2].setIcon(iconair5v);
                i2 = i2 + 4;
              }
            }
          }
        }
      }
    }
  }