Reputation: 61
So I'm making this text based game for my 'C' Programming class. I want to continue it even after I'm done with the class so it's not just a homework assignment I want to actually keep it for later use.
My question is in this part of the code is there a way to send the user back to the previous question asked when using an 'IF' statement so they can make multiple choices before ultimately going the direction of the story?
if (door2 == 1)
{
// what happens when you touch the orb.
printf("The orb transports you to an unknown location.\n");
printf("You look around to get the lay of your area,\n");
printf("off in the distance you see a church like building.");
door2 = door2 - 1;
printf("\n");
printf("Do you go to the building? ");
scanf_s("%d", &door1);
printf("\n");
if (door1 == 1)
{
// Going up to the church.
printf("You walk up to the building, it gives you a strange feeling\n");
printf("in the pit of your stomach. You look around and see two windows\n");
printf("low enough for you to see into along with the main door.\n");
printf("Do you go to the left window? right window? or try to open the front door?\n");
scanf_s("%d", &movement);
printf("\n");
if (movement = 1)
{
// left window.
printf("You creep up to the window and peak inside...\n");
printf("You see a large figure pacing back and forth in the room\n");
printf("what ever it is spots you and darts further into the church.");
movement = movement - 1;
// i subtract the L,S,R numbers to insure there is not an infinite loop.
}
else if (movement = 2)
{
// front door.
printf("You walk up to the front door, and try to open it.\n");
printf("The door does not budge, it must be locked.");
movement = movement - 2;
}
else if (movement = 3)
{
// right window.
printf("You creep up to the window and find an empty room.\n");
movement = movement - 3;
printf("The window is cracked open somewhat... Do you try to enter the\n");
printf("church through the window? ");
scanf_s("%d", &door2);
printf("\n");
}
Upvotes: 1
Views: 506
Reputation: 16540
each location in the game needs an entry in a table,
along with certain environment details such as:
which direction is the player facing
visible items at each location
current game state,
current inventory,
'next' table entry to use, etc.
This table grows huge very quickly and can have many parameters per line in the table. It is the actual game. All the rest is just user manipulation of the table, current conditions, etc.
Every possible user input has a unique function to handle that input (there are not that many possible inputs).
Each of these functions gets a parameter that is a pointer to an entry in the location table, that lets that function process the user input.
The main loop of the program handles getting input from the user and dispatching the appropriate inputFunction handler.
There will be sub functions to handle inventory changes, display inventory, enable/disable certain types of actions, etc
Upvotes: 0
Reputation:
This is probably not the correct approach at all. A game generally is split into several components: a main loop (that runs infinitely), an update loop (that simulates the game world) and a process input function (which takes input from the user.)
The way your code is structured also leaves much to be desired. For one thing, your prompts to the user are inconsistent. Sometimes you seem to be asking a yes or no question, other times it seems you want a movement command with no indication to the user which is expected. This is a UX nightmare. Instead, you can treat each movement as an action. For example, going forward to a locked door means the user wants to try to open the door. Going forward with an open window means the user wants to travel through the window. With that in mind, it becomes clear that you only actually need to keep track of one variable.
Making a game is not an easy task and you cannot brute force your way into making one - it requires thoughtful planning ahead of time. Try drawing a dialogue tree on a piece of paper. You'll quickly see it spiral out of control as you run out of room on the paper - designing a game is a complex task. It's important to have a blueprint before you begin coding.
First, let's tackle what your game loop can look like:
#include <stdio.h>
#include <stdbool.h> // for true and false
typedef enum { NONE = 0, LEFT, RIGHT, UP, DOWN } movement_t;
int main()
{
movement_t movement = NONE;
movement_t last_movement = NONE;
while (true)
{
int movement_input = 0;
scanf("%d", &movement_input);
movement = (movement_t) movement_input;
// ...
last_movement = movement;
}
}
Now you want to know what kind of data structure should represent your game. Unfortunately this is a complex topic and I'm not a game developer, so I'll try to present a naive approach. The simplest way that takes less work is to have a static message for each room as you enter it. If something changes, then you could reflect that change once a variable has been updated.
void church()
{
printf("Generic church description.");
if (some_player_did_something)
{
printf("You now spot a shiny object on the floor.");
}
}
Keeping track of the player's last movement (which I have demonstrated above) is useful for determining where they came from.
if (last_movement == LEFT)
{
printf("You come in from the left and noticed something you haven't before...");
}
I would take the suggestions from the others and split your code into functions (as I'm sure your instructor has drilled into you by now.) A monolithic approach is not the way to go.
Upvotes: 2
Reputation: 654
Actually yes, if you would just put a loop around that, but i'd advise splitting your ifs in functions. Your code will look much better.
void main() {
enterTheCurch();
}
void enterTheCurch() {
printf("You've entered the Curch. There are 2 Doors. Do you want to go left or right?");
String direction;
scanf_s("%s", &direction);
if(direction == "left") {
goLeftCurchDoor();
}
else {
goRightCurchDoor();
}
}
goLeftCurchDoor() {
...
}
goRightCurchDoor() {
...
}
Try using this structure. Like this you also can give parameters to the function like bool alreadyVisited
and change the prints depending on these parameters.
Upvotes: 2
Reputation: 13414
There is one: goto
.
label:
if (someCondition) {
goto label;
}
This code will loop until someCondition
becomes false.
goto
is a inconditional jump, and allow you to jump excecution forward or backward to a label, provided you stay in the same scope.
Lots of people hates goto
, and you should make sure not to use it if you have other better alternatives.
Story telling sounds like a nice use case for goto
.
Loops or recursion are other options to produce this result, such as while
do ... while
for
.
Upvotes: 0