#include<stdio.h>
main()
{
int a; //assigns an int value to a
char c;//assigns a char value to c
printf("Welcome to someones adventure game, props to who wrote it in batch!/n");
printf(" This is a text-based game, meaning there are no graphics./n");
printf(" Type 1 to play and type 2 to play the game:/n");
scanf("%d", a;
if (a = 2){//starts if loop and initializes a
printf("Commands you can use/n e for east/nw for west/ns for south/nn for north/nx to exit/n");
}//ends if
else{//starts else
printf("You are in a cave. There are tunnels going north and east./n");
}//ends else
scanf("%c", c;//assigns a value to c
switch (c){
case 'e':
printf("You were attacked by bats and killed. Start over/n");
break;
case 's':
printf("You cannot go that way./n");
break;
case 'w':
printf("You cannot go that way./n");
break;
case 'n':
printf("You chose the correct path. You are out of the cave. You are standing in an open field. Where do you go?/n");
break;
case 'x':
return 0;
break;
}
Sharmz wrote:I thought your game was really cool so I tried to write it in C because I needed a project to practice, I haven't come close
to mastering C yet so could someone look at my code so far and tell me how I can improve.
- Code: Select all
#include<stdio.h>
main()
{
int a; //assigns an int value to a
char c;//assigns a char value to c
printf("Welcome to someones adventure game, props to who wrote it in batch!/n");
printf(" This is a text-based game, meaning there are no graphics./n");
printf(" Type 1 to play and type 2 to play the game:/n");
scanf("%d", a;
if (a = 2){//starts if loop and initializes a
printf("Commands you can use/n e for east/nw for west/ns for south/nn for north/nx to exit/n");
}//ends if
else{//starts else
printf("You are in a cave. There are tunnels going north and east./n");
}//ends else
scanf("%c", c;//assigns a value to c
switch (c){
case 'e':
printf("You were attacked by bats and killed. Start over/n");
break;
case 's':
printf("You cannot go that way./n");
break;
case 'w':
printf("You cannot go that way./n");
break;
case 'n':
printf("You chose the correct path. You are out of the cave. You are standing in an open field. Where do you go?/n");
break;
case 'x':
return 0;
break;
}
char *field[][] = {
{ null, null, null, null, null },
{ null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
{ null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
{ null, "You fell off a cliff", "You fell off a cliff", "You win", null },
{ null, null, null, null, null }
};
int xPosition = 2;
int yPosition = 2;
char direction;
while (direction != 'x') {
printf("Where do you want to go? 'x' to quit\n");
scanf("%c", &direction);
switch (direction) {
case 'n':
if (field[xPosition][xPosition - 1] == null) printf("You cannot go there\n");
else {
yPosition--;
printf("%s\n", field[xPosition][yPosition]);
}
break;
case 'e': case 'w': case 's': //appropriate stuff like 'n'
case 'x':
printf("Ok, quitting.\n");
break;
}
}
BhaaL wrote:It would probably make sense to add a 2-dimensional (or pseudo-2-dimensional is enough actually) array with predefined answers.
- Code: Select all
char *field[][] = {
{ null, null, null, null, null },
{ null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
{ null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
{ null, "You fell off a cliff", "You fell off a cliff", "You win", null },
{ null, null, null, null, null }
};
int xPosition = 2;
int yPosition = 2;
And then simply loop, and see where you can go:
- Code: Select all
char direction;
while (direction != 'x') {
printf("Where do you want to go? 'x' to quit\n");
scanf("%c", &direction);
switch (direction) {
case 'n':
if (field[xPosition][xPosition - 1] == null) printf("You cannot go there\n");
else {
yPosition--;
printf("%s\n", field[xPosition][yPosition]);
}
break;
case 'e': case 'w': case 's': //appropriate stuff like 'n'
case 'x':
printf("Ok, quitting.\n");
break;
}
}
You could extend that loop to allow more commands, like starting over ('r', resetting x/yPosition), or even make it read whole words/sentences, to make a more/less "real" parser game out of it (like "go north", "take red flower" etc)
Sharmz wrote:Can you please explain your code a little?, I'm new to C and haven't really covered arrays yet. This is a really good way to learn. Thanks a lot for your time & patience with me
//physically, an array is nothing more than a series of variables.
//another way to show the following array would be:
//char *field_0_0 = null;
//char *field_0_1 = null;
//...
//char *field_2_2 = "A red flower blooms here";
//etc.
//it simply groups together stuff that belongs together logically.
char *field[][] = {
{ null, null, null, null, null },
{ null, "Bats, you just died", "A red flower blooms here", "Nothing here?", null },
{ null, "Nothing here?", "A red flower blooms here", "Nothing here?", null },
{ null, "You fell off a cliff", "You fell off a cliff", "You win", null },
{ null, null, null, null, null }
};
//variables for later, keep track where we are.
//if the field value is null, we hit a wall.
int xPosition = 2;
int yPosition = 2;
char direction; //char the user entered, one of n s w e
while (direction != 'x') {
printf("Where do you want to go? 'x' to quit\n");
scanf("%c", &direction);
//%c reads a character, &direction because we need
//the address of it (look into pointers for more info)
switch (direction) {
case 'n':
//check the field at yPosition - 1 (which is north of it. imagine that array as coordinate system spanning left/down)
if (field[xPosition][yPosition - 1] == null) printf("You cannot go there\n");
else {
//field is not null, lets move there. yPosition-- decrements yPosition by one.
yPosition--;
printf("%s\n", field[xPosition][yPosition]);
}
break;
case 'e': case 'w': case 's': //appropriate stuff like 'n'
case 'x':
printf("Ok, quitting.\n");
break;
}
}
Users browsing this forum: No registered users and 0 guests