/**
   * Launch the game, and imitate what would happen in a typical game.
   *
   * @throws InterruptedException Since we're sleeping in this test.
   */
  @Test
  public void storyTwoTest() throws InterruptedException {
    Game game = launcher.getGame();
    Player player = game.getPlayers().get(0);

    // start cleanly.
    assertFalse(game.isInProgress());

    game.start();
    assertTrue(game.isInProgress());
    assertEquals(0, player.getScore());

    // scenario 2.1: player moves one step to the right and gets 10 points
    game.move(player, Direction.EAST);
    assertEquals(10, player.getScore());

    // scenario 2.2: player moves back to the left (empty sqare) and the score doesn't change;
    game.move(player, Direction.WEST);
    assertEquals(10, player.getScore());

    // scenario 2.4: try to move downwards and the movement fails
    game.move(player, Direction.SOUTH);
    assertEquals(0, Direction.SOUTH.getDeltaX());

    game.stop();
    assertFalse(game.isInProgress());
  }
Beispiel #2
0
  /**
   * Creates a new UI for a JPac-Man game.
   *
   * @param game The game to play.
   * @param buttons The map of caption-to-action entries that will appear as buttons on the
   *     interface.
   * @param keyMappings The map of keyCode-to-action entries that will be added as key listeners to
   *     the interface.
   */
  public PacManUI(
      final Game game, final Map<String, Action> buttons, final Map<Integer, Action> keyMappings) {
    super("JPac-Man");
    assert game != null;
    assert buttons != null;
    assert keyMappings != null;

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    PacKeyListener keys = new PacKeyListener(keyMappings);
    addKeyListener(keys);

    JPanel buttonPanel = new ButtonPanel(buttons, this);

    scorePanel = new ScorePanel(game.getPlayers());
    boardPanel = new BoardPanel(game);

    Container contentPanel = getContentPane();
    contentPanel.setLayout(new BorderLayout());
    contentPanel.add(buttonPanel, BorderLayout.SOUTH);
    contentPanel.add(scorePanel, BorderLayout.NORTH);
    contentPanel.add(boardPanel, BorderLayout.CENTER);

    pack();
  }
 /**
  * Make number of moves in given direction.
  *
  * @param game The game we're playing
  * @param dir The direction to be taken
  * @param numSteps The number of steps to take
  */
 public static void move(Game game, Direction dir, int numSteps) {
   Player player = game.getPlayers().get(0);
   for (int i = 0; i < numSteps; i++) {
     game.move(player, dir);
   }
 }