2014-10-30 275 views
-2

我必须制作一个基于文本的冒险游戏,玩家必须按顺序收集四个对象,并将其返回到10号房间才能构建它。玩家只有20次移动来收集物体,但只要他们有四次移动,他们只有5次移动。scala基于文本的冒险游戏

我很困惑如何跟踪收集的对象以及如何跟踪移动。

这是我已经编码:

/********* 
* getRequest: 
* Prompts the user for a move 
* and returns the string entered 
*********/ 
def getRequest(): String = { 
    println("Where do you go now?") 
    readLine() 
} 

/********* 
* printHelp: 
* help menu with basic instructions 
*********/ 
def printHelp() { 
println("N=North, E=East, S=South and W=West") 
} 


/********* 
* processSpecialCommand: 
* Processes the special commands: H, Q (only) 
*********/ 
def processSpecialCommand(req: String) { 
if (req == "H") 
    printHelp 
else if (req == "Q") { 
    println("I can't believe you are giving up. Are you afraid of Perry?") 
    println("Oh well, maybe another day then.") 
    sys.exit(1) // This quits the program immediately (aka- abort) 
} else { 
    println("What are you saying?") 
    println("Use 'H' for help.") 
    } 
} 

/*** Room 1: Foyer (E=6, S=2, W=3) ***/ 
def room1() { 
// Print the message 
println() 
println("------") 
println("Room 1") 
println("------") 
println(" Ah, the foyer. I probably should call it a lobby") 
println(" but foyer just sounds so much better. Don't you agree?") 
println("There are doors to the East, South, and West") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    println("You cannot go there.") 
    return room1() // Go back to room 1 
    case "E" => 
    // Go to room 6 
    return room6() 
    case "S" => 
    // Go to room 2 
    return room2() 
    case "W" => 
    // Go to room 3 
    return room3() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room1() // Go back to room 1 
    } 
} 

/*** Room 2: (N=1, W=4, S=7) ***/ 
def room2() { 
    // Print the message 
    println() 
    println("------") 
    println("Room 2") 
    println("------") 
    println("There are doors to the North, South, and West") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 1 
    return room1() // Go to room 1 
    case "E" => 
    println("You cannot go there.") 
    return room2() // Go back to room 2 
    case "S" => 
    // Go to room 7 
    return room7() 
    case "W" => 
    // Go to room 4 
    return room4() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room2() // Go back to room 2 
} 
} 

/*** Room 3: (E=1, S=4) ***/ 
def room3() { 
    // Print the message 
    println() 
    println("------") 
    println("Room 3") 
    println("------") 
    println("You found piece number 4!!!") 
    println("There are doors to the East and South") 

//if you have pieces 1,2 and 3 you can collect this piece else this part cannot be collected yet 


// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    println("You cannot go there.") 
    return room3() // Go back to room 3 
    case "E" => 
    // Go to room 1 
    return room1() 
    case "S" => 
    // Go to room 4 
    return room4() 
    case "W" => 
    println("You cannot go there.") 
    return room3() // Go back to room 3 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room3() // Go back to room 3 
    } 
} 

/*** Room 4: (N=3, E=2) ***/ 
def room4() { 
// Print the message 
    println() 
    println("------") 
    println("Room 4") 
    println("------") 
    println("You found piece number 2!!!") 
    println("There are doors to the North and East") 

//if you have piece number 1 you can collect this piece else this part cannot be collected yet 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 3 
    return room3() 
    case "E" => 
    // Go to room 2 
    return room2() 
    case "S" => 
    println("You cannot go there.") 
    return room4() // Go back to room 4 
    case "W" => 
    println("You cannot go there.") 
    return room4() // Go back to room 4 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room4() // Go back to room 4 
} 
} 

/*** Room 5: (N=6, S=8) ***/ 
def room5() { 
// Print the message 
println() 
println("------") 
println("Room 5") 
println("------") 
println("You found piece number3!!!") 
println("There are doors to the North and South") 

//if you have pieces 1 and 2 you can collect this piece else this part cannot be collected yet 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 6 
    return room6() 
    case "E" => 
    println("You cannot go there.") 
    return room5() 
    case "S" => 
    // Go to room 8 
    return room8() 
    case "W" => 
    println("You cannot go there.") 
    return room5() // Go back to room 5 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room5() // Go back to room 5 
} 
} 

/*** Room 6: (E=9, S=5, W=1) ***/ 
def room6() { 
// Print the message 
println() 
println("------") 
println("Room 6") 
println("------") 
println("There are doors to the East, South and West") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    println("You cannot go there.") 
    return room6() 
    case "E" => 
    // Go to room 9 
    return room9() 
    case "S" => 
    // Go to room 5 
    return room5() 
    case "W" => 
    //Go to room 1 
    return room1() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room6() // Go back to room 6 
    } 
} 

/*** Room 7: (N=2, E=8) ***/ 
def room7() { 
// Print the message 
println() 
println("------") 
println("Room 7") 
println("------") 
println("There are doors to the North and East") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 2 
    return room2() 
    case "E" => 
    // Go to room 8 
    return room8() 
    case "S" => 
    println("You cannont go there.") 
    return room7() 
    case "W" => 
    println("You cannont go there.") 
    return room7() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room7() // Go back to room 7 
    } 
} 

/*** Room 8: (N=5, E=10, W=7) ***/ 
def room8() { 
// Print the message 
println() 
println("------") 
println("Room 8") 
println("------") 
println("There are doors to the North, East and West") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 5 
    return room5() 
    case "E" => 
    // Go to room 10 
    return room10() 
    case "S" => 
    println("You cannont go there.") 
    return room8() 
    case "W" => 
    // Go to room 7 
    return room7() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room8() // Go back to room 8 
} 
} 

/*** Room 9: (S=10, W=6) ***/ 
def room9() { 
// Print the message 
println() 
println("------") 
println("Room 9") 
println("------") 
println("You found piece number 1!!!") 
println("There are doors to the South and West") 

//collect the first piece 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    println("You cannot go there.") 
    return room9() 
    case "E" => 
    println("You cannot go there.") 
    return room9() 
    case "S" => 
    // Go to room 10 
    return room10() 
    case "W" => 
    // Go to room 6 
    return room6() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room9() // Go back to room 9 
} 
}  

/*** Room 10: (N=9, W=8) ***/ 
def room10() { 
// Print the message 
println() 
println("------") 
println("Room 10") 
println("------") 
println("There are doors to the North and West") 

// Get and process the request (moving on to the next state/room) 
val move = getRequest.toUpperCase 
move match { 
    case "N" => 
    // Go to room 9 
    return room9() 
    case "E" => 
    println("You cannot go there.") 
    return room10() 
    case "S" => 
    println("You cannot go there.") 
    return room10() 
    case "W" => 
    // Go to room 8 
    return room8() 
    case cmd => 
    // Maybe it is a special request (Help or Quit) 
    processSpecialCommand(cmd) 
    return room10() // Go back to room 10 
} 
} 

回答

3

这听起来像你开始使用architecture。解决这个问题的一个好方法就是改为模型这个游戏分开,从你如何显示这个游戏给用户,分别从你如何读取用户的输入。这个流行的体系结构是MVC,但也有其他的。

的策略基本上是这样的:

  1. 你能想到的方式来描述,通过类型,地牢?
  2. 你能想到的一种方式来描述游戏需要“知道”用户是什么(即状态。)

然后问自己:

  • 如何将用户的输入修改游戏的状态?
  • 如何更新用户在状态更改时看到的内容?

游戏通常具有读取输入,更新游戏状态,然后更新演示给玩家的游戏循环。一旦你为你需要“知道”的东西建模(例如创建类),就像当前的房间,玩家的背包里有什么,等等,然后你就可以更新这个状态。

顺便说一句,以您使用NESW方向的方式连接在一起的房间实际上是对角线。一个了解更多关于图形和游戏编程的好地方是http://www.redblobgames.com/

+0

+1链接到redblobgames。该博客非常有用;它总是正确/准确和易于阅读。 – Nate 2014-10-30 14:30:45