该样例工程包含以下几个类
- Game
- Room
- Parser
- Command
- CommandWords
Game类:包含游戏的主要逻辑和数据结构
Room类:游戏的场景,被划分为一个个房间
Parser类:从终端获取用户的输入,将输入解析为系统理解的命令
Command类:玩家输入被转换成命令发送到系统,系统根据命令做出响应
CommandWords类:保存有效的命令
classDiagram
class Parser {
-CommandWords commands
-Scanner reader
+getCommand() Command
}
class CommandWords{
-String VALID_COMMANDS
+isCommand(aString) boolean
+showAll()
}
class Command{
-String commandWord
-String secondWord
+getCommandWord() String
+getSecondWord() String
+isUnknown() boolean
+hasSecondWord() boolean
}
class Room{
-String description
-HashMap~String,Room~ exits
+setExit(direction, neighbour)
+getShortDescription() String
+getLongDescription() String
+getExitString() String
+getExit(direction) Room
}
class Game{
-Parser paser
-Room currentRoom
+createRooms()
+play()
+printWelcome()
+processCommand(command) boolean
+printHelp()
+goRoom(command)
+quit(command)
}
Parser ..> CommandWords
Game ..> Parser
Game ..> Room
Game ..> Command
Parser ..> Command
原本该方法通过多个else if识别command,调用合适的处理方法,这样非常不易添加命令,破坏了封装性。
将Game的处理命令的方法抽出来成为类,使用Java的反射机制,通过类名获取类GoCommand,HelpCommand,QuitCommand的实例,这些类统一实现ICommand接口,这样就可以通过execute()方法调用各自的实现。
改进后的类图:
classDiagram
class Parser {
-CommandWords commands
-Scanner reader
+getCommand() Command
}
class CommandWords{
-String VALID_COMMANDS
+isCommand(aString) boolean
+showAll()
}
class Command{
-String commandWord
-String secondWord
+getCommandWord() String
+getSecondWord() String
+isUnknown() boolean
+hasSecondWord() boolean
}
class Room{
-String description
-HashMap~String,Room~ exits
+setExit(direction, neighbour)
+getShortDescription() String
+getLongDescription() String
+getExitString() String
+getExit(direction) Room
}
class Game{
-Parser paser
-Room currentRoom
+createRooms()
+play()
+printWelcome()
+processCommand(command) boolean
}
class ICommand{
<<interface>>
execute(game) boolean
}
class GoCommand {
+execute(game) boolean
}
class HelpCommand {
+execute(game) boolean
}
class QuitCommand{
+execute(game) boolean
}
Parser ..> CommandWords
Game ..> Parser
Game ..> Room
Game ..> Command
Parser ..> Command
Command <|-- GoCommand
Command <|-- HelpCommand
Command <|-- QuitCommand
ICommand <|.. GoCommand
ICommand <|.. HelpCommand
ICommand <|.. QuitCommand
添加了类LookCommand, Item,给Room添加了新的属性
classDiagram
class Item{
-String name
-String description
-float weight
}
class LookCommand{
+execute(game) boolean
}
class Room{
-String description
-HashMap~String,Room~ exits
-List~Item~ itemList
+setExit(direction, neighbour)
+getShortDescription() String
+getLongDescription() String
+getExitString() String
+getExit(direction) Room
+getItemList() List~Item~
+addItem(item)
}
添加了BackCommand类,给Game类添加了lastRoom属性和相关方法
classDiagram
class BackCommand{
+execute(game) boolean
}
class Game{
-Parser paser
-Room currentRoom
-Room lastRoom
+createRooms()
+play()
+printWelcome()
+processCommand(command) boolean
}
添加了Portal类,给Game添加了portals属性
classDiagram
class Portal{
-Room triggerRoom
-Room targetRoom
}
class Game{
-Parser paser
-Room currentRoom
-Room lastRoom
-List~Portal~ portals
+createRooms()
+play()
+printWelcome()
+processCommand(command) boolean
+addPortal(portal)
}
5.在游戏中新建一个独立的Player类用来表示玩家,一个玩家对象应该保存玩家的姓名等基本信息,也应该保存玩家当前所在的房间,玩家可以随身携带任意数量的物件,但随身物品的总重量不能操过某个上限值,在游戏中增加两个新的命令“take”和“drop”,使得玩家可以拾取房间内的指定物品或丢弃身上携带的某件或全部物品,当拾取新的物件时超过了玩家可携带的重量上限,系统应给出提示;在游戏中增加一个新的命令“items”, 可以打印出当前房间内所有的物件及总重量,以及玩家随身携带的所有物件及总重量;在某个或某些房间中增加一个物件,并增加一个“eat”命令,如果玩家找到并吃掉它,就可以增长玩家的负重能力
添加了TakeCommand, DropCommand, ItemsCommand, EatCommand, Player类
classDiagram
class Player{
-String name
-Room currentRoom
-float maxWeight
-float weight
-List~Item~ itemList
+hasItem(name) boolean
+getItem(item)
+loseItem(item)
}
class TakeCommand{
execute(game) boolean
}
class DropCommand{
execute(game) boolean
}
class ItemsCommand{
execute(game) boolean
}
class EatCommand{
execute(game) boolean
}
添加了Constraint, UseCommand类
Constraint类用于给门增加”锁“机制。
classDiagram
class Constraint{
-Room from
-Room to
}
class UseCommand{
execute(game) boolean
}
查看项目根目录