Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DnD.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <conio.h>
- #include <string.h>
- #include <time.h>
- #define MAX_NAME_PLAYER 100
- #define PLAYER_INITIAL_ENERGY 100
- #define PLAYER_INITIAL_CELL 0
- #define PLAYER_INITIAL_OBJECT -1
- #define PLAYER_INITIAL_TREASURE -1
- #define MONSTER_NAME 10
- #define MONSTER_INITIAL_ENERGY 150
- #define MONSTER_INITIAL_CELL 9
- #define MAX_CELL_DESCRIPTION 200
- #define MAX_CELLS 20
- #define MAX_LIN 100
- #define MAX_OBJECT_NAME 100
- struct Player
- {
- char name[MAX_NAME_PLAYER];
- int energy;
- int cell;
- int object;
- int treasure;
- };
- struct Monster
- {
- char name[MONSTER_NAME];
- int energy;
- int cell;
- };
- struct Cell
- {
- int north;
- int south;
- int east;
- int west;
- int up;
- int down;
- char description1[MAX_CELL_DESCRIPTION];
- char description2[MAX_CELL_DESCRIPTION];
- int object;
- int treasure;
- };
- struct Object
- {
- char name[MAX_OBJECT_NAME];
- int damage;
- };
- int CheckEndGame(struct Player *pPlayer, struct Monster *pMonster);
- int CheckPlayerLocation(struct Player *pPlayer);
- int GetArrowKeys();
- int InitializeMap(struct Cell cells[]);
- void Fight(struct Player *pPlayer, struct Monster *pMonster);
- void InitializeMonster(struct Monster *pMonster);
- void InitializeObjects(struct Object *pObject);
- void InitializePlayer(struct Player *pPlayer);
- void MoveMonster(struct Monster *pMonster);
- void PrintMap(struct Cell cells[], int nCells);
- void PrintMonster(struct Monster *pMonster);
- void PrintObjects(struct Object *pObject);
- void PrintPlayer(struct Player *pPlayer);
- void PrintPlayerLocation(struct Player *pPlayer, struct Cell cells[]);
- int main()
- {
- struct Player player;
- struct Monster monster;
- struct Cell cells[MAX_CELLS];
- struct Object object;
- int nCells;
- // Initialize Player
- InitializePlayer(&player);
- PrintPlayer(&player);
- //Initialize Monster
- InitializeMonster(&monster);
- MoveMonster(&monster);
- PrintMonster(&monster);
- // Initialize Map
- nCells = InitializeMap(cells);
- PrintMap(cells, nCells);
- // Initialize Objects
- InitializeObjects(&object);
- PrintObjects(&object);
- if (CheckEndGame(&player, &monster) == 0)
- {
- while (CheckEndGame(&player, &monster) != -1 && CheckEndGame(&player, &monster) != 1)
- {
- int PlayerLocation = player.cell;
- MoveMonster(&monster);
- PrintPlayerLocation(&player, cells);
- int arrow = GetArrowKeys();
- if (arrow == 0)
- {
- if (cells[PlayerLocation].north != -1)
- player.cell = cells[player.cell].north;
- }
- else if (arrow == 1)
- {
- if (cells[PlayerLocation].south != -1)
- player.cell = cells[PlayerLocation].south;
- }
- else if (arrow == 2)
- {
- if (cells[PlayerLocation].east != -1)
- player.cell = cells[PlayerLocation].east;
- }
- else if (arrow == 3)
- {
- if (cells[PlayerLocation].west != -1)
- player.cell = cells[PlayerLocation].west;
- }
- if (player.cell == monster.cell)
- {
- printf("\nYou have met with the monster!\n Let's Fight!\n");
- Fight(&player, &monster);
- PrintPlayer(&player);
- PrintMonster(&monster);
- }
- }
- }
- else if (CheckEndGame(&player, &monster) == -1)
- {
- printf("You Lost!");
- }
- else if (CheckEndGame(&player, &monster) == 1)
- {
- printf("You Won!");
- }
- else
- {
- PrintMonster(&monster);
- }
- printf("\nGood Luck %s.\n", player.name);
- system("pause");
- return 0;
- }
- void InitializePlayer(struct Player *pPlayer)
- {
- printf("Hello Adventurer! What's your name?\n");
- scanf("%s", pPlayer->name);
- pPlayer->energy = PLAYER_INITIAL_ENERGY;
- pPlayer->cell = PLAYER_INITIAL_CELL;
- pPlayer->object = PLAYER_INITIAL_OBJECT;
- pPlayer->treasure = PLAYER_INITIAL_TREASURE;
- }
- void PrintPlayer(struct Player *pPlayer)
- {
- printf("\n*** Player ***\n");
- printf("Name: %s\n", pPlayer->name);
- printf("Energy: %d\n", pPlayer->energy);
- printf("Cell: %d\n", pPlayer->cell);
- printf("Object: %d\n", pPlayer->object);
- printf("Treasure: %d\n", pPlayer->treasure);
- }
- void InitializeMonster(struct Monster *pMonster)
- {
- strcpy(pMonster->name, "Owlbear");
- pMonster->energy = MONSTER_INITIAL_ENERGY;
- pMonster->cell = MONSTER_INITIAL_CELL;
- }
- void PrintMonster(struct Monster *pMonster)
- {
- printf("\n*** Monster ***\n");
- printf("Name: %s\n", pMonster->name);
- printf("Energy: %d\n", pMonster->energy);
- printf("Cell: %d\n", pMonster->cell);
- }
- int InitializeMap(struct Cell cells[])
- {
- FILE *f;
- char l[MAX_LIN];
- char *filename = (char*)"C:/Users/bruno/Desktop/DnD/DnD/map.txt";
- f = fopen(filename, "r");
- if (f == NULL) {
- printf("Could not open file %s", filename);
- return 1;
- }
- int count = 0;
- int i = 0;
- while (fgets(l, MAX_LIN, f) != NULL) {
- if (count == 0)
- {
- sscanf(l, "%d %d %d %d %d %d %d %d", &cells[i].north, &cells[i].south, &cells[i].east,
- &cells[i].west, &cells[i].up, &cells[i].down,
- &cells[i].object, &cells[i].treasure);
- }
- if (count == 1)
- {
- strcpy(cells[i].description1, l);
- }
- if (count == 2)
- {
- strcpy(cells[i].description2, l);
- }
- if(count == 3)
- {
- count = -1;
- i++;
- }
- count++;
- }
- fclose(f);
- return 10;
- }
- void PrintMap(struct Cell cells[], int nCells)
- {
- for (int i = 0; i < nCells; i++) {
- printf("\nCell: %d \nNorth: %d \nSouth: %d \nEast: %d \nWest: %d \nUp: %d \nDown: %d \nObject: %d \nTreasure: %d \nDescription: %s \n",
- i, cells[i].north, cells[i].south, cells[i].east, cells[i].west,
- cells[i].up, cells[i].down, cells[i].object, cells[i].treasure, cells[i].description1, cells[i].description2);
- }
- }
- void InitializeObjects(struct Object *pObject)
- {
- strcpy(pObject[0].name, "Ring of Contrariness");
- pObject[0].damage = 30;
- strcpy(pObject[1].name, "Bountiful Spade");
- pObject[1].damage = 50;
- strcpy(pObject[2].name, "Axe of the Dwarvish Lords");
- pObject[2].damage = 60;
- }
- void PrintObjects(struct Object *pObject)
- {
- for (int i = 0; i < 3; i++)
- {
- printf("\nObject: %d \nName: %s \nDamage: %d\n", i, pObject[i].name, pObject[i].damage);
- }
- }
- int CheckEndGame(struct Player *pPlayer, struct Monster *pMonster)
- {
- if (pPlayer->energy <= 0)
- {
- return -1;
- }
- else if (pMonster->energy <= 0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void MoveMonster(struct Monster *pMonster)
- {
- time_t t;
- srand(time(&t));
- pMonster->cell = rand() % 9 + 1;
- }
- void PrintPlayerLocation(struct Player *pPlayer, struct Cell cells[])
- {
- printf("\nPlayer's Location: Room %d\n%s\n", pPlayer->cell, cells[pPlayer->cell].description1);
- }
- int GetArrowKeys()
- {
- int arrow;
- unsigned char a;
- printf("\nPlease press an arrow key to proceed.\n");
- a = getch();
- //for detect the function\arrow keys
- //we must call the getch() again
- //testing if a is '0' or '0xE0'
- if (a == 0 || a == 0xE0) a = getch();
- if (a == 72)
- {
- arrow = 0;
- printf("UP\n");
- }
- else if (a == 80)
- {
- arrow = 1;
- printf("DOWN\n");
- }
- else if (a == 75)
- {
- arrow = 2;
- printf("LEFT\n");
- }
- else if (a == 77)
- {
- arrow = 3;
- printf("RIGHT\n");
- }
- else
- {
- printf("%d\n", (int)a);
- }
- return arrow;
- }
- void Fight(struct Player *pPlayer, struct Monster *pMonster)
- {
- pPlayer->energy = pPlayer->energy - 50;
- pMonster->energy = pMonster->energy - 100;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement