Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main.java ////////////////////////////////////////////////////////////////
- package dungeon;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- // new Dungeon(10,10,5,14,true).run();
- Dungeon dungeon = new Dungeon(10, 10, 5, 14, true);
- Player player = new Player(14);
- List<Vampire> vampires = new ArrayList<Vampire>();
- Vampire vampire1 = new Vampire();
- vampire1.setBoundaries(10, 10);
- vampire1.setPosition(1, 2);
- vampires.add(vampire1);
- Vampire vampire2 = new Vampire();
- vampire2.setBoundaries(10, 10);
- vampire2.setPosition(7, 8);
- vampires.add(vampire2);
- Vampire vampire3 = new Vampire();
- vampire3.setBoundaries(10, 10);
- vampire3.setPosition(7, 5);
- vampires.add(vampire3);
- Vampire vampire4 = new Vampire();
- vampire4.setBoundaries(10, 10);
- vampire4.setPosition(8, 0);
- vampires.add(vampire4);
- Vampire vampire5 = new Vampire();
- vampire5.setBoundaries(10, 10);
- vampire5.setPosition(2, 9);
- vampires.add(vampire5);
- dungeon.printMap(player, vampires);
- }
- }
- // Dungeon.java ////////////////////////////////////////////////////////////////
- package dungeon;
- import java.util.*;
- public class Dungeon {
- private Scanner reader;
- private DungeonMap dungeonMap;
- private int length;
- private int height;
- private int vampires;
- private int moves;
- private boolean vampiresMove;
- public Dungeon(int length, int height, int vampires, int moves, boolean vampiresMove) {
- reader = new Scanner(System.in);
- this.length = length;
- this.height = height;
- this.vampires = vampires;
- this.moves = moves;
- this.vampiresMove = vampiresMove;
- }
- public void run() {
- dungeonMap = new DungeonMap(length, height);
- createPlayer(moves);
- createVampires(vampires);
- // gameplay loop
- while (true) {
- printDisplay();
- System.out.println("");
- String input = reader.nextLine();
- // loop through movement input
- for (int i = 0; i < input.length(); i++) {
- dungeonMap.move(input.charAt(i));
- }
- }
- }
- public void printDisplay() {
- // player's lamp
- System.out.println(dungeonMap.getPlayerLamp());
- System.out.println("");
- // player's position
- System.out.println(dungeonMap.getPlayer());
- // vampire's positions
- for (Vampire vampire : dungeonMap.getVampires()) {
- System.out.println(vampire);
- }
- System.out.println("");
- // print map
- printMap(dungeonMap.getPlayer(), dungeonMap.getVampires());
- }
- public void printMap(Player player, List<Vampire> vampires) {
- for (int y = 0; y < dungeonMap.getYMax() + 1; y++) {
- for (int x = 0; x < dungeonMap.getXMax() + 1; x++) {
- boolean isNothing = true;
- // if the player is at this position, print "@"
- if (player.getX() == x && player.getY() == y) {
- System.out.print("@");
- isNothing = false;
- continue;
- }
- // loop through vampires. if a vampire is at this position, print "V"
- for (Vampire vampire : vampires) {
- if (vampire.getX() == x && vampire.getY() == y) {
- System.out.print("V");
- isNothing = false;
- continue;
- }
- }
- if (isNothing == true) {
- System.out.print(".");
- }
- }
- System.out.println("");
- }
- }
- // create player and feed it to map
- public void createPlayer(int moves) {
- Player player = new Player(moves);
- dungeonMap.addPlayer(player);
- }
- // create a set number of vampires;
- public void createVampires(int vampireCount) {
- List<Vampire> vampires = new ArrayList<Vampire>();
- for (int i = 0; i < vampireCount; i++) {
- vampires.add(new Vampire());
- }
- dungeonMap.addVampires(vampires); // NOTE: vampires do not have a position yet!
- }
- }
- // DungeonMap.java ////////////////////////////////////////////////////////////////
- package dungeon;
- import java.util.*;
- public class DungeonMap {
- // private int length;
- // private int height;
- private Player player;
- private List<Vampire> vampires;
- private int xMax;
- private int yMax;
- public DungeonMap(int length, int height) {
- // this.length = length;
- // this.height = height;
- xMax = length - 1;
- yMax = height - 1;
- }
- // one movement
- // how do I check for collisions?
- public void move(char direction) {
- // move player
- if (direction == 'w') {
- player.moveUp();
- }
- if (direction == 's') {
- player.moveDown();
- }
- if (direction == 'a') {
- player.moveLeft();
- }
- if (direction == 'd') {
- player.moveRight();
- }
- // move vampires
- // check vampire collisions
- // check if player is in same position as vampire
- List<Vampire> deadVampire = new ArrayList<Vampire>();
- for (Vampire vampire : vampires) {
- if (vampire.getX() == player.getX() && vampire.getY() == player.getY()) {
- deadVampire.add(vampire);
- }
- }
- vampires.removeAll(deadVampire);
- // check if all vampires are dead
- if (vampires.isEmpty()) {
- // return win
- }
- // check if player's moves reached zero
- if (player.getLamp() == 0) {
- // return lose
- }
- // keep playing
- }
- public int getXMax() {
- return xMax;
- }
- public int getYMax() {
- return yMax;
- }
- public int getPlayerLamp() {
- return player.getLamp();
- }
- public Player getPlayer() {
- return player;
- }
- public List<Vampire> getVampires() {
- return vampires;
- }
- public void addPlayer(Player player) {
- this.player = player;
- }
- public void addVampires(List<Vampire> vampires) {
- // give vampires position
- }
- }
- // Player.java ////////////////////////////////////////////////////////////////
- package dungeon;
- public class Player extends Character {
- private int x;
- private int y;
- private int lamp;
- private int xMax;
- private int yMax;
- public Player(int lamp) {
- x = 0;
- y = 0;
- this.lamp = lamp;
- }
- public void setBoundaries(int length, int height) {
- xMax = length - 1;
- yMax = height - 1;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public int getLamp() {
- return lamp;
- }
- public void moveUp() {
- if (y > 0) {
- y--;
- }
- }
- public void moveDown() {
- if (y < yMax) {
- y++;
- }
- }
- public void moveLeft() {
- if (x > 0) {
- x--;
- }
- }
- public void moveRight() {
- if (x < xMax) {
- x++;
- }
- }
- public String toString() {
- return "@ " + x + " " + y;
- }
- }
- // Vampire.java ////////////////////////////////////////////////////////////////
- package dungeon;
- public class Vampire extends Character {
- private int x;
- private int y;
- private boolean alive;
- private int xMax;
- private int yMax;
- public Vampire() {
- alive = true;
- }
- public void setBoundaries(int length, int height) {
- xMax = length - 1;
- yMax = height - 1;
- }
- public void setPosition(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void moveUp() {
- if (y > 0) {
- y--;
- }
- }
- public void moveDown() {
- if (y < yMax) {
- y++;
- }
- }
- public void moveLeft() {
- if (x > 0) {
- x--;
- }
- }
- public void moveRight() {
- if (x < xMax) {
- x++;
- }
- }
- public String toString() {
- return "V " + x + " " + y;
- }
- }
- // Character.java ////////////////////////////////////////////////////////////////
- package dungeon;
- public abstract class Character {
- private int x;
- private int y;
- private int xMax;
- private int yMax;
- public void setBoundaries(int length, int height) {
- xMax = length - 1;
- yMax = height - 1;
- }
- // public abstract void move();
- public abstract int getX();
- public abstract int getY();
- public void moveUp() {
- if (y > 0) {
- y--;
- }
- }
- public void moveDown() {
- if (y < yMax) {
- y++;
- }
- }
- public void moveLeft() {
- if (x > 0) {
- x--;
- }
- }
- public void moveRight() {
- if (x < xMax) {
- x++;
- }
- }
- public abstract String toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement