Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Created December 14, 2016 15:50
Show Gist options
  • Save timmolderez/f661e42a84484b66b4b22dd79f7a0c11 to your computer and use it in GitHub Desktop.
Save timmolderez/f661e42a84484b66b4b22dd79f7a0c11 to your computer and use it in GitHub Desktop.
Modified version of Launcher.java to be used in the mock testing exercises
package nl.tudelft.jpacman;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.google.java.contract.Ensures;
import com.google.java.contract.Invariant;
import com.google.java.contract.Requires;
import nl.tudelft.jpacman.board.BoardFactory;
import nl.tudelft.jpacman.board.Direction;
import nl.tudelft.jpacman.game.Game;
import nl.tudelft.jpacman.game.GameFactory;
import nl.tudelft.jpacman.level.Level;
import nl.tudelft.jpacman.level.LevelFactory;
import nl.tudelft.jpacman.level.MapParser;
import nl.tudelft.jpacman.level.Player;
import nl.tudelft.jpacman.level.PlayerFactory;
import nl.tudelft.jpacman.npc.ghost.GhostFactory;
import nl.tudelft.jpacman.sprite.PacManSprites;
import nl.tudelft.jpacman.ui.Action;
import nl.tudelft.jpacman.ui.PacManUI;
import nl.tudelft.jpacman.ui.PacManUiBuilder;
/**
* Creates and launches the JPacMan UI.
* (Modified so you can specify which level .txt file to load - Tim Molderez)
*
* @author Jeroen Roosen
*/
@Invariant("false")
public class Launcher {
private static final PacManSprites SPRITE_STORE = new PacManSprites();
private PacManUI pacManUI;
private Game game;
/**
* @return The game object this launcher will start when {@link #launch()}
* is called.
*/
public Game getGame() {
return game;
}
/**
* Creates a new game.
* @param boardFile Text file specifying the Pacman level
* @return a new Game.
*/
public Game makeGame(String boardFile) {
GameFactory gf = getGameFactory();
Level level = makeLevel(boardFile);
return gf.createSinglePlayerGame(level);
}
/**
* Creates a new level. By default this method will use the map parser to
* parse the default board stored in the <code>board.txt</code> resource.
*
* @return A new level.
*/
public Level makeLevel(String boardFile) {
MapParser parser = getMapParser();
try (InputStream boardStream = Launcher.class
.getResourceAsStream("/" + boardFile)) {
return parser.parseMap(boardStream);
} catch (IOException e) {
throw new PacmanConfigurationException("Unable to create level.", e);
}
}
/**
* @return A new map parser object using the factories from
* {@link #getLevelFactory()} and {@link #getBoardFactory()}.
*/
protected MapParser getMapParser() {
return new MapParser(getLevelFactory(), getBoardFactory());
}
/**
* @return A new board factory using the sprite store from
* {@link #getSpriteStore()}.
*/
protected BoardFactory getBoardFactory() {
return new BoardFactory(getSpriteStore());
}
/**
* @return The default {@link PacManSprites}.
*/
protected PacManSprites getSpriteStore() {
return SPRITE_STORE;
}
/**
* @return A new factory using the sprites from {@link #getSpriteStore()}
* and the ghosts from {@link #getGhostFactory()}.
*/
protected LevelFactory getLevelFactory() {
return new LevelFactory(getSpriteStore(), getGhostFactory());
}
/**
* @return A new factory using the sprites from {@link #getSpriteStore()}.
*/
protected GhostFactory getGhostFactory() {
return new GhostFactory(getSpriteStore());
}
/**
* @return A new factory using the players from {@link #getPlayerFactory()}.
*/
protected GameFactory getGameFactory() {
return new GameFactory(getPlayerFactory());
}
/**
* @return A new factory using the sprites from {@link #getSpriteStore()}.
*/
protected PlayerFactory getPlayerFactory() {
return new PlayerFactory(getSpriteStore());
}
/**
* Adds key events UP, DOWN, LEFT and RIGHT to a game.
*
* @param builder
* The {@link PacManUiBuilder} that will provide the UI.
* @param game
* The game that will process the events.
*/
@Requires("false")
protected void addSinglePlayerKeys(final PacManUiBuilder builder,
final Game game) {
final Player p1 = getSinglePlayer(game);
builder.addKey(KeyEvent.VK_UP, new Action() {
@Override
public void doAction() {
game.move(p1, Direction.NORTH);
}
}).addKey(KeyEvent.VK_DOWN, new Action() {
@Override
public void doAction() {
game.move(p1, Direction.SOUTH);
}
}).addKey(KeyEvent.VK_LEFT, new Action() {
@Override
public void doAction() {
game.move(p1, Direction.WEST);
}
}).addKey(KeyEvent.VK_RIGHT, new Action() {
@Override
public void doAction() {
game.move(p1, Direction.EAST);
}
});
}
private Player getSinglePlayer(final Game game) {
List<Player> players = game.getPlayers();
if (players.isEmpty()) {
throw new IllegalArgumentException("Game has 0 players.");
}
final Player p1 = players.get(0);
return p1;
}
/**
* Creates and starts a JPac-Man game.
*/
@Requires("false")
@Ensures("false")
public void launch() {
launch("board.txt");
}
/**
* Creates and starts a JPac-Man game.
* @param boardFile Text file (in resources directory) specifying the Pacman level
*/
@Requires("false")
@Ensures("false")
public void launch(String boardFile) {
game = makeGame(boardFile);
PacManUiBuilder builder = new PacManUiBuilder().withDefaultButtons();
addSinglePlayerKeys(builder, game);
pacManUI = builder.build(game);
pacManUI.start();
}
/**
* Disposes of the UI. For more information see {@link javax.swing.JFrame#dispose()}.
*/
public void dispose() {
pacManUI.dispose();
}
/**
* Main execution method for the Launcher.
*
* @param args
* The command line arguments - which are ignored.
* @throws IOException
* When a resource could not be read.
*/
public static void main(String[] args) throws IOException {
new Launcher().launch();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment