Exemplo 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!");
  }
Exemplo 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);
  }
Exemplo n.º 3
0
  public void serverPlay() {
    server = new Server();
    if ((server.getClient() == null) || (server.getServerSocket() == null)) {
      System.out.println("Error during establishing connection!");
      return;
    }
    server.writeToOutputStream(game.getFieldSize());
    int answer;
    do {
      boolean okCoords = false;
      do {
        answer = humanInput();
        if (answer == 1) {
          continue;
        } else if (answer == 2) {
          server.closeSocket();
          server = null;
          return;
        }
        okCoords = controller.figurePlaced(coordI, coordJ); // server place X
      } while (!okCoords);
      server.writeToOutputStream(coordI);
      server.writeToOutputStream(coordJ);
      if (game.getWinner() != ' ') {
        break;
      }

      System.out.println("O goes to...");
      coordI = server.getToInputStream();
      coordJ = server.getToInputStream();
      if ((coordI != -1) && (coordJ != -1)) {
        controller.figurePlaced(coordI, coordJ); // client place O
      } else {
        System.out.println("Network connection error!");
        server.closeSocket();
        server = null;
        return;
      }
    } while (game.getWinner() == ' ');
    server.closeSocket();
  }
Exemplo n.º 4
0
 private int pvcHumanGoes() {
   boolean placed = false;
   int answer;
   do {
     System.out.println("Press 'z' instead of coordinate to cancel your last move.");
     answer = humanInput();
     if (answer == 1) {
       continue;
     } else if (answer == 2) {
       return 2;
     } else if (answer == 3) {
       System.out.println("Field after cancel:");
       if (!controller.cancelMove()) {
         System.out.println("Error during cancel!");
       }
       placed = false;
       continue;
     }
     placed = controller.figurePlaced(coordI, coordJ);
   } while (!placed);
   return 0;
 }
Exemplo n.º 5
0
 public void pvpGameOffline() {
   System.out.println(RULE);
   printField();
   do {
     int answer = humanInput();
     if (answer == 1) {
       continue;
     } else if (answer == 2) {
       break;
     }
     controller.figurePlaced(coordI, coordJ);
   } while (game.getWinner() == ' ');
 }
Exemplo n.º 6
0
 private void pvcCompGoes() {
   System.out.println("Computer goes to...");
   game.makeComputerStep(coordI, coordJ, game.getFieldSize(), game.getInstanceField());
   controller.figurePlaced(game.getComputer().getCoordI(), game.getComputer().getCoordJ());
 }