Advertisement
jaedashson

Dungeon

Mar 28th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.66 KB | None | 0 0
  1. // Main.java ////////////////////////////////////////////////////////////////
  2.  
  3. package dungeon;
  4.  
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10. //        new Dungeon(10,10,5,14,true).run();
  11.  
  12.         Dungeon dungeon = new Dungeon(10, 10, 5, 14, true);
  13.        
  14.         Player player = new Player(14);
  15.        
  16.         List<Vampire> vampires = new ArrayList<Vampire>();
  17.        
  18.         Vampire vampire1 = new Vampire();
  19.         vampire1.setBoundaries(10, 10);
  20.         vampire1.setPosition(1, 2);
  21.         vampires.add(vampire1);
  22.        
  23.         Vampire vampire2 = new Vampire();
  24.         vampire2.setBoundaries(10, 10);
  25.         vampire2.setPosition(7, 8);
  26.         vampires.add(vampire2);
  27.        
  28.         Vampire vampire3 = new Vampire();
  29.         vampire3.setBoundaries(10, 10);
  30.         vampire3.setPosition(7, 5);
  31.         vampires.add(vampire3);
  32.        
  33.         Vampire vampire4 = new Vampire();
  34.         vampire4.setBoundaries(10, 10);
  35.         vampire4.setPosition(8, 0);
  36.         vampires.add(vampire4);
  37.        
  38.         Vampire vampire5 = new Vampire();
  39.         vampire5.setBoundaries(10, 10);
  40.         vampire5.setPosition(2, 9);
  41.         vampires.add(vampire5);
  42.        
  43.         dungeon.printMap(player, vampires);
  44.        
  45.     }
  46. }
  47.  
  48. // Dungeon.java ////////////////////////////////////////////////////////////////
  49.  
  50. package dungeon;
  51.  
  52. import java.util.*;
  53.  
  54. public class Dungeon {
  55.  
  56.     private Scanner reader;
  57.     private DungeonMap dungeonMap;
  58.     private int length;
  59.     private int height;
  60.     private int vampires;
  61.     private int moves;
  62.     private boolean vampiresMove;
  63.  
  64.     public Dungeon(int length, int height, int vampires, int moves, boolean vampiresMove) {
  65.         reader = new Scanner(System.in);
  66.         this.length = length;
  67.         this.height = height;
  68.         this.vampires = vampires;
  69.         this.moves = moves;
  70.         this.vampiresMove = vampiresMove;
  71.  
  72.     }
  73.  
  74.     public void run() {
  75.  
  76.         dungeonMap = new DungeonMap(length, height);
  77.         createPlayer(moves);
  78.         createVampires(vampires);
  79.  
  80.         // gameplay loop
  81.         while (true) {
  82.             printDisplay();
  83.             System.out.println("");
  84.  
  85.             String input = reader.nextLine();
  86.  
  87.             // loop through movement input
  88.             for (int i = 0; i < input.length(); i++) {
  89.                 dungeonMap.move(input.charAt(i));
  90.             }
  91.  
  92.         }
  93.  
  94.     }
  95.  
  96.     public void printDisplay() {
  97.         // player's lamp
  98.         System.out.println(dungeonMap.getPlayerLamp());
  99.         System.out.println("");
  100.  
  101.         // player's position
  102.         System.out.println(dungeonMap.getPlayer());
  103.  
  104.         // vampire's positions
  105.         for (Vampire vampire : dungeonMap.getVampires()) {
  106.             System.out.println(vampire);
  107.         }
  108.  
  109.         System.out.println("");
  110.  
  111.         // print map
  112.         printMap(dungeonMap.getPlayer(), dungeonMap.getVampires());
  113.     }
  114.  
  115.     public void printMap(Player player, List<Vampire> vampires) {
  116.         for (int y = 0; y < dungeonMap.getYMax() + 1; y++) {
  117.  
  118.             for (int x = 0; x < dungeonMap.getXMax() + 1; x++) {
  119.                 boolean isNothing = true;
  120.  
  121.                 // if the player is at this position, print "@"
  122.                 if (player.getX() == x && player.getY() == y) {
  123.                     System.out.print("@");
  124.                     isNothing = false;
  125.                     continue;
  126.                 }
  127.  
  128.                 // loop through vampires. if a vampire is at this position, print "V"
  129.                 for (Vampire vampire : vampires) {
  130.                     if (vampire.getX() == x && vampire.getY() == y) {
  131.                         System.out.print("V");
  132.                         isNothing = false;
  133.                         continue;
  134.                     }
  135.                 }
  136.  
  137.                 if (isNothing == true) {
  138.                     System.out.print(".");
  139.                 }
  140.             }
  141.  
  142.             System.out.println("");
  143.         }
  144.     }
  145.  
  146.     // create player and feed it to map
  147.     public void createPlayer(int moves) {
  148.         Player player = new Player(moves);
  149.         dungeonMap.addPlayer(player);
  150.     }
  151.  
  152.     // create a set number of vampires;
  153.     public void createVampires(int vampireCount) {
  154.         List<Vampire> vampires = new ArrayList<Vampire>();
  155.  
  156.         for (int i = 0; i < vampireCount; i++) {
  157.             vampires.add(new Vampire());
  158.         }
  159.  
  160.         dungeonMap.addVampires(vampires); // NOTE: vampires do not have a position yet!
  161.     }
  162.  
  163. }
  164.  
  165. // DungeonMap.java ////////////////////////////////////////////////////////////////
  166.  
  167. package dungeon;
  168.  
  169. import java.util.*;
  170.  
  171. public class DungeonMap {
  172.  
  173. //    private int length;
  174. //    private int height;
  175.     private Player player;
  176.     private List<Vampire> vampires;
  177.     private int xMax;
  178.     private int yMax;
  179.  
  180.     public DungeonMap(int length, int height) {
  181. //        this.length = length;
  182. //        this.height = height;
  183.  
  184.         xMax = length - 1;
  185.         yMax = height - 1;
  186.     }
  187.  
  188.     // one movement
  189.     // how do I check for collisions?
  190.     public void move(char direction) {
  191.         // move player
  192.         if (direction == 'w') {
  193.             player.moveUp();
  194.         }
  195.         if (direction == 's') {
  196.             player.moveDown();
  197.         }
  198.         if (direction == 'a') {
  199.             player.moveLeft();
  200.         }
  201.         if (direction == 'd') {
  202.             player.moveRight();
  203.         }
  204.  
  205.         // move vampires
  206.         // check vampire collisions
  207.        
  208.         // check if player is in same position as vampire
  209.         List<Vampire> deadVampire = new ArrayList<Vampire>();
  210.         for (Vampire vampire : vampires) {
  211.             if (vampire.getX() == player.getX() && vampire.getY() == player.getY()) {
  212.                 deadVampire.add(vampire);
  213.             }
  214.         }
  215.         vampires.removeAll(deadVampire);
  216.        
  217.         // check if all vampires are dead
  218.         if (vampires.isEmpty()) {
  219.             // return win
  220.         }
  221.        
  222.         // check if player's moves reached zero
  223.         if (player.getLamp() == 0) {
  224.             // return lose
  225.         }
  226.        
  227.         // keep playing
  228.     }
  229.  
  230.     public int getXMax() {
  231.         return xMax;
  232.     }
  233.  
  234.     public int getYMax() {
  235.         return yMax;
  236.     }
  237.  
  238.     public int getPlayerLamp() {
  239.         return player.getLamp();
  240.     }
  241.  
  242.     public Player getPlayer() {
  243.         return player;
  244.     }
  245.  
  246.     public List<Vampire> getVampires() {
  247.         return vampires;
  248.     }
  249.  
  250.     public void addPlayer(Player player) {
  251.         this.player = player;
  252.     }
  253.  
  254.     public void addVampires(List<Vampire> vampires) {
  255.         // give vampires position
  256.  
  257.     }
  258.  
  259. }
  260.  
  261. // Player.java ////////////////////////////////////////////////////////////////
  262.  
  263. package dungeon;
  264.  
  265. public class Player extends Character {
  266.  
  267.     private int x;
  268.     private int y;
  269.     private int lamp;
  270.     private int xMax;
  271.     private int yMax;
  272.  
  273.     public Player(int lamp) {
  274.         x = 0;
  275.         y = 0;
  276.         this.lamp = lamp;
  277.     }
  278.  
  279.     public void setBoundaries(int length, int height) {
  280.         xMax = length - 1;
  281.         yMax = height - 1;
  282.     }
  283.  
  284.     public int getX() {
  285.         return x;
  286.     }
  287.  
  288.     public int getY() {
  289.         return y;
  290.     }
  291.  
  292.     public int getLamp() {
  293.         return lamp;
  294.     }
  295.  
  296.     public void moveUp() {
  297.         if (y > 0) {
  298.             y--;
  299.         }
  300.     }
  301.  
  302.     public void moveDown() {
  303.         if (y < yMax) {
  304.             y++;
  305.         }
  306.     }
  307.  
  308.     public  void moveLeft() {
  309.         if (x > 0) {
  310.             x--;
  311.         }
  312.     }
  313.  
  314.     public  void moveRight() {
  315.         if (x < xMax) {
  316.             x++;
  317.         }
  318.     }
  319.  
  320.     public String toString() {
  321.         return "@ " + x + " " + y;
  322.     }
  323.  
  324. }
  325.  
  326. // Vampire.java ////////////////////////////////////////////////////////////////
  327.  
  328. package dungeon;
  329.  
  330. public class Vampire extends Character {
  331.  
  332.     private int x;
  333.     private int y;
  334.     private boolean alive;
  335.     private int xMax;
  336.     private int yMax;
  337.  
  338.     public Vampire() {
  339.         alive = true;
  340.        
  341.     }
  342.    
  343.     public void setBoundaries(int length, int height) {
  344.         xMax = length - 1;
  345.         yMax = height - 1;
  346.     }
  347.  
  348.     public void setPosition(int x, int y) {
  349.         this.x = x;
  350.         this.y = y;
  351.     }
  352.  
  353.     public int getX() {
  354.         return x;
  355.     }
  356.  
  357.     public int getY() {
  358.         return y;
  359.     }
  360.  
  361.     public void moveUp() {
  362.         if (y > 0) {
  363.             y--;
  364.         }
  365.     }
  366.  
  367.     public void moveDown() {
  368.         if (y < yMax) {
  369.             y++;
  370.         }
  371.     }
  372.  
  373.     public void moveLeft() {
  374.         if (x > 0) {
  375.             x--;
  376.         }
  377.     }
  378.  
  379.     public void moveRight() {
  380.         if (x < xMax) {
  381.             x++;
  382.         }
  383.     }
  384.  
  385.     public String toString() {
  386.         return "V " + x + " " + y;
  387.     }
  388. }
  389.  
  390. // Character.java ////////////////////////////////////////////////////////////////
  391.  
  392. package dungeon;
  393.  
  394. public abstract class Character {
  395.  
  396.     private int x;
  397.     private int y;
  398.     private int xMax;
  399.     private int yMax;
  400.  
  401.     public void setBoundaries(int length, int height) {
  402.         xMax = length - 1;
  403.         yMax = height - 1;
  404.     }
  405.  
  406. //    public abstract void move();
  407.     public abstract int getX();
  408.  
  409.     public abstract int getY();
  410.  
  411.     public void moveUp() {
  412.         if (y > 0) {
  413.             y--;
  414.         }
  415.     }
  416.  
  417.     public void moveDown() {
  418.         if (y < yMax) {
  419.             y++;
  420.         }
  421.     }
  422.  
  423.     public void moveLeft() {
  424.         if (x > 0) {
  425.             x--;
  426.         }
  427.     }
  428.  
  429.     public void moveRight() {
  430.         if (x < xMax) {
  431.             x++;
  432.         }
  433.     }
  434.  
  435.     public abstract String toString();
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement