框架加数据

  • 把数据的硬编码尽可能解成框架加数据的结构
  • 城堡游戏修改后的代码
    •   Room类
    •  1 package com.castle;
       2 
       3 import java.util.HashMap;
       4 
       5 public class Room {
       6     private String description;
       7     private HashMap<String, Room> exits=new HashMap<String, Room>();
       8     
       9 
      10     public Room(String description) 
      11     {
      12         this.description = description;
      13     }
      14 
      15     public String getEixtDesc()
      16     {
      17         StringBuffer sb=new StringBuffer();
      18         for (String dir : exits.keySet())
      19         {
      20             sb.append(dir+" ");
      21         }
      22         return sb.toString();
      23     }
      24     
      25     public void setExit(String dir,Room room)
      26     {
      27         exits.put(dir, room);
      28     }
      29     
      30 
      31     public Room getExit(String direction)
      32     {
      33         return exits.get(direction);
      34     }
      35     
      36     @Override
      37     public String toString()
      38     {
      39         return description;
      40     }
      41 }

       

    • Game类
    •   1 package com.castle;
        2 
        3 import java.util.HashMap;
        4 import java.util.Scanner;
        5 
        6 public class Game {
        7     private Room currentRoom;
        8         
        9     private HashMap<String, Handler> handlers=new HashMap<String, Handler>();
       10     public Game() 
       11     {
       12         handlers.put("go", new HandlerGo(this));
       13         handlers.put("Bye", new HandlerBye(this));
       14         handlers.put("Help", new HandlerHelp(this));
       15         createRooms();
       16     }
       17 
       18     private void createRooms()
       19     {
       20         Room outside, lobby, pub, study, bedroom;
       21       
       22         //    制造房间
       23         outside = new Room("城堡外");
       24         lobby = new Room("大堂");
       25         pub = new Room("小酒吧");
       26         study = new Room("书房");
       27         bedroom = new Room("卧室");
       28         
       29         //    初始化房间的出口
       30         outside.setExit("east", lobby) ;
       31         outside.setExit("south", study) ;
       32         outside.setExit("west", pub) ;
       33         
       34         lobby.setExit("west", outside) ;
       35         
       36         pub.setExit("east", outside) ;
       37         
       38         study.setExit("north", outside) ;
       39         study.setExit("east", bedroom) ;
       40         
       41         bedroom.setExit("west", study) ;
       42         
       43         lobby.setExit("up", pub);
       44         pub.setExit("down", lobby);
       45         
       46        
       47 
       48         currentRoom = outside;  //    从城堡门外开始
       49     }
       50 
       51     private void printWelcome() {
       52         System.out.println();
       53         System.out.println("欢迎来到城堡!");
       54         System.out.println("这是一个超级无聊的游戏。");
       55         System.out.println("如果需要帮助,请输入 'help' 。");
       56         System.out.println();
       57         showPrompt();
       58         
       59     }
       60 
       61     // 以下为用户命令
       62 
       63   
       64 
       65     public void showPrompt()
       66     {
       67         System.out.println("你在" + currentRoom);
       68         System.out.print("出口有: ");
       69         System.out.println(currentRoom.getEixtDesc());
       70         System.out.println();
       71     }
       72     
       73     public void goRoom(String direction) 
       74     {
       75         Room nextRoom = currentRoom.getExit(direction);
       76         
       77         if (nextRoom == null) {
       78             System.out.println("那里没有门!");
       79         }
       80         else {
       81             currentRoom = nextRoom;
       82             showPrompt();
       83         }
       84     }
       85     
       86     public void play()
       87     {
       88         Scanner in = new Scanner(System.in);
       89         while ( true ) {
       90             String line = in.nextLine();
       91             String[] words = line.split(" ");
       92             Handler handler=handlers.get(words[0]);
       93             String value="";
       94             if(words.length>1)
       95                 value=words[1];
       96             if(handler !=null) 
       97             {
       98                 handler.doCmd(value);
       99                 if(handler.isBye())
      100                     break;
      101             }
      102 //            if ( words[0].equals("help") ) {
      103 //                printHelp();
      104 //            } else if (words[0].equals("go") ) {
      105 //                goRoom(words[1]);
      106 //            } else if ( words[0].equals("bye") ) {
      107 //                break;
      108 //            }
      109         }
      110         in.close();
      111     }
      112     
      113     public static void main(String[] args) {
      114         Game game = new Game();
      115         game.printWelcome();
      116         game.play();
      117         System.out.println("感谢您的光临。再见!");
      118     }
      119 
      120 }

       

    • Handler类及其子类
    •  1 package com.castle;
       2 
       3 public class Handler
       4 {
       5 
       6     protected Game game;
       7     
       8     
       9     
      10     public Handler(Game game)
      11     {
      12         
      13         this.game = game;
      14     }
      15     public void doCmd(String word) {}
      16     public boolean isBye() {return false;}
      17     public static void main(String[] args)
      18     {
      19         // TODO Auto-generated method stub
      20 
      21     }
      22 
      23 }
      24 
      25 
      26 
      27 package com.castle;
      28 
      29 public class HandlerBye extends Handler
      30 {
      31 
      32     public HandlerBye(Game game)
      33     {
      34         super(game);
      35         // TODO Auto-generated constructor stub
      36     }
      37 
      38     @Override
      39     public boolean isBye()
      40     {
      41         // TODO Auto-generated method stub
      42         return true;
      43     }
      44 
      45 }
      46 
      47 
      48 
      49 
      50 package com.castle;
      51 
      52 public class HandlerHelp extends Handler
      53 {
      54 
      55     public HandlerHelp(Game game)
      56     {
      57         super(game);
      58         // TODO Auto-generated constructor stub
      59     }
      60 
      61     @Override
      62     public void doCmd(String word)
      63     {
      64         System.out.print("迷路了吗?你可以做的命令有:go bye help");
      65         System.out.println("如:\tgo east");
      66     }
      67 
      68 }
      69 
      70 
      71 
      72 
      73 package com.castle;
      74 
      75 public class HandlerGo extends Handler
      76 {
      77 
      78     public HandlerGo(Game game)
      79     {
      80         super(game);
      81         // TODO Auto-generated constructor stub
      82     }
      83 
      84     @Override
      85     public void doCmd(String word)
      86     {
      87         // TODO Auto-generated method stub
      88         game.goRoom(word);
      89     }
      90 
      91 }

       

posted on 2020-02-07 20:17  生长的力量  阅读(133)  评论(0编辑  收藏  举报