Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Game/Project proposal.docx
Binary file not shown.
60 changes: 60 additions & 0 deletions Game/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Final Project

It’s a text adventure game. You play as a rap god trying to collect his rap lyric notes. You win when you rap by yourself in a shack.
I picked a text adventure game to do because it seemed accomplishable and I like playing games.

## Compliation
gcc game.c

## How to play
Type in commands at the >> prompt. Available commands are:
- goto [location] will print out a statement when you move to the room then print out the new room summary
- take [item] will move the item into your inventory
- look - print a look summary
- read note - will display collected notes
- yell - will make you yell. (doesn't do anything unless you're in the yard)
- cook (only in kitchen)
- QUIT - Quits game
- help - prints this

## Differences from proposal
- Player has more variables, like inventory size, has_poo, has_backpack, notes_read.
- never implemented enumerator, just used strings instead
- Room summaries can change, though none of them really do as I put in a separate "room" with a different functionality. Room pointers can point to different rooms too, so you can create multiple rooms with the same room name.
- Commands are each their own individual functions and could be called from the get_args function but I didn't have time to really change this so they're all in main.
- Look in bag not implemented, it just prints inventory all the time as it got cumbersome to try to just look in bag
- there is no Open command as it would have made it unnecessarily complicated
- Go is goto
- Help would be easy to put in, though I feel it was redundant as the game is pretty easy.
- put would have also made the game too complicated.
- there are new room-specific commands like cook and rap that help you to win the game.

## How to win
Use these commands in this order:
- goto bedroom
- take backpack
- take note
- goto yard
- take note
- yell
- goto park
- take note
- take dog poo
- goto yard
- goto kitchen
- cook dog poo
- take note
- take rusty key
- goto yard
- goto shack
- take note
- read note
- rap

Use these commands to lose the game:
- goto bedroom
- take backpack
- goto yard
- goto park
- goto anywhere
alternatively: QUIT or goto QUIT works well.
Binary file added Game/a.out
Binary file not shown.
266 changes: 266 additions & 0 deletions Game/func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "func.h"



void get_args(char* arg1 , char * arg2, char* command) // The input sanitizer, it will return the first arg as the first word, the second as all words after
{
int i = 0, j = 0;
for(;i <= (int) strlen(command); i++) //Iterate through commands
{
arg1[i] = command[i]; //Copy first word to char
if (command[i] == '\n' || command[i] == ' ')//When you reach a space or newline, replace with end char
{
arg1[i] = '\0';
i++;
break;
}
}
for(; command[i] != '\0'; j++, i++) // repeat for second arg
{
arg2[j] = command[i];
if (command[i] == '\n')
{
arg2[j] = '\0';
break;
}
}
}

void print_room_summary(character *playerPtr) // This prints the "HUD" for the player: inventory, items in the room
{
int saw = 0, is_last = 0;
printf("|%s|\n", playerPtr->current_room->room_name);
printf("%s\n\n", playerPtr->current_room->description);

//PRINT ROOM'S ITEMS
printf("I see");
for (int i = 0; i < playerPtr->current_room->items_size; i++)
{
is_last = 1;
for (int j = i+1; j < playerPtr->current_room->items_size; j++)
{
if (playerPtr->current_room->items[j] != NULL) is_last = 0;
}
if (playerPtr->current_room->items[i] == NULL)
;
else if (is_last && saw == 1)
{
printf(" and a %s", playerPtr->current_room->items[i]);
}
else
{
printf(" a %s", playerPtr->current_room->items[i]);
if (playerPtr->current_room->items[i+1] != NULL) printf(",");
saw = 1;
}

}
if (!saw) printf(" nothing here");
printf(".\n");

//PRINT INVENTORY
if (!playerPtr->has_backpack)
{
printf("\n\n");
return;
}
printf("I have: ");
for (int i = 0;i < playerPtr->inventory_size; i++)
{
if (playerPtr->inventory[i] == NULL) continue;
printf("|%s| ", playerPtr->inventory[i]);
}
printf("\n\n");
}
void take(character * playerPtr, char * item, room * roomPtr, int room_items_size) // Transfers an item in the room to inventory. Didn't know where else to put the backpack interaction
{
int slot = 0;
while (playerPtr->inventory[slot] != NULL)
{
slot++; //find a free slot. This might produce a segfault somehow but I have not been running into errors.
}
if (slot+1 == playerPtr->inventory_size) //no free slots
{
printf("\033[2J");
printf("I have no room left in my bag. Might as well just sit here.");
return;
}
for (int i = 0; i < room_items_size; i++)
{
if (playerPtr->current_room->items[i] != NULL) //if the item in the room exists. Just to prevent segfaults with strcmp
{
if(strcmp(playerPtr->current_room->items[i], item) == 0) //if the item we're looking for is in the room
{
if (strcmp(item, "backpack") == 0) //hardcoded backpack interactions
{
playerPtr->has_backpack = 1; //do NOT add to inventory!
playerPtr->current_room->items[i] = NULL; //delete from room
printf("\033[2J");
printf("I can hold things now.\nThere's a note still here! I can [read note].\n\n\n\n");
printf("Press enter to continue.\n");

return;
}
if (playerPtr->has_backpack == 0)
{
printf("I try to pick up the %s, but I hopelessly fumble and drop it.", item);
return;
}
playerPtr->inventory[slot] = playerPtr->current_room->items[i]; //add a copy of the item to inventory
roomPtr->items[i] = NULL; //delete
printf("%s\n", playerPtr->current_room->items[i]); //reprint summary
printf("\033[2J");
print_room_summary(playerPtr);
return;
}
}
}
printf("I can't find a %s here.", item);
}
void moveto(character*playerPtr, char *droom, room * rooms[]) //Moves character to a connected room. Converts strings to rooms first. Also where I put the interactions with rooms
{
int connected = 0, found = 0;
room *to_room;
for (int i = 0;;i++)
{
if (strcmp(rooms[i]->room_name, "END") == 0) //end of rooms list
break;
if (strcmp(droom, rooms[i]->room_name) == 0) //we found a room
{
to_room = rooms[i]; //convert the droom string to a room pointer
found = 1;
}
}
for (int i = 0;;i++)
{
if (found && strcmp(playerPtr->current_room->connected_rooms[i], to_room->room_name) == 0) //check for room in connected_room
{
connected = 1;
break;
}
if (strcmp(playerPtr->current_room->connected_rooms[i], "END") == 0) //If we've reached end of list
{

if (i-1 == 0) //If there's only one connected room
{
moveto(playerPtr, playerPtr->current_room->connected_rooms[0], rooms);//move there instead
return; //cancel original moveto
}
break;
}
}

if (!found) //no rooms found
{
if (strlen(droom) != 0) //if they wanted to move somewhere
printf("I don't know where that is.");

return;
}

if (strcmp(playerPtr->current_room->room_name,droom) == 0) //already in room
{
printf("You're already here.");
return;
}
if (!connected) //room not connected
{
printf("I can't get to the %s from here.", to_room->room_name);
return;
}
if (strcmp(playerPtr->current_room->room_name, "bedroom") == 0 && playerPtr->has_backpack == 0) //hard coded
{
printf("I should [take] my backpack before I go.");
return;
}
if (strcmp(to_room->room_name, "kitchen") == 0) //hard coded
{
for (int i = 0; i < playerPtr->inventory_size; i++)
{
if (playerPtr->inventory[i] != NULL && strcmp(playerPtr->inventory[i], "dog poo") == 0)
playerPtr->has_poo = 1;
}
if (!playerPtr->has_poo)
{
printf("I don't feel like going to the kitchen without something to cook.");
return;
}
}
if (strcmp(to_room->room_name, "shack") == 0) //hard coded again
{
int has_rkey = 0;
for (int i = 0; i < playerPtr->inventory_size; i++)
{
if (playerPtr->inventory[i] != NULL && strcmp(playerPtr->inventory[i], "rusty key") == 0)
has_rkey = 1;
}
if (!has_rkey)
{
printf("I can't get into the shack without a key.");
return;
}
}
playerPtr->current_room = to_room;
printf("\033[2J");
printf("%s\n\n\n\n", playerPtr->current_room->entrance_msg);
printf("Press enter to continue.\n");
}

void look(character* playerPtr) // Prints look statement. This function was not needed honestly
{
printf("\033[2J");
printf("%s\n\n\n\n", playerPtr->current_room->look_msg);
printf("Press enter to continue.\n");
}
void note(character* playerPtr) // The note system. I could've just iterated but I already had the switch statements set up
{
for (int i = 0; i < playerPtr->inventory_size; i++)
{
if (playerPtr->inventory[i] != NULL && strcmp(playerPtr->inventory[i], "note") == 0)
{
playerPtr->notes_read++;
playerPtr->inventory[i] = NULL;
}
}

if (playerPtr->notes_read == 0)
{
printf("Read what?");
return;
}

printf("\033[2J");
int note_reader = 0;
do
{
switch (note_reader)
{
case 1:

printf(" MAP: |"); printf(" MY SICK RAP:\n");
break;
case 2:
printf("BEDROOM -> YARD<- |"); printf("but for me to rap like a computer must be in my genes\n");
break;
case 3:
printf(" | / | \\ |"); printf("I got a laptop in my back pocket\n");
printf("KITCHEN<- PARK ->SHACK |"); printf("my pen'll go off when I half cock it\n");
break;
case 4:
printf(" | |"); printf("got a fat knot from that rap profit\n");
break;
case 5:
printf(" | |"); printf("made a living and a killing off it\n");
break;
case 6:
printf(" RAP HERE |"); printf(" --\"Gap Rod\"");

break;
}
note_reader++;
} while (note_reader <= playerPtr->notes_read);
printf("\n\n\nPress enter to continue\n");
}
32 changes: 32 additions & 0 deletions Game/func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef FUNC_H
#define FUNC_H

typedef struct {
char * room_name; //room's name
char * connected_rooms[10]; //names of connected rooms. Note that this is not pointers as I didn't know how to make an array of room pointers while in a room
char * items[10]; //names of items. There is no item struct, though that would have been a good idea.
char entrance_msg[200];
char description[200];
char look_msg[200];
int items_size; //size of items. Can't pass array size to function, so I have this.
} room;

typedef struct {
room * current_room;
int has_backpack;
int inventory_size;
char * inventory[6];
int notes_read;
int has_poo;

} character;

//function prototypes!
void print_room_summary(character *playerPtr);
void get_args(char*, char*, char*);
void take(character*,char*,room*,int);
void moveto(character*, char*, room**);
void look(character*);
void note(character*);

#endif
Loading