2014-09-04 36 views
1

我已經研究過這個,到目前爲止沒有找到我在這裏找到的網站。我或多或少都在嘗試一種半基本的基於文​​本的冒險類遊戲,其中玩家選擇諸如(l)ook,(w)ander,(s)leep等選項。我創建了類爲房間,並已初始化它,其餘的和描述等設置。在其中一個選項中,你進入一個洞穴,而且我還沒有從其他人和我的老師那裏學到足夠的知識,以允許描述你所在的區域進行改變。迄今爲止的代碼行數如下:如何更改使用的房間

public void Gameloop(Room startRoom) 
{ 
    String input; 
    boolean notDead = false; 

    Room currentRoom = startRoom; 

    while(!notDead) 
    { 
     //Shows description of the room 
    input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What do you do? \n(l)ook around\n(g)rab a stick\n(s)leep."); 

這是用於在森林中進行遊戲的開始位置。說明正確顯示,通過選擇的進步是正確的。問題是與代碼這個領域

if(input.equalsIgnoreCase("l")) 
    { 
     input = JOptionPane.showInputDialog(null,"You see trees and a cave. Do you want to go into the casve? \n(y)es \n(n)o"); 
     if(input.equalsIgnoreCase("y")) 
     { 

      input = JOptionPane.showInputDialog(null,currentRoom.getDescription() + " Do you want to set up a camp?\n(y)es\n(n)o"); 

的問題特別是,我還沒有學會如何實現空間變化可言,否則,遊戲基礎是完善的,做,能,期權會比較好深思熟慮並且物品系統稍後會實施。基本上,我該如何改變這個人所在的「房間」。注意,這個遊戲沒有圖形用戶界面,它的字面意思是基於你爲每個動作輸入一個字母

+1

'currentRoom = cave;'' – 2014-09-04 15:52:46

回答

0

我會在這裏使用策略模式。我會給你一些僞代碼來讓你開始:),注意這沒有編譯。 這個想法是你製作了一個IRoom接口。這將使您可以根據需要製作多種不同類型的房間。每個房間都可以定義這個房間中可能的操作,例如,您可以將某些操作添加到森林房間。 它也有確保房間執行動作的方法。這可能會導致一個空間變化,因此它會返回一個新的IRoom。

 public class Game { 
      private IRoom currentRoom; 

      public void Gameloop(IRoom startRoom){ 

      while(!notDead) 
      { 
      //Shows description of the room 
      input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What do you do? " + dissplayPossibleActionsToUser(startRoom.getPossibleActionsInRoom())); 

      Action chosenAction = dericeActionFromImput(input); 
      currentRoom = startRoom.performAction(chosenAction); 


      } 

     } 

    } 

     public interface IRoom{ 
      //Returns all the actions the current room provides 
      List<Actions> getPossibleActionsInRoom(); 

      //Does the action 
      IRoom performAction(Action action); 
     } 

     public enum Actions{ 
      LOOK, WANDER, SLEEP; 
     } 


      public class Forest implements IRoom{ 

       public List<Actions> getPossibleActionsInRoom(){ 
        return new ArrayList(Actions.wander); 
       } 

       public IRoom performAction(Action action){ 
        if(action == wander){ 
         return new HouseRoom(); 
        } 
       } 
      }