private Message readMessage(Player p) { Message msg = null; while (msg == null) { try { msg = p.readMessage(); if (msg == null) { Thread.sleep(500); } else { System.out.println("Got a message: " + msg.getType()); } } catch (Exception e) { } } return msg; }
/** Listening for NavyMessage and verifies the Navy object. */ private boolean readAndValidate(int playerNumber) { Validator validator = new Validator(SUBMARINES, DESTROYERS, AIRCRAFT_CARRIERS); Message msg = readMessage(player[playerNumber]); if (msg.getType().equals("NavyMessage")) { System.out.println("We have a navy"); NavyMessage navMsg = (NavyMessage) msg; if (navMsg.getNavy().allSet()) { System.out.println("allSet"); } if (validator.validateNavy(navMsg.getNavy())) { System.out.println("it´s valid"); navy[playerNumber] = navMsg.getNavy(); return true; } else { System.out.println("it´s not valid"); return false; } } else { System.out.println("We don´t have a navy"); return false; } }
/** * The game loop. Read messages/getshots and evaluate. Messages are sent to clients(not to the * ServerAI) */ private void enterGameLoop() { boolean loop = true; Coordinate shotCoordinate = null; Ship hitShip = null; while (loop) { // reset isHit = false; isSunk = false; grantTurn = false; finished = false; hitShip = null; shotCoordinate = null; // Read message if (player[currentPlayer] != null) { // an actual player Message msg = readMessage(player[currentPlayer]); Shot shotMsg = null; if (msg.getType().equals("Shot")) { System.out.println("received Shot"); shotMsg = (Shot) msg; shotCoordinate = shotMsg.getCoordinate(); } } else { // get shot coordinate from ServerAI if (serverAI != null) { shotCoordinate = serverAI.shoot(); System.out.println("server generated shot"); } } // do we have a Coordinate? if (shotCoordinate != null) { System.out.println( "X: " + Integer.toString(shotCoordinate.getX()) + " Y: " + Integer.toString(shotCoordinate.getY())); // shoot the other players navy hitShip = navy[otherPlayer].shot(shotCoordinate); // a hit if (hitShip != null) { isHit = true; System.out.println(hitShip.getName()); if (!hitShip.isSunk()) { hitShip = null; // don´t send Ship unless sunk } else { System.out.println("sunk"); isSunk = true; // check if won if (navy[otherPlayer].allGone()) { finished = true; } } } // no hit else { System.out.println("Let the other one fire"); // if miss let the other one fire grantTurn = true; } // Tell players if it´s a hit? // send hitMessage to currentPlayer if (player[currentPlayer] != null) { player[currentPlayer].sendMessage(new HitMessage(isHit, shotCoordinate, isSunk, hitShip)); } // send NavyMessage to otherPlayer if (player[otherPlayer] != null) { player[otherPlayer].sendMessage(new NavyMessage(navy[otherPlayer], grantTurn)); } if (finished) { if (player[currentPlayer] != null) { player[currentPlayer].sendMessage(new FinishedMessage(true, navy[currentPlayer])); } // send hitMessage to otherPlayer if (player[otherPlayer] != null) { player[otherPlayer].sendMessage(new FinishedMessage(false, navy[otherPlayer])); } loop = false; } // if otherPlayer was granted next turn, Switch player if (grantTurn) { System.out.println("Switching player"); switchPlayer(); } } else { // the message received was of wrong type if (player[currentPlayer] != null) { player[currentPlayer].sendMessage(new ValidationMessage(false)); } } } // while end }