public TentsAndTrees(PlayingField playingField) { if (playingField == null) { throw new IllegalArgumentException("PlayingField is null."); } gameField = playingField.getPlayingField(); columnHints = playingField.getColumnHints(); rowHints = playingField.getRowHints(); this.height = gameField.length; this.width = gameField[0].length; }
/* * Controls what happens when an action is detected (a menu item is * selected). */ public void actionPerformed(ActionEvent e) { /* The file to load from or save to. */ File file; /* Cast the source of the ActionEvent into a menu item for selection. */ JMenuItem source = (JMenuItem) (e.getSource()); /* Detect which menu item was clicked. */ if (source.getText().equals("Basic game")) { /* Upon selecting a new basic game, create a new basic game. */ this.currentgame.newGame("Basic", 5); } else if (source.getText().equals("Easy feeding game")) { /* * Upon selecting an easy feeding game, create a new easy feeding * game. */ this.currentgame.newGame("Feeding", 3); } else if (source.getText().equals("Moderate feeding game")) { /* * Upon selecting a moderate feeding game, create a new moderate * feeding game. */ this.currentgame.newGame("Feeding", 4); } else if (source.getText().equals("Hard feeding game")) { /* * Upon selecting a hard feeding game, create a new hard feeding * game. */ currentgame.newGame("Feeding", 10); } else if (source.getText().equals("Load...")) { /* Pause the game. */ this.currentgame.stopFeedingRow(); this.currentgame.pauseTimeLimit(); this.currentgame.stopTimeLimit(); /* * Present the user with a file chooser dialog to select the file * to load. */ file = getInputFile("Choose game to load"); if (file != null) { /* If the user chose a file, load it. */ this.currentgame.loadGame(file); } else { /* If a user cancelled loading resume play. */ this.currentgame.startFeedingRow(); this.currentgame.resumeTimeLimit(); this.currentgame.startTimeLimit(); } } else if (source.getText().equals("Save...")) { /* If the game is still going on... */ if (!this.currentgame.isGameOver()) { /* Pause the game. */ this.currentgame.stopFeedingRow(); this.currentgame.stopTimeLimit(); this.currentgame.pauseTimeLimit(); /* * Present the user with a file chooser dialog to choose where * they want to save to. */ file = getOutputFile("Choose where to save"); if (file != null) { /* If a file is selected, save to it. */ this.currentgame.saveGame(file); } /* Resume play. */ this.currentgame.startFeedingRow(); this.currentgame.resumeTimeLimit(); this.currentgame.startTimeLimit(); } else { /* * Emit a system beep to alert the user that the game cannot * be saved at this time. */ Toolkit.getDefaultToolkit().beep(); } } else if (source.getText().equals("Quit")) { /* Quit the game. */ System.exit(0); } }