城堡小游戏

来源:互联网 发布:洛阳智网网络和恒凯 编辑:程序博客网 时间:2024/06/08 19:39

可根据提示进行输入,这样会进到相应方位的房间,也可做一些其他的小游戏

import java.util.Scanner;public class Game0 {    private Room currentRoom;    public Game0()    {        createRooms();    }    public void createRooms()    {        Room outside;        Room lobby;        Room bedroom;        Room pub;        Room study;        //创建房间        outside = new Room("城堡外");        lobby = new Room("大堂");        bedroom = new Room("卧室");        study = new Room("书房");        pub = new Room("酒吧");        //给房间设置方位        outside.setExits(null, lobby, study,pub );        lobby.setExits(null, null, null,outside );        bedroom.setExits(null, null, null,study );        pub.setExits(null, outside, null,null);        study.setExits(outside,bedroom, null,null);        currentRoom = outside;    }    private void printWelcome()    {        System.out.println();        System.out.println("欢迎来到城堡,希望您玩的愉快!");        System.out.println("你可以输入help");        System.out.println("现在您在"+ currentRoom);        System.out.println("出口有:");        if(currentRoom.eastExit!=null)            System.out.print("east ");        if(currentRoom.northExit!=null)            System.out.print("north ");        if(currentRoom.southExit!=null)            System.out.print("south ");        if(currentRoom.westExit!=null)            System.out.print("west ");        System.out.println();    }    private void Help()    {        System.out.println("迷路了吗?您可以输入的命令有:bye help go");        System.out.println("例如\t:go east");    }    private void goRoom(String direction)    {        Room nextRoom  = null;        if(direction.equals("east")){            nextRoom =  currentRoom.eastExit;        }        if(direction.equals("south")){            nextRoom =  currentRoom.southExit;        }        if(direction.equals("west"))        {            nextRoom =  currentRoom.westExit;        }        if(direction.equals("north"))        {            nextRoom =  currentRoom.northExit;        }        if(nextRoom == null)        {            System.out.println("没有下一个房间啦");        }        else        {            currentRoom = nextRoom;            System.out.println("您当前位置是:"+ currentRoom);            System.out.println("出口有:");            if(currentRoom.eastExit!=null)                System.out.print("east ");            if(currentRoom.northExit!=null)                System.out.print("north ");            if(currentRoom.southExit!=null)                System.out.print("south ");            if(currentRoom.westExit!=null)                System.out.print("west ");            System.out.println();        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        Game0 game = new Game0();        game.printWelcome();        while(true)        {            String line = in.nextLine();            String[] words = line.split(" ");            if ( words[0].equals("help") ) {                game.Help();            } else if (words[0].equals("go") ) {                game.goRoom(words[1]);            } else if ( words[0].equals("bye") ) {                break;            }        }        System.out.println("谢谢您的光临");        in.close();    }}
public class Room {    public String description;    public Room northExit;    public Room southExit;    public Room eastExit;    public Room westExit;    public Room(String description)     {        this.description = description;    }    public void setExits(Room north, Room east, Room south, Room west)     {        if(north != null)            northExit = north;        if(east != null)            eastExit = east;        if(south != null)            southExit = south;        if(west != null)            westExit = west;    }    public String toString()    {        return description;    }}

若有不到之处,希望海涵,还望提出,以便改进。

原创粉丝点击