Advertisement
ManuAlvarado22

Java Text Adventure

Mar 7th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. // Main
  2.  
  3. public class Main {
  4.  
  5.     public static boolean validGender(char gender) {
  6.  
  7.         char[] genders = {'m', 'f', 'o'};
  8.  
  9.         for (int i = 0; i < genders.length; i++) {
  10.             if (genders[i] == Character.toLowerCase(gender))
  11.                 return true;
  12.         }
  13.  
  14.         return false;
  15.  
  16.     }
  17.  
  18.     public static boolean playerIsGah(String name) {
  19.  
  20.         if (name.toLowerCase().equals("gah"))
  21.             return true;
  22.  
  23.         return false;
  24.  
  25.     }
  26.  
  27.     public static boolean validMovement(char[] validMovements, char movement) {
  28.  
  29.         for (char validMovement : validMovements) {
  30.             if (validMovement == movement)
  31.                 return true;
  32.         }
  33.  
  34.         return false;
  35.  
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.  
  40.         Scanner input = new Scanner(System.in);
  41.  
  42.         Player player = new Player();
  43.  
  44.         Item trumpet = new Item("Instrument", "Trumpet");
  45.         Item fiestita = new Item("Album", "La fiesta del rey dramita");
  46.  
  47.         Item cello = new Item("Instrument", "Cello");
  48.         Item kidAcito = new Item("Album", "Kid A");
  49.  
  50.         List<Item> initialItems = Arrays.asList(trumpet, fiestita);
  51.         List<Item> purrrsiaItems = Arrays.asList(cello, kidAcito);
  52.  
  53.         char[] startAdjacentRooms = new char[]{'n'};
  54.         char[] purrrsiaAdjacentRooms = new char[]{'s'};
  55.         char[] nyazuAdjacentRooms = new char[]{'s', 'w'};
  56.  
  57.         Room start = new Room("Start", new int[]{0, 0, 0, 0}, initialItems, startAdjacentRooms);
  58.         Room purrrsia = new Room("Purrrsia", new int[]{1, 0, 0, 0}, purrrsiaItems, purrrsiaAdjacentRooms);
  59.         Room nyazu = new Room("Nyazu", new int[]{2, 1, 0, 0}, purrrsiaItems, nyazuAdjacentRooms);
  60.         Room[] gameRooms = new Room[]{start, purrrsia, nyazu};
  61.         GameMap nicelyNiceMap = new GameMap(gameRooms);
  62.  
  63.     }
  64. }
  65.  
  66. // GameMap
  67.  
  68. public class GameMap {
  69.  
  70.     private Map<String, Room> rooms = new LinkedHashMap<>();
  71.  
  72.     public GameMap(Room[] rooms) {
  73.         for (Room room : rooms) {
  74.             this.rooms.put(room.getLocationToString(), room);
  75.         }
  76.     }
  77.  
  78.     public Map<String, Room> getRooms() {
  79.         return rooms;
  80.     }
  81. }
  82.  
  83. // Room
  84.  
  85. public class Room {
  86.  
  87.     private String name;
  88.     private Map<Character, Integer> location = new LinkedHashMap<>();
  89.     private List<Item> items = new ArrayList<>();
  90.     private char[] adjacentRooms;
  91.  
  92.     public Room(String name, int[] coordinates, List<Item> items, char[] adjacentRooms) {
  93.         this.name = name;
  94.         this.location.put('n', coordinates[0]);
  95.         this.location.put('e', coordinates[1]);
  96.         this.location.put('s', coordinates[2]);
  97.         this.location.put('w', coordinates[3]);
  98.         this.items.addAll(items);
  99.         this.adjacentRooms = adjacentRooms;
  100.     }
  101.  
  102.     public String toString() {
  103.         return this.name;
  104.     }
  105.  
  106.     public String getLocationToString() {
  107.  
  108.         String locationStr = "";
  109.  
  110.         for (Map.Entry<Character, Integer> entry : location.entrySet()) {
  111.             locationStr += entry.getValue() + " ";
  112.         }
  113.  
  114.         return locationStr.trim();
  115.  
  116.     }
  117.  
  118.     public String getName() {
  119.         return name;
  120.     }
  121.  
  122.     public Map<Character, Integer> getLocation() {
  123.         return location;
  124.     }
  125.  
  126.     public List<Item> getItems() {
  127.         return items;
  128.     }
  129.  
  130.     public char[] getAdjacentRooms() {
  131.         return adjacentRooms;
  132.     }
  133. }
  134.  
  135. // Item
  136.  
  137. public class Item {
  138.  
  139.     private String type;
  140.     private String name;
  141.  
  142.     public Item(String type, String name) {
  143.         this.type = type;
  144.         this.name = name;
  145.     }
  146.  
  147.     public String toString() {
  148.         return this.name;
  149.     }
  150.  
  151.     public String getType() {
  152.         return type;
  153.     }
  154.  
  155.     public String getName() {
  156.         return name;
  157.     }
  158. }
  159.  
  160. // Player
  161.  
  162. public class Player {
  163.  
  164.     private String name;
  165.     private char gender;
  166.     private Map<String, List<String>> inventory = new HashMap<>();
  167.     private Map<Character, Integer> position = new LinkedHashMap<>();
  168.  
  169.     public Player() {
  170.         this("Gah", 'o');
  171.     }
  172.  
  173.     public Player(String name, char gender) {
  174.         this.name = name;
  175.         this.gender = gender;
  176.         this.inventory.put("albums", new ArrayList<String>());
  177.         this.inventory.put("instruments", new ArrayList<String>());
  178.         this.position.put('n', 0);
  179.         this.position.put('e', 0);
  180.         this.position.put('s', 0);
  181.         this.position.put('w', 0);
  182.     }
  183.  
  184.     public void updateNorth(char direction) {
  185.         if (position.get('s') > 0) {
  186.             position.put('s', position.get('s') - 1);
  187.         }
  188.         else {
  189.             position.put('n', position.get('n') + 1);
  190.         }
  191.  
  192.     }
  193.  
  194.     public void updateSouth(char direction) {
  195.         if (position.get('n') > 0) {
  196.             position.put('n', position.get('n') - 1);
  197.         }
  198.         else {
  199.             position.put('s', position.get('s') + 1);
  200.         }
  201.     }
  202.  
  203.     public void updateEast(char direction) {
  204.         if (position.get('w') > 0) {
  205.             position.put('w', position.get('w') - 1);
  206.         }
  207.         else {
  208.             position.put('e', position.get('e') + 1);
  209.         }
  210.     }
  211.  
  212.     public void updateWest(char direction) {
  213.         if (position.get('e') > 0) {
  214.             position.put('e', position.get('e') - 1);
  215.         }
  216.         else {
  217.             position.put('w', position.get('w') + 1);
  218.         }
  219.     }
  220.  
  221.     public void move(char direction) {
  222.         switch(direction) {
  223.             case 'n':
  224.                 updateNorth(direction);
  225.                 break;
  226.             case 'e':
  227.                 updateEast(direction);
  228.                 break;
  229.             case 's':
  230.                 updateSouth(direction);
  231.                 break;
  232.             case 'w':
  233.                 updateWest(direction);
  234.                 break;
  235.         }
  236.     }
  237.  
  238.     public String getPositionToString() {
  239.  
  240.         String positionStr = "";
  241.  
  242.         for (Map.Entry<Character, Integer> coordinate : this.position.entrySet()) {
  243.             positionStr += coordinate.getValue() + " ";
  244.         }
  245.  
  246.         return positionStr.trim();
  247.  
  248.     }
  249.  
  250.     public String getName() {
  251.         return name;
  252.     }
  253.  
  254.     public char getGender() {
  255.         return gender;
  256.     }
  257.  
  258.     public Map<String, List<String>> getInventory() {
  259.         return inventory;
  260.     }
  261.  
  262.     public Map<Character, Integer> getPosition() {
  263.         return position;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement