public void pvcGame() { System.out.println(RULE); printField(); String humanFigure; do { System.out.println("Input your figure. Press 'x' or 'o'. Press 'q' to quit."); if (!tryInput()) { return; } } while ((!inString.equals(X)) && (!inString.equals(O))); humanFigure = inString; if (humanFigure.equals(X)) { do { if (pvcHumanGoes() == 2) { return; } if (game.getWinner() != ' ') { break; } pvcCompGoes(); } while (game.getWinner() == ' '); } else { do { pvcCompGoes(); if (game.getWinner() != ' ') { break; } if (pvcHumanGoes() == 2) { return; } } while (game.getWinner() == ' '); } }
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!"); }
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(); }
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() == ' '); }
public void printLine(int i) { for (int j = 0; j < game.getFieldSize(); j++) { System.out.print("[" + game.getField()[i][j] + "] "); } System.out.println(""); }
public void printField() { for (int i = 0; i < game.getFieldSize(); i++) { printLine(i); } }
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()); }