⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 castle.java

📁 龙与地下城游戏(小型)的设计与实现。主要是随机产生地牢
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package dungeonsanddragons.model;import dungeonsanddragons.game.GameFrame;import dungeonsanddragons.model.monsters.DevilMonster;import dungeonsanddragons.model.monsters.DragonMonster;import dungeonsanddragons.model.monsters.PacmanMonster;import dungeonsanddragons.model.monsters.Bb;import dungeonsanddragons.model.weapons.Bow;import dungeonsanddragons.model.weapons.D;import dungeonsanddragons.model.weapons.Raygun;import dungeonsanddragons.model.weapons.Sword;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Random;import java.util.Stack;import javax.imageio.ImageIO;/** * Class representing the whole castle with rooms and items in the rooms * @author Sandra Nilsson */public class Castle {    /**The Rooms of this castle*/    private Room[][] rooms;    /**THe image representing the current rooms*/    private Image castleImage;        /**Where to temporary save the image of the current rooms*/    private File imageFile = new File("castle.jpg");         /**Those two is used only for generation of the board*/    private boolean[][] visited;    private int totalVisited;    /**     * Create a new castle*/    public Castle() {        try {            rooms = new Room[GameFrame.SIZE][GameFrame.SIZE];            //randomize the rooms            randomizeCastle();            //Build the image showing the randomized rooms            buildCastleImg();        } catch (IOException ex) {            System.out.println("Castle image could not be rendered");            ex.printStackTrace();        }    }    /**     *      * @return the Multidimensional fields of Rooms representing this castle     */    public Room[][] getRooms() {        return rooms;    }    /**     *      * @return the room where the game shall start     */    public Room getStartRoom() {        return rooms[rooms.length - 1][0];    }    /**     * Set the game course. Randomize the labyrinth of rooms.     */    private void randomizeCastle() {        totalVisited = 0;        visited = new boolean[GameFrame.SIZE][GameFrame.SIZE];                //Generate Rooms for the whole board        for (int r = 0; r < rooms.length; r++) {            for (int c = 0; c < rooms.length; c++) {                Room room = new Room(c * GameFrame.ROOM_SIZE, r * GameFrame.ROOM_SIZE, r, c);                rooms[r][c] = room;                visited[r][c] = false;            }        }        Random rand = new Random();        //Start at a random cell in the grid        int row = rand.nextInt(GameFrame.SIZE);        int col = rand.nextInt(GameFrame.SIZE);        Room currentRoom = rooms[row][col];        Stack<Room> s = new Stack<Room>();        //Continue untill all rooms are visited        while (totalVisited < (GameFrame.SIZE * GameFrame.SIZE)) {            //Find an nieghbour not visited already            Room nextRoom = findNext(currentRoom);            if (nextRoom == null) {                //No neighbour found, Go back                       currentRoom = s.pop();            } else {                //Save the current room on the stack                s.push(currentRoom);                //Go to next room                currentRoom = nextRoom;            }        }        //Create the exit door        rooms[0][GameFrame.SIZE - 1].setNorth(new Exit(rooms[0][GameFrame.SIZE - 1]));        //Open the board up a little bit more        randomizeSomeDoors();    }    /**     * Finds a randomly choosen neightbour room that is not already visited     * @param currentRoom the room to find neighbour to     * @return a neighbour room, null if no unvisited neighbour found     */    private Room findNext(Room currentRoom) {        int row = currentRoom.getRow();        int col = currentRoom.getCol();        if (!visited[row][col]) {            //If this room was not visited before, then it is the first time and we have to count that            totalVisited++;            visited[row][col] = true;        }        //Find all neighbours not visited        ArrayList<Room> n = new ArrayList<Room>();        ArrayList<String> direction = new ArrayList<String>();        //Check the north        if (row > 0) {            if (!visited[row - 1][col]) {                n.add(rooms[row - 1][col]);                direction.add("n");            }        }        //Check the east        if (col < GameFrame.SIZE - 1) {            if (!visited[row][col + 1]) {                n.add(rooms[row][col + 1]);                direction.add("e");            }        }        //Check the south        if (row < GameFrame.SIZE - 1) {            if (!visited[row + 1][col]) {                n.add(rooms[row + 1][col]);                direction.add("s");            }        }        //Check the west        if (col > 0) {            if (!visited[row][col - 1]) {                n.add(rooms[row][col - 1]);                direction.add("w");            }        }        if (n.size() == 0) {            //All neighbours visited already            return null;        }        Random rand = new Random();        //Find a random neighbourh that is not visited (now saved in the list).         int neigh = rand.nextInt(n.size());        String dir = direction.get(neigh);        //Open up a door between them        Room next = n.get(neigh);        Door d = new Door(currentRoom);        d.setR2(next);        if (dir.equals("n")) {            //North door randomly picked            currentRoom.setNorth(d);            next.setSouth(d);        } else if (dir.equals("e")) {            //East room randomly picked            currentRoom.setEast(d);            next.setWest(d);        } else if (dir.equals("s")) {            //South room randomly picked            currentRoom.setSouth(d);            next.setNorth(d);        } else if (dir.equals("w")) {            //West room randomly picked            currentRoom.setWest(d);            next.setEast(d);        }        //Return the room found        return next;    }    /**     * Randomize some extra doors     */    private void randomizeSomeDoors() {        Random rand = new Random();        //Randomize 5 south doors        int row = rand.nextInt(GameFrame.SIZE - 1);        int col = rand.nextInt(GameFrame.SIZE);        int counter = 0;        boolean success = false;        while (!success && counter <= 5) {            //Check if that door not already exists            if (rooms[row][col].getSouth() == null) {                success = true;                counter++;                Door d = new Door(rooms[row][col]);                d.setR2(rooms[row + 1][col]);                rooms[row][col].setSouth(d);                rooms[row + 1][col].setNorth(d);            }            row = rand.nextInt(GameFrame.SIZE - 1);            col = rand.nextInt(GameFrame.SIZE);            success = false;        }        //Randomize 4 east doors        row = rand.nextInt(GameFrame.SIZE);        col = rand.nextInt(GameFrame.SIZE - 1);        counter = 0;        success = false;        while (!success && counter <= 4) {            //Check if that door not already exists            if (rooms[row][col].getEast() == null) {                success = true;                counter++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -