Skip to content

Instantly share code, notes, and snippets.

@98chimp
Created March 12, 2015 06:18
Show Gist options
  • Save 98chimp/a76ba6fae5fd9f77865f to your computer and use it in GitHub Desktop.
Save 98chimp/a76ba6fae5fd9f77865f to your computer and use it in GitHub Desktop.
Adventure Game
//
// main.c
// Adventure Game
//
// Created by Shahin on 2015-03-11.
// Copyright (c) 2015 98% Chimp. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct room {
struct room *north;
struct room *south;
struct room *east;
struct room *west;
int x;
}Room;
typedef struct player{
Room *currentRoom;
int health;
Room *exitsAvailable;
}Player;
void movePlayer(Player *myPlayer, Room *myRoom);
void functionStatus();
void playerStatus(Player *myPlayer);
Room *roomCreator();
void appendRoom(Room *roomOrigin, Room *nextRoom);
Room *createGrid(int numberOfRooms);
void scanMovement(Player *currentPlayer);
int main(int argc, const char * argv[]) {
bool isRunning = true;
Room *room1 = createGrid(4);
printf("%d\n", room1->x);
Player *loligan = (Player *)malloc(sizeof(Player));
loligan->health = 10;
loligan->currentRoom = room1;
playerStatus(loligan);
// playerStatus(loligan);
// movePlayer(loligan, loligan->currentRoom->east);
// playerStatus(loligan);
// movePlayer(loligan, loligan->currentRoom->east);
// playerStatus(loligan);
// movePlayer(loligan, loligan->currentRoom->east);
while (isRunning) {
scanMovement(loligan);
movePlayer(loligan, loligan->currentRoom->east);
playerStatus(loligan);
}
return 0;
}
Room *roomCreator(){
Room *builtRoom = (Room *)malloc(sizeof(Room));
return builtRoom;
}
void appendRoom(Room *roomOrigin, Room *nextRoom){
if ( (roomOrigin != NULL) && (nextRoom != NULL)){
roomOrigin->east = nextRoom;
nextRoom->west = roomOrigin;
if (nextRoom->x == 0){
nextRoom->west = NULL;}
if (nextRoom->x == 3){
nextRoom->east = NULL;
}
}
}
void playerStatus (Player *myPlayer) {
printf("your health is: %d\n", myPlayer->health);
printf("your location is: %d\n", myPlayer->currentRoom->x);
printf("you can go: ");
if (myPlayer->currentRoom->east != NULL) {
printf("East, \n");
}
if (myPlayer->currentRoom->west != NULL) {
printf("West.\n");
}
}
void movePlayer(Player *myPlayer, Room *myRoom){
myPlayer->currentRoom = myRoom;
}
Room *createGrid(int numberOfRooms){
if ( numberOfRooms == 0){
return NULL;
}
Room *originRoom = roomCreator();
Room *previousRoom = originRoom;
for ( int i = 0; i < numberOfRooms; i++){
previousRoom->x = i;
Room *linkRoom = roomCreator();
appendRoom(previousRoom, linkRoom);
previousRoom = linkRoom;
}
return originRoom;
}
void scanMovement(Player *currentPlayer) {
char *movePerson;
scanf("%s", movePerson);
if (strcmp(movePerson, "e")) {
movePlayer(currentPlayer, currentPlayer->currentRoom->east);
}
if (strcmp(movePerson, "w")) {
movePlayer(currentPlayer, currentPlayer->currentRoom->west);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment