城堡小游戏
来源:互联网 发布:洛阳智网网络和恒凯 编辑:程序博客网 时间: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; }}
若有不到之处,希望海涵,还望提出,以便改进。
阅读全文
0 0
- 城堡小游戏
- java的学习笔记——一个城堡小游戏
- 城堡
- 城堡
- 城堡
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 小游戏
- 一篇转自大神的关于23种设计模式的文档
- text mining and analytics学习笔记week2
- 自动调参(GridSearchCV)及数据降维(PCA)在人脸识别中的应用
- 使用myecplise连接sqlserver数据库的所见异常及实现细则
- 真正的高手很爱写文章
- 城堡小游戏
- vmware的三种网络模式
- 用 Handler 轻松实现专属Android定时器
- 程序员应该掌握的省市联动
- 如何批量修改sql中某个字段中的特定信息
- 总结Qt5调用windows本地程序的三个方法以及异同
- 关于plsql远程连接oracle,以及连接64位oracle处理方法
- 面向初学者的XCP——测量/校准协议是什么?
- AVL树的基本操作