Java学习日记DAY19

城堡小游戏

package castle;

import java.util.HashMap;

public class Room {
    private String description;
//    private Room northExit;
//    private Room southExit;
//    private Room eastExit;
//    private Room westExit;
    private HashMap<String, Room> exit=new HashMap<String, Room>();

    public Room(String description) 
    {
        this.description = description;
    }

    public void setExits(String dir,Room room) 
    {
    	exit.put(dir, room);
//        if(north != null)
//            northExit = north;
//        if(east != null)
//            eastExit = east;
//        if(south != null)
//            southExit = south;
//        if(west != null)
//            westExit = west;
    }
    public String getExitDesc() {
    	StringBuffer sb=new StringBuffer();
//    	if(northExit!=null) {
//            sb.append("north ");
//        }
//    	if(eastExit!=null) {
//            sb.append("east ");
//        }
//    	if(westExit!=null) {
//            sb.append("west ");
//        }
//    	if(southExit!=null) {
//            sb.append("south ");
//        }
    	for(String s:exit.keySet()) {
    		sb.append(s+" ");
    	}
    	return sb.toString();
    }
    
    public Room getNext(String direction) 
    {
//        Room nextRoom = null;
//        if(direction.equals("north")) {
//            nextRoom = northExit;
//        }
//        if(direction.equals("east")) {
//            nextRoom = eastExit;
//        }
//        if(direction.equals("south")) {
//            nextRoom = southExit;
//        }
//        if(direction.equals("west")) {
//            nextRoom =westExit;
//        }


//        return nextRoom;
        return exit.get(direction);
      
    }

    @Override
    public String toString()
    {
        return description;
    }
}

package castle;

import java.util.HashMap;
import java.util.Scanner;

public class Game {
    private Room currentRoom;
    
    private HashMap<String, Handler> handlers=new HashMap<String, Handler>();
        
    public Game() 
    {
    	handlers.put("go",new HandlerGo(this));
    	handlers.put("bye", new HandlerBye(this));
    	handlers.put("help", new HandlerHelp(this));
        createRooms();
    }

    private void createRooms()
    {
        Room outside, lobby, pub, study, bedroom;
      
        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");
        
        //	初始化房间的出口
//        outside.setExits(null, lobby, study, pub);
//        lobby.setExits(null, null, null, outside);
//        pub.setExits(null, outside, null, null);
//        study.setExits(outside, bedroom, null, null);
//        bedroom.setExits(null, null, null, study);
        outside.setExits("east", lobby);
        outside.setExits("south", study);
        outside.setExits("west", pub);
        lobby.setExits("west", outside);
        pub.setExits("east", outside);
        study.setExits("north", outside);
        study.setExits("east", bedroom);
        bedroom.setExits("east", study);
        
        currentRoom = outside;  //	从城堡门外开始
    }

    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡!");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助,请输入 'help' 。");
        System.out.println();
        showNext();
    }

    // 以下为用户命令

//    private void printHelp() 
//    {
//        System.out.print("迷路了吗?你可以做的命令有:go bye help");
//        System.out.println("如:\tgo east");
//    }

    public void goRoom(String direction) 
    {
    	 Room nextRoom=null;
    	 nextRoom=currentRoom.getNext(direction);
    	  
//        Room nextRoom = null;
//        if(direction.equals("north")) {
//            nextRoom = currentRoom.northExit;
//        }
//        if(direction.equals("east")) {
//            nextRoom = currentRoom.eastExit;
//        }
//        if(direction.equals("south")) {
//            nextRoom = currentRoom.southExit;
//        }
//        if(direction.equals("west")) {
//            nextRoom = currentRoom.westExit;
//        }
//
        if (nextRoom == null) {
            System.out.println("那里没有门!");
        }
        else {
            currentRoom = nextRoom;
            showNext();
            
        }
    }
    public void showNext() {
    	System.out.println("你在" + currentRoom);
        System.out.print("出口有: ");
        
        System.out.println(currentRoom.getExitDesc());
    }
    public void play() {
    	
    	Scanner in = new Scanner(System.in);
    	while ( true ) {
    		
    		String line = in.nextLine();
    		String[] words = line.split(" ");
    		Handler handler=handlers.get(words[0]);
    		String value="";
    		if(words.length>1)
    			value=words[1];
    		if(handler!=null) {
    			handler.doCMD(value);
    			if(handler.isBye()) {
    				break;
    			}
    		}
//    		if ( words[0].equals("help") ) {
//    			printHelp();
//    		} else if (words[0].equals("go") ) {
//    			goRoom(words[1]);
//    		} else if ( words[0].equals("bye") ) {
//    			break;
//    		}
    		
       }
    	 
    	in.close();
    }
	
	public static void main(String[] args) {
		
		Game game = new Game();
		game.printWelcome();
		game.play();

        
        System.out.println("感谢您的光临。再见!");
       
	}

}

package castle;

public class Handler {
	protected Game gam;
	public Handler(Game game) {
		this.gam=game;
		
	}
	public void doCMD(String word) {
		
	}
	public boolean isBye() {
		return false;
	}

}

package castle;

public class HandlerBye extends Handler {
	public HandlerBye(Game game) {
		super(game);
	}
	

	@Override
	public boolean isBye() {
		return true;
	}
	

}
package castle;

public class HandlerGo extends Handler {
	
	public HandlerGo(Game game) {
		super(game);
		
	}

	@Override
	public void doCMD(String word) {
		gam.goRoom(word);
		
	}
	

}

package castle;

public class HandlerHelp extends Handler {
	public HandlerHelp(Game game) {
		super(game);
	}
		

	@Override
	public void doCMD(String word) {
		System.out.println("迷路了吗?你可以做的命令有:go bye help");
        System.out.println("如:\tgo east");
	}
	

}

猜你喜欢

转载自blog.csdn.net/qq_32591735/article/details/88088409