2014-10-30 2 views
-2

나는 플레이어가 4 개의 물건을 순서대로 수집하여 그것을 다시 만들 수있는 텍스트 기반 어드벤처 게임을 만들어야합니다. 플레이어는 대상을 수집하는 데 20 개의 이동 만 있지만 4 개가 모두 생기면 5 개의 이동 만 가능합니다.텍스트 기반 어드벤처 게임 (scala)

수집 된 개체를 추적하는 방법과 이동을 추적하는 방법으로 혼란 스럽습니다.

이것은 이미 코딩 한 것입니다 : 당신이 architecture를 시작하는 것 같은

/********* 
* 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

그것은 소리. 이 문제에 접근하는 좋은 방법은 모델 게임을 별도로 수행하는 것입니다. 어떻게하면 디스플레이 게임을 사용자에게 전달하고, 사용자가 입력을 읽는 방법과 별도로을 읽는 것입니다. 이것에 대한 대중적인 아키텍처는 MVC이지만 다른 것들도 있습니다.

전략은 기본적으로 다음과 같이 진행됩니다

  1. 당신은, 유형을 통해 설명 할 수있는 방법의 던전을 생각 할 수 있습니까?

    • 사용자의 입력 방법 수정 않습니다
    • 당신은 게임이 사용자에 대해 "알"을 (. 즉, 상태) 다음

자신에게 필요한 항목을 설명하는 방법을 생각할 수 게임의 상태?

  • 상태가 변경되면 사용자에게 표시되는 내용을 어떻게 업데이트합니까?
  • 게임은 일반적으로 입력, 업데이트 게임 상태, 다음 플레이어에게 프레젠테이션을 업데이트 읽는 게임 루프가 있습니다. 현재 방과 같은 "알아야"하는 것, 플레이어의 배낭에있는 것 등을 모델링했다면 (예를 들어 생성 된 클래스), 그 상태를 업데이트 할 수 있습니다.

    그런데 NESW 방향을 사용하는 방식으로 연결된 객실은 실제로 디 그래프입니다. 일반적으로 그래프 및 게임 프로그래밍에 대해 자세히 학습 할 수있는 훌륭한 사이트는 http://www.redblobgames.com/입니다.

    +0

    +1 빨강 머리 글자로 연결하는 경우. 이 블로그는 매우 유용합니다. 항상 정확하고 정확하며 읽기 쉽습니다. – Nate

    관련 문제