浙江大学翁恺老师的城堡游戏源代码

许多小伙伴们可能因为课程关闭无法查看课件了,所以下面我来给出翁恺老师的源代码

Game.java中的:

package castle;

import java.util.*;

public class Game {
	private Room currentRoom;

	public Game() {
  		createRooms();
 	}

	public 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(outside,bedroom,null,null);
  		bedroom.setExits(null,null,null,study);
   
  		currentRoom = outside;        //从城堡门外开始
 	}

	private void printWelcome()
 	{
  		System.out.println();
  		System.out.println("欢迎来到城堡!");
  		System.out.println("这是一个超级无聊的游戏。");
  		System.out.println("如果需要帮助,请输入'help'");
  		System.out.println();
  		System.out.println("现在你在:" + currentRoom);
  		System.out.println("欢迎来到城堡");
  		System.out.println("出口有:");
  		if(currentRoom.northExit !=null)
   			System.out.print("north ");
  		if(currentRoom.eastExit !=null)
   			System.out.print("east ");
  		if(currentRoom.southExit !=null)
   			System.out.print("south ");
  		if(currentRoom.westExit !=null)
   			System.out.print("west ");
  		System.out.println();
 	}
 	
//  以下为用户命令
 
 	private void printHelp()
 	{
  		System.out.println("迷路了吗?你可以做的命令有:go bye help");
  		System.out.println("如:\tgo east");
 	}

	private void goRoom(String 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;
   			System.out.println("你在"+ currentRoom);
   			System.out.println("出口有:");
   			if(currentRoom.northExit != null)
    				System.out.println("norh");
   			if(currentRoom.eastExit != null)
    				System.out.println("east");
   			if(currentRoom.southExit != null)
    				System.out.println("south");
   			if(currentRoom.westExit != null)
    				System.out.println("west");  
   			System.out.println();
  		}
 	}

	public static void main(String[] args) {
  		Scanner in = new Scanner(System.in);
  		Game game = new Game();
  		game.printWelcome();
  
  		while (true){
   			String line = in.nextLine();
   			String[] words = line.split(" ");
   			if ( words[0].equals("help")){
    				game.printHelp();
   			}else if ( words[0].equals("go")){
    				game.goRoom(words[1]);
   			}else if ( words[0].equals("bye")){
    				break;
   			}
  		}
  
  		System.out.println("感谢您的光临。再见!");
  		in.close();
 	}
 }

Room.java中的:

package castle;

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 = east;
  		if(west != null)
   			westExit = west;
 	}

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

	public void southExit(Object object, Room lobby, Room study, Room pub) {
  		// TODO Auto-generated method stub
  
 	}
 }	
  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值