@Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Cancel": screen.show("menu"); break; case "Start game": Game startGame = null; // 1. if tjekker værdien af hvad der er valgt i comboBoxen og sammenligner med de spil som // for-loopet kører igennem // 2. if gør så hosten ikke kan joine sit eget spil for (Game g : games) { if (g.getName().equals(screen.getFindGamePanel().getSelectedGame())) { if (g.getHost().getId() != currentUser.getId()) startGame = g; } } if (startGame != null) { Gamer opponent = new Gamer(); opponent.setId(currentUser.getId()); opponent.setControls(screen.getFindGamePanel().getDirectionsTextfield()); startGame.setOpponent(opponent); String joinGamemessage = api.joinGame(startGame); String startGamemessage = api.startGame(startGame); System.out.println(startGamemessage); String winnerName = ""; for (User u : users) { try { if (u.getId() == Integer.parseInt(startGamemessage)) { winnerName = u.getUsername(); } } catch (NumberFormatException e1) { e1.printStackTrace(); } } JOptionPane.showMessageDialog( screen, joinGamemessage + ". The winner was:" + winnerName); } else { JOptionPane.showMessageDialog(screen, "You can't join a game where you're the host"); } break; } }
@Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Cancel": screen.show("menu"); break; case "Create": Game startGame = new Game(); startGame.setName(screen.getStartGamePanel().getGameName()); startGame.setMapSize(25); Gamer opponent = new Gamer(); // For-loop kører brugerne igennem og viser dem i en comboBox // 1. if tjekker værdien af hvad der er valgt i comboBoxen og sammenligner med de brugere // som for-loopet kører igennem for (User u : users) { if (u.getUsername().equals(screen.getStartGamePanel().getSelectedUSer())) { opponent.setId(u.getId()); } } // If tjekker om hosten vil udfordre sig selv og giver fejlmeddelelse og ellers er spillet // oprettet. if (opponent.getId() == currentUser.getId()) { JOptionPane.showMessageDialog( screen, "Error: You need to choose a different opponent than yourself"); } else { Gamer host = new Gamer(); host.setId(currentUser.getId()); host.setControls(screen.getStartGamePanel().getControlsToSnake()); startGame.setHost(host); startGame.setOpponent(opponent); String message = api.createGame(startGame); JOptionPane.showMessageDialog(screen, message); screen.show("menu"); } break; } }
@Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Cancel": screen.show("menu"); break; case "Delete game": Game deleteGame = new Game(); deleteGame.setName(screen.getDeleteGamePanel().getSelectedGame()); for (Game g : deleteGames) { if (g.getName().equals(screen.getDeleteGamePanel().getSelectedGame())) { deleteGame = g; } } // Besked om at spillet er slettet String message = api.deleteGame(deleteGame.getGameId()); JOptionPane.showMessageDialog(screen, message); if (message.equals("Game was deleted")) { screen.getDeleteGamePanel().removeGame(); } break; } }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // myPrintStream asd = new myPrintStream(System.out); // System.setOut(asd); Presenter presenter = new ConsolePresenter(System.out); Board board = new Board(); Player xPlayer = new ConsolePlayer(scanner, System.out); Player oPlayer = new ConsolePlayer(scanner, System.out); Referee referee = new PrimeReferee(); Game game = new Game(board, xPlayer, oPlayer, referee, presenter); game.play(); try { board.getLog(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } scanner.close(); }