A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

定义多个房间,房间之间,有的可以互相通过,有的不能通过,房间中有家具。
通过show rooms,可以查看当前房间可以通往的房间;
通过show furnitures,可以查看当前房间的家具;
通过talk 家具名,可以打印显示家具的信息。
我以前见过的这个题,具体是怎么描述的我也忘记了,姑且就这样实现吧。
[Java] 纯文本查看 复制代码
import java.util.ArrayList;
import java.util.Scanner;
class Game { 
	public static void main(String[] args){ 
		Room now = init();
		play(now);
	}
	public static Room init(){ 
		Room roomA = new Room("roomA");
		roomA.add(new Table("茶几"));
		roomA.add(new Chair("木凳"));
		roomA.add(new Bed("席梦思"));

		Room roomB = new Room("roomB");
		roomB.add(new Table("方桌"));
		roomB.add(new Chair("铁櫈"));
		roomB.add(new Bed("木床"));
		
		Room roomC = new Room("roomC");
		roomC.add(new Table("八仙桌"));
		roomC.add(new Chair("铁櫈1"));
		roomC.add(new Chair("铁櫈2"));
		roomC.add(new Chair("铁櫈3"));
		roomC.add(new Chair("铁櫈4"));

		connect(roomA,roomB);
		connect(roomC,roomB);

		return roomA;
	}
	public static void connect(Room roomA, Room roomB){ 
		roomA.add(roomB);
		roomB.add(roomA);
	}
	public static void play(Room room){ 
		if ( room == null ) { 
			System.out.println("未知的房间.");
			return;
		}
		Scanner sc = new Scanner(System.in);
		for ( boolean loopFlag = true ; loopFlag ;  ) { 
			System.out.print("please enter your command : ");
			String msg = sc.nextLine();
			switch ( msg ) { 
			case "":
				break;
			case "show rooms":
				room.showRooms();
				break;
			case "show furnitures":
				room.showFurnitures();
				break;
			case "exit":
				System.out.println("游戏将结束!");
				loopFlag = false;
				break;
			case "help":
				System.out.println("------ help start ------");
				System.out.println("help : 获取帮助");
				System.out.println("exit : 退出游戏");
				System.out.println("talk : 激活物件的说话功能,比如: talk 茶几");
				System.out.println("go : 前往另一个房间,比如: go roomB");
				System.out.println("show rooms : 获取可以前往的房间");
				System.out.println("show furnitures : 获取可以激活说话功能的物件");
				System.out.println("------ help end ------");
				break;
			default:
				if ( msg.startsWith("go ") ) { 
					String nextRoomName = msg.substring(2).trim();
					Room next = room.getRoomByName(nextRoomName);
					if ( next != null ) { 
						room = next;
						System.out.println("now in Room : " + room.getName());
					} else { 
						System.out.println("can not go to the room : " + nextRoomName);
					}
					continue;
				} else if ( msg.startsWith("talk ") ) { 
					String furnitureName = msg.substring(5).trim();
					Furniture furniture = room.getFurnitureByName(furnitureName);
					if ( furniture != null ) { 
						furniture.talk();
					} else { 
						System.out.println("have no furniture : " + furnitureName);
					}
					continue;
				}
				System.out.println("不支持的命令: " + msg);
				break;
			}
		}
	}
}
abstract class Thing { 
	private String name;
	public Thing () {}
	public Thing(String name){ 
		this.name = name;
	}
	public void setName(String name){ 
		this.name = name;
	}
	public String getName(){ 
		return name;
	}
}
class Room extends Thing { 
	private ArrayList<Furniture> furnitures;
	private ArrayList<Room> rooms;
	public Room() {}
	public Room(String name){ 
		super(name);
	}
	
	private int indexOf(Room room){ 
		if ( rooms == null || room == null ) { 
			return -1;
		}
		for ( int i = 0 ; i < rooms.size() ; i ++ ) { 
			if ( rooms.get(i) == room ) { 
				return i;
			}
		}
		return -1;
	}
	private boolean had(Room room){ 
		return indexOf(room)!=-1;
	}
	private int indexOf(Furniture furniture){ 
		if ( furnitures == null || furniture == null ) { 
			return -1;
		}
		for ( int i = 0 ; i < furnitures.size() ; i ++ ) { 
			if ( furnitures.get(i) == furniture ) { 
				return i;
			}
		}
		return -1;
	}
	private boolean had(Furniture furniture){ 
		return indexOf(furniture)!=-1;
	}
	public boolean add(Room room){ 
		if ( room == null ) { 
			return false;
		}
		if ( rooms == null ) { 
			rooms = new ArrayList<Room>();
		}
		if ( !had(room) ) { 
			rooms.add(room);
			return true;
		}
		return false;
	}
	public boolean remove(Room room){ 
		return rooms.remove(room);
	}
	public boolean add(Furniture furniture){ 
		if ( furniture == null ) { 
			return false;
		}
		if ( furnitures == null ) { 
			furnitures = new ArrayList<Furniture>();
		}
		if ( !had(furniture) ) { 
			furnitures.add(furniture);
			return true;
		}
		return false;
	}
	public boolean remove(Furniture furniture){ 
		return furnitures.remove(furniture);
	}
	public Room getRoomByName(String name){ 
		if ( name == null ) { 
			return null;
		}
		for ( int i = 0 ; i < rooms.size() ; i ++ ) { 
			if ( name.equals( rooms.get(i).getName() ) ) { 
				return rooms.get(i);
			}
		}
		return null;
	}
	public Furniture getFurnitureByName(String name){ 
		if ( name == null ) { 
			return null;
		}
		for ( int i = 0 ; i < furnitures.size() ; i ++ ) { 
			if ( name.equals( furnitures.get(i).getName() ) ) { 
				return furnitures.get(i);
			}
		}
		return null;
	}
	public void showRooms(){ 
		Room room;
		for ( int i = 0 ; i < rooms.size() ; i ++ ) { 
			room = rooms.get(i);
			System.out.println("Room : " + i + " --> " + room.getName());
		}
		System.out.println("----------------------");
	}
	public void showFurnitures(){ 
		Furniture furniture;
		for ( int i = 0 ; i < furnitures.size() ; i ++ ) { 
			furniture = furnitures.get(i);
			System.out.println("Furniture : " + i + " --> " + furniture.getName());
		}
		System.out.println("----------------------");
	}
}
interface ITalk { 
	public abstract void talk();
}
abstract class Furniture extends Thing implements ITalk { 
	public Furniture() {}
	public Furniture(String name){ 
		super(name);
	}
	public abstract void talk();
}
class Table extends Furniture { 
	public Table() {}
	public Table(String name){ 
		super(name);
	}
	public void talk(){ 
		System.out.println("我是"+getName()+",我很快乐!");
	}
}
class Chair extends Furniture { 
	public Chair() {}
	public Chair(String name){ 
		super(name);
	}
	public void talk(){ 
		System.out.println("我是"+getName()+",我讨厌人类!");
	}
}
class Bed extends Furniture { 
	public Bed() {}
	public Bed(String name){ 
		super(name);
	}
	public void talk(){ 
		System.out.println("我是"+getName()+",我又被睡了!");
	}
}
class Cup extends Furniture { 
	public Cup() {}
	public Cup(String name) { 
		super(name);
	}
	public void talk(){ 
		System.out.println("我是"+getName()+",我爱泡茶!");
	}
}

3 个回复

倒序浏览
支持顶一下
回复 使用道具 举报
可以,新手很形象
回复 使用道具 举报 1 0

谢谢,一起加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马