Untitled
unknown
java
3 years ago
2.7 kB
5
Indexable
package com.comp301.a02adventure; import java.util.List; public class GameImpl implements Game { private final Map map; private final Player player; public GameImpl(Map map, Player player) throws IllegalArgumentException { this.map = map; if (this.map == null) { throw new IllegalArgumentException("Map is null"); } this.player = player; if (this.player == null) { throw new IllegalArgumentException("Player is null"); } } @Override public Position getPlayerPosition() { return player.getPosition(); } @Override public String getPlayerName() { return player.getName(); } @Override public List<Item> getPlayerItems() { return player.getInventory().getItems(); } @Override public boolean getIsWinner() { return player.getInventory().getNumItems() == map.getNumItems(); } @Override public void printCellInfo() { System.out.println("Location: " + map.getCell(getPlayerPosition()).getName()); System.out.println(map.getCell(getPlayerPosition()).getDescription()); if ((map.getCell(getPlayerPosition()).getIsVisited())) { System.out.println("You have already visited this location."); } if ((map.getCell(getPlayerPosition()).hasChest())) { System.out.println("You found a chest! Type 'open' to see what's inside, or keep moving."); } map.getCell(getPlayerPosition()).visit(); } @Override public void openChest() { if (!(map.getCell(getPlayerPosition()).hasChest())) { System.out.println("No chest to open, sorry!"); } else { if (map.getCell(getPlayerPosition()).getChest().isEmpty()) { System.out.println("The chest is empty."); } else { System.out.println( "You collected these items: " + map.getCell(getPlayerPosition()).getChest().getItems()); player.getInventory().transferFrom(map.getCell(getPlayerPosition()).getChest()); } } } @Override public boolean canMove(Direction direction) { try { map.getCell(getPlayerPosition().getNeighbor(direction)); } catch (Exception e) { return false; } return (map.getCell(getPlayerPosition().getNeighbor(direction)) != null) && ((getPlayerPosition().getNeighbor(direction).getX()) <= map.getWidth()) && ((getPlayerPosition().getNeighbor(direction).getY()) <= map.getHeight()); } @Override public void move(Direction direction) { if (!canMove(direction)) { System.out.println("You can't go that way! Try another direction."); } else { player.move(direction); printCellInfo(); } } }
Editor is loading...