Ejemplo n.º 1
0
  public void clientPlay() {
    System.out.println("Input server ip-address");
    if (!tryInput()) {
      return;
    }
    client = new Client(inString);
    if (client.getSocket() == null) {
      System.out.println("Can't connect to the server. It could be due to bad ip-address.");
      return;
    }
    int in = client.getToInputStream();
    if (in != -1) {
      game = new Game(in); // force new game to server field.
      controller.setGame(game);
    } else {
      System.out.println("Network connection error!");
      client.closeSocket();
      client = null;
      return;
    }

    int answer;
    do {
      System.out.println("X goes to...");
      coordI = client.getToInputStream();
      coordJ = client.getToInputStream();
      if ((coordI != -1) && (coordJ != -1)) {
        controller.figurePlaced(coordI, coordJ); // server place X
      } else {
        System.out.println("Network connection error!");
        client.closeSocket();
        client = null;
        return;
      }
      if (game.getWinner() != ' ') {
        break;
      }

      boolean okCoords = false;
      do {
        answer = humanInput();
        if (answer == 1) {
          continue;
        } else if (answer == 2) {
          client.closeSocket();
          client = null;
          System.out.println("Server session ended!");
          return;
        }
        okCoords = controller.figurePlaced(coordI, coordJ); // client place O
      } while (!okCoords);
      client.writeToOutputStream(coordI);
      client.writeToOutputStream(coordJ);
    } while (game.getWinner() == ' ');

    client.closeSocket();
    System.out.println("Client session ended!");
  }
Ejemplo n.º 2
0
  public void play() {

    do {
      System.out.println(GREETING);
      if (!tryInput()) {
        break;
      }
      int answer = tryParse();
      if (answer != -1) {
        game = new Game(answer);
        controller.setGame(game);
      } else {
        game = null;
        controller.setGame(null);
        continue;
      }
      printChooseGameMode();
      if (!tryInput()) {
        continue;
      }
      answer = tryParse();
      switch (answer) {
        case 1:
          pvpGameOffline();
          continue;

        case 2:
          pvpGameOnline();
          continue;

        case 3:
          pvcGame();
          continue;

        default:
          System.out.println("You have inputted wrong number!");
          game = null;
          controller.setGame(null);
          continue;
      }
    } while (true);
  }