text game? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

text game?

progr2progr2 Posts: 2Member
How can I do a simple text adventure? (Pascal)



Comments

  • zibadianzibadian Posts: 6,349Member
    : How can I do a simple text adventure? (Pascal)
    :
    :
    :
    :
    The most basic version has an array of room descriptions, an integer indicating the current room, and a repeat-until loop which looks much like this:
    [code]
    repeat
    {Give room description}
    {User enters command}
    if UserCommand = 'North' then
    case RoomIndex of {All rooms with north exit}
    1: RoomIndex := 5; {go to room 5}
    else {Error message: cannot go there};
    end
    else if UserCommand = 'South' then
    case RoomIndex of {All rooms with south exit}
    5: RoomIndex := 1; {go to room 1}
    6: if Room6DoorOpen then {example of a door}
    RoomIndex := 2
    else
    {Error message: cannot go there};
    else {Error message: cannot go there};
    end
    else {etc}
    else
    {Error message: Unknown command}
    until {EndGame};
    [/code]
    This allows the user to walk through a maze. You can introduce additional variables to simulate doors, as shown in the example.
    A slightly more advanced method uses an additional 2D-array to describe the connections. This changes the "walking" if-thens to:
    [code]
    if UserCommand = 'North' then
    begin
    if Conenctions[RoomIndex, 1] = 0 then
    {Error message: cannot go there}
    else
    RoomIndex := Conenctions[RoomIndex, 1];
    end else
    [/code]
    I chose the variable names pretty much at random. The code given does not include any user-defined functions or procedures, and some parts need to be written by you. I hope this gives you an idea.
Sign In or Register to comment.