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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# text game?

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

• 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.