private void createPlayersCards() { this.playersCards = new BingoCard[this.numOfPlayers]; BingoCard card; for (int i = 0; i < this.playersCards.length; i++) { card = new BingoCard(); System.out.println("---> Creating bingo card for player " + (i + 1)); this.playersCards[i] = card; card.printCard(); } }
/** * Return <code>false</code> if any of the cells in a row are not marked or if the free space is * contained in the row. Returns <code> true</code> if all cells in a row are marked. */ private boolean rowChecker(int rowNumber, BingoCard bingoCard) { // Begin row checker int numCols = bingoCard.numCols(); for (int column = 0; column < numCols; column++) { // Begin for columns loop // Get the cell at the (row, column) desired. BingoValueCell cell = bingoCard.getCellAt(rowNumber, column); // If any cell in a row is not marked, return false. if (cell.isMarked() == false) return false; // Get the location of the free space, which is located at the center of the board. Location centerLocation = new Location(bingoCard.numRows() / 2, bingoCard.numCols() / 2); // If the free space is in the column, the card does not win. if (cell.location() == centerLocation) return false; } // End for columns loop // If all of the cells in a row are marked, return true. return true; } // End row checker
/** * Returns <code>true</code> if all of the values in any row are marked and the free space is not * contained in the winning row. */ public boolean isWinner(BingoCard bingoCard) { // Begin isWinner int numRows = bingoCard.numRows(); for (int row = 1; row < numRows; row++) { if (rowChecker(row, bingoCard) == true) { return true; // If all 5 numbers in a row are filled, then return that the card has a // winner. } } return false; // If all 5 numbers are not filled, return false. } // End isWinner
@Override public void run() { List<Integer> picks = new ArrayList<>(); for (int i = 1; i <= 75; i++) { picks.add(i); } Collections.shuffle(picks); // System.out.println("game being run"); // boolean gameOver = false; int winner = 0; byte[] sendData = new byte[512]; // System.out.println(clients.size()); for (int i = 0; i < clients.size(); i++) { BingoCard card = new BingoCard(); clients.get(i).card = card; sendData = ("card:" + card.getCardValues()).getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clients.get(i).ip, clients.get(i).port); try { s.send(sendPacket); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("unable to send"); } } for (int w = 0; w < 75; w++) { // Integer randomNum =(int) (Math.random()*75); Integer randomNum = picks.get(w); for (int i = 0; i < clients.size(); i++) { clients.get(i).card.newPick(randomNum); sendData = ("pick:" + randomNum + ":" + 0 + ":" + clients.size() + " ").getBytes(); if (clients.get(i).card.isWinner()) { winner = i; w = 75; for (int j = 0; j < clients.size(); j++) { if (j == winner) { sendData = ("pick:" + randomNum + ":" + 0 + ":" + clients.size() + "You won!").getBytes(); } else { sendData = ("pick:" + randomNum + ":" + 0 + ":" + clients.size() + "You did not win. Sorry!") .getBytes(); } DatagramPacket sendPacket = new DatagramPacket( sendData, sendData.length, clients.get(j).ip, clients.get(j).port); try { s.send(sendPacket); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("unable to send"); } } } // System.out.println(w); if (w != 75) { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clients.get(i).ip, clients.get(i).port); try { s.send(sendPacket); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("unable to send"); } } } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block System.out.println("unable to sleep"); } } }