城堡游戲
我們在嘗試了之前的簡單媒體庫構造之后,試著整合一下之前學到的關于類,繼承,多型等知識,制作一個簡單的城堡游戲,城堡游戲是一個簡單的文字游戲,通過輸入命令可以在地圖上不同的房間進行移動,
一、城堡游戲介紹:
1.這個程式的任務是通過玩家的輸入的方向(純文字)在虛構的城堡內移動(以純文字作為移動后的回傳結果),
2.這個程式接受help、bye、go south、go north、go west、go east六種命令,要求命令單獨一行輸入并在結束時敲回車,另外如果接受go xxx的不合規資訊會輸出不存在這樣的房間,
3.help提供幫助資訊,bye結束游戲,go后面空一格加south、north、west、east表示在虛構的城堡中移動,
4.有五個地點,分別是:小酒吧,大廳,書房,臥室,次臥,
5.地圖:

二、類的設計
首先我們要有三個類,第一個是Game類,用來表示游戲本身和執行各種游戲命令,第二個是Room類,用來表示游戲中的所有房間,可見整個城堡游戲只由這兩個類構成,具體怎么運行就要先構造這兩個大類,
1.Game類
Game類是整個游戲運行的核心,具有構造房間,開始游戲,執行命令三個功能,
2.Room類
城堡游戲中是由很多個小房間構成的,分別是小酒吧,大堂,書房,臥室,次臥這五個地點,每個房間只有一種屬性,即description(房間名),
三、具體實作
1.類的創建
首先我們要創造一個Game類,用來作為游戲的主體,我們知道Game類有三個功能,構造房間,開始游戲,執行命令,那么在創建好Game類之后,我們再著手從構造房間這個功能開始實作,所以我們要再創建一個Room類,用來表示游戲中的所有房間,
2.Room類
首先創造好Room的description屬性,然后創建構造表,
public String description;
public Room(String description) {
this.description = description;
}
然后我們在Game類中構造createRoom函式,用來創造我們已知的五個房間(酒吧,大廳,書房,臥室,次臥),
private void createRoom(){
Room pub,lobby,study,bedroom,bedroom1;
}
在創建好房間之后,我們要開始考慮5個房間的連通性,也就是五個房間分別的四個方向的出口通往哪個房間,我們在Room類中創建setExit函式來完成,同時我們需要在Room類中先創建4個物件,分別表示當前房間通往的下一個房間,當setExit函式得到null時,說明該房間的對應方向不存在新的房間,
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 south,Room west,Room east){
if ( north != null )
northExit = north;
if ( south != null )
southExit = south;
if ( west != null )
westExit = west;
if ( east != null )
eastExit = east;
}
3.構造房間
接下來我們在Game類中創造五個房間,并初始化五個房間分別對應的出口,同時創建一個Room物件currentRoom,用來表示初始房間,并將初始房間設定為小酒館,
private Room currentRoom;
private void createRoom(){
Room pub,lobby,study,bedroom,bedroom1;
//創造房間
pub = new Room("小酒館");
lobby = new Room("大廳");
study = new Room("書房");
bedroom = new Room("臥室");
bedroom1 = new Room("次臥");
//設定出口
pub.setExits(null,null,null,lobby);
lobby.setExits(null,study,pub,bedroom1);
study.setExits(lobby,null,null,bedroom);
bedroom.setExits(null,null,study,null);
bedroom1.setExits(null,null,lobby,null);
currentRoom = pub;
}
4.游戲設計
構造好房間之后,就可以開始設計游戲的運行邏輯了,游戲分為開始游戲,游戲程序,游戲結束三個部分,我們來一個一個完成,
①開始游戲
首先我們需要在開始游戲的時候介紹一下城堡游戲,然后輸出初始化的地點,再執行游戲的具體操作,
public void printWelcome(){
System.out.println("***********************************");
System.out.println("歡迎來到城堡游戲!");
System.out.println("這是一個超級無聊的小游戲,");
System.out.println("如果需要幫助,請輸入help,");
System.out.println("那么我們開始游戲吧!");
System.out.println("***********************************");
System.out.println("現在你在"+currentRoom);
}
這里要注意到的是,我們直接輸出currentRoom物件的話只能得到null結果,要想得到currentRoom中的description字串的話,必須要使用toString方法,在Room類設定一個toString函式,來回傳description值,
public String toString()
{
return description;
}


可以看到我們在開始游戲的介紹中加入了一個help可以得到游戲幫助的小命令,用來提示玩家可以進行怎樣的操作,那么我們現在開始設計這個help,
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("那里沒有門!");
}
}
構造goRoom函式,用來表示當我們收到一個前往下個方向的命令時,創造一個nextRoom物件,將相對應方向的房間的物件交給它,以此來實作從一個房間到下一個對應房間的移動,同時我們考慮到當玩家不按常理出牌,如果某個當前房間并沒有對應方向的房間的時候,玩家輸入這個方向,我們要輸出“那里沒有門!”,然后將其他方向的房間說明給玩家,
else{
currentRoom = nextRoom;
System.out.println("你在"+ currentRoom);
System.out.println("出口有:");
if(currentRoom.northExit != null)
System.out.println("north");
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();
}
然后我們在Game類的main函式中設計get命令的while回圈,設定一個死回圈,不停地獲取命令,知道得到bye命令時結束回圈,并結束游戲,
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("***********************************");
System.out.println("游戲到此結束!");
System.out.println("歡迎下次光臨!");
System.out.println("謝謝!");
System.out.println("***********************************");
四、代碼測驗

五、詳細代碼
1.Game:
package castlegame;
import java.util.Scanner;
public class Game {
private Room currentRoom;
public Game()
{
createRooms();
}
private void createRooms(){
Room pub,lobby,study,bedroom,bedroom1;
//創造房間
pub = new Room("小酒館");
lobby = new Room("大廳");
study = new Room("書房");
bedroom = new Room("臥室");
bedroom1 = new Room("次臥");
//設定出口
pub.setExits(null,null,null,lobby);
lobby.setExits(null,study,pub,bedroom1);
study.setExits(lobby,null,null,bedroom);
bedroom.setExits(null,null,study,null);
bedroom1.setExits(null,null,lobby,null);
currentRoom = pub;
}
public void printWelcome(){
System.out.println("***********************************");
System.out.println("歡迎來到城堡游戲!");
System.out.println("這是一個超級無聊的小游戲,");
System.out.println("如果需要幫助,請輸入help,");
System.out.println("那么我們開始游戲吧!");
System.out.println("***********************************");
System.out.println("現在你在"+currentRoom);
}
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("north");
if(currentRoom.eastExit != null)
System.out.println("east");
if(currentRoom.southExit != null)
System.out.println("south");
if(currentRoom.westExit != null)
System.out.println("west");
}
}
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("***********************************");
System.out.println("游戲到此結束!");
System.out.println("歡迎下次光臨!");
System.out.println("謝謝!");
System.out.println("***********************************");
}
}
2.Room:
package castlegame;
public class Room {
public String description;
public Room northExit;
public Room southExit;
public Room westExit;
public Room eastExit;
public Room(String description) {
this.description = description;
}
public void setExits(Room north,Room south,Room west,Room east){
if ( north != null )
northExit = north;
if ( south != null )
southExit = south;
if ( west != null )
westExit = west;
if ( east != null )
eastExit = east;
}
public String toString()
{
return description;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/224785.html
標籤:java
