示例#1
0
 private String requestEntry(String prompt) {
   String entry = "";
   while (entry.length() < 1) {
     entry = JOptionPane.showInputDialog(null, prompt, "");
     if (entry == null) System.exit(0);
   }
   return entry.trim();
 }
示例#2
0
 public GGNimPlayer() {
   super(56, 9, 12, false);
   sessionID = sessionID + requestEntry("Enter a unique session ID (ASCII >3 chars):");
   agent = new TcpAgent(sessionID, serverName);
   agentName = requestEntry("Enter your name (ASCII >3 chars):");
   initGameWindow();
   addStatusBar(30);
   setStatusText("Connecting to relay " + agent.getRelay() + "...");
   agent.addTcpAgentListener(new MyTcpAgentAdapter());
   ArrayList<String> connectList = agent.connectToRelay(agentName, 6000);
   if (connectList.isEmpty()) {
     setStatusText("Connection to relay failed. Terminating now...");
     TcpTools.delay(3000);
     System.exit(1);
   }
   setStatusText("Connection established. Personal name: " + connectList.get(0));
   setTitle("Player's name: " + connectList.get(0));
   // Game server must be up and running
   if (!agent.isBridgeConnected()) {
     setStatusText("Game server not found. Terminating now...");
     TcpTools.delay(3000);
     System.exit(1);
   }
 }
示例#3
0
    public void dataReceived(String source, int[] data) {
      if (!isReady) return;

      String str = "";
      switch (data[0]) {
        case Command.GAME_RUNNING:
          setStatusText("Game in process.\nPlease wait for next game.\n" + "Terminating now...");
          TcpTools.delay(5000);
          System.exit(0);
          break;

        case Command.PLAYER_DISCONNECT:
          disconnectedPlayer = TcpTools.intAryToString(data, 1);
          break;

        case Command.NUMBER_OF_PLAYERS:
          initPlayground();
          if (!disconnectedPlayer.equals(""))
            disconnectedPlayer = "Disconnected: " + disconnectedPlayer + ". ";
          setStatusText(
              disconnectedPlayer
                  + "Current number in group: "
                  + data[1]
                  + ". Waiting for more players to join...");
          disconnectedPlayer = "";
          break;

        case Command.GAME_STARTING:
          str = "Group complete. In group: ";
          String[] names = TcpTools.split(TcpTools.intAryToString(data, 1), "&&");
          for (int i = 0; i < names.length; i++) {
            if (i < names.length - 1) str += names[i] + "; ";
            else str += names[i];
          }
          setStatusText(str + ". Wait start selection...");
          break;

        case Command.INIT_PLAYGROUND:
          nbMatches = data[1];
          initPlayground();
          break;

        case Command.REPORT_POSITION:
          Location loc = new Location(data[1], 4);
          getOneActorAt(loc).removeSelf();
          nbMatches--;
          setStatusText(nbMatches + " left. " + remotePlayer + " is playing.");
          refresh();
          break;

        case Command.LOCAL_MOVE:
          activate();
          setStatusText(nbMatches + " left. It's you to play.");
          nbTaken = 0;
          okBtn.setEnabled(true);
          isMouseEnabled = true;
          break;

        case Command.REMOTE_MOVE:
          remotePlayer = TcpTools.intAryToString(data, 1);
          setStatusText(nbMatches + " left. " + remotePlayer + " will play.");
          break;

        case Command.WINNER:
          String loser = TcpTools.intAryToString(data, 1);
          setStatusText("Game over. Player " + loser + " lost the game.");
          break;

        case Command.LOSER:
          setStatusText("Game over. You lost the game.");
          break;
      }
    }