Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "projekti.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void get_line(char line[MAX_LINE_LENGTH], char* command) {
- printf("// ");
- if (fgets(line, MAX_LINE_LENGTH, stdin) && sscanf(line, "%c", command))
- return;
- else
- *command = '?';
- }
- fighter* add_fighter(fighter* fighters, char line[MAX_LINE_LENGTH]) {
- char name[MAX_NAME_LENGTH] = { 0 };
- char weapon[MAX_NAME_LENGTH] = { 0 };
- int hp, dmg;
- if (sscanf(line, "A %s %d %s %d", (char*)& name, &hp, (char*)& weapon, &dmg) != 4) {
- printf("Use valid command specifers!\n");
- return fighters;
- }
- // Checking For Invalid Input
- for (int i = 0; fighters[i].name[0] != 0; i++) {
- if (!strcmp(fighters[i].name, name)) {
- printf("Fighter %s already exists!\n", name);
- return fighters;
- }
- }
- if (hp <= 0 || dmg <= 0) {
- if (hp <= 0)
- printf("HP must be greater than 0!\n");
- if (dmg <= 0)
- printf("Damage must be greater than 0!\n");
- return fighters;
- }
- // Adding The Fighter
- unsigned int num_fighters = 1;
- for (unsigned int i = 0; 1; i++) {
- if (fighters[i].name[0] == 0) {
- strcpy(fighters[i].name, name);
- strcpy(fighters[i].weapon, weapon);
- fighters[i].hp = hp;
- fighters[i].dmg = dmg;
- num_fighters = i + 1;
- break;
- }
- }
- fighter* fighters_new = realloc(fighters, (num_fighters + 2) * sizeof(fighter));
- if (!fighters_new) {
- fprintf(stderr, "Memory Allocation failed!\n");
- return fighters;
- }
- char null_str[MAX_NAME_LENGTH] = { 0 };
- strcpy(fighters_new[num_fighters].name, null_str);
- strcpy(fighters_new[num_fighters].weapon, null_str);
- fighters_new[num_fighters].hp = 0;
- fighters_new[num_fighters].exp = 0;
- fighters_new[num_fighters].dmg = 0;
- printf("Successfully added fighter %s\n", name);
- return fighters_new;
- }
- void attack(fighter* fighters, char line[MAX_LINE_LENGTH]) {
- char attacker[MAX_NAME_LENGTH] = { 0 };
- char target[MAX_NAME_LENGTH] = { 0 };
- int attacker_i = -1;
- int target_i = -1;
- if (sscanf(line, "H %s %s", attacker, target) != 2) {
- printf("Use valid command specifers!\n");
- return;
- }
- // Checking For Invalid Input
- for (int i = 0; fighters[i].name[0] != 0; i++) {
- if (!strcmp(fighters[i].name, attacker))
- attacker_i = i;
- if (!strcmp(fighters[i].name, target))
- target_i = i;
- }
- if (attacker_i < 0 || target_i < 0) {
- if (attacker_i < 0)
- printf("Attacker not found!\n");
- if (target_i < 0)
- printf("Target not found!\n");
- return;
- }
- if (!strcmp(attacker, target)) {
- printf("Attacker can not be the target!\n");
- return;
- }
- if (fighters[attacker_i].hp == 0 || fighters[target_i].hp == 0) {
- if (fighters[attacker_i].hp == 0)
- printf("%s is already dead!\n", attacker);
- if (fighters[target_i].hp == 0)
- printf("%s is already dead!\n", target);
- return;
- }
- //Doing The Attacking
- fighters[target_i].hp -= fighters[attacker_i].dmg;
- if (fighters[target_i].hp <= 0) {
- fighters[target_i].hp = 0;
- fighters[attacker_i].exp += EXP_GAIN;
- printf("%s killed %s with %s!\n", attacker, target, fighters[attacker_i].weapon);
- printf("%s gained %d exp!\n", attacker, EXP_GAIN);
- return;
- }
- else {
- printf("%s attacked %s with %s by %d damage!\n",
- attacker, target, fighters[attacker_i].weapon, fighters[attacker_i].dmg);
- printf("%s has %d hitpoints remaining!\n", target, fighters[target_i].hp);
- }
- }
- void print_fighters(fighter* fighters) {
- //FIX THIS ** FIX THIS ** FIX THIS ** FIX THIS !!!!!!!!!!!!!!!!!!!!!!!!!
- if (fighters[0].name[0] == 0)
- printf("There are no fighters!\n");
- for (int i = 0; fighters[i].name[0] != 0; i++) {
- printf("%s: EXP=%d, HP=%d, WPN=%s, DMG=%d\n",
- fighters[i].name, fighters[i].exp,
- fighters[i].hp, fighters[i].weapon,
- fighters[i].dmg);
- }
- }
- void write_game(fighter* fighters, char line[MAX_LINE_LENGTH]) {
- char filename[MAX_NAME_LENGTH] = { 0 };
- if (sscanf(line, "W %s", (char*)& filename) != 1) {
- printf("Use valid command specifiers!\n");
- return;
- }
- FILE* file = fopen(filename, "w");
- if (!file) {
- printf("Could not open file %s!\n", filename);
- return;
- }
- int num_fighters = 0;
- for (int i = 0; fighters[i].name[0] != 0; i++) {
- num_fighters = i + 1;
- }
- if (fwrite(fighters, sizeof(fighter), num_fighters, file) != num_fighters) {
- printf("Writing failed!\n");
- fclose(file);
- return;
- }
- printf("Writing was successful!\n");
- fclose(file);
- }
- fighter* read_game(fighter* fighters, char line[MAX_LINE_LENGTH]) {
- char filename[MAX_NAME_LENGTH] = { 0 };
- int num_fighters = 0;
- if (sscanf(line, "O %s", (char*)& filename) != 1) {
- printf("Use valid command specifiers!\n");
- return fighters;
- }
- FILE* file = fopen(filename, "r");
- if (!file) {
- printf("Could not open file %s!\n", filename);
- return fighters;
- }
- while (fgetc(file) != EOF) {
- fseek(file, sizeof(fighter) - 1, SEEK_CUR);
- num_fighters++;
- }
- fseek(file, 0, SEEK_SET);
- fighter* fighters_new = malloc((num_fighters + 1) * sizeof(fighter));
- if (!fighters_new) {
- printf("Memory allocation failed!\n");
- fclose(file);
- return fighters;
- }
- if (fread(fighters_new, sizeof(fighter), num_fighters, file) != num_fighters) {
- printf("Reading failed!\n");
- fclose(file);
- return fighters;
- }
- char null_str[MAX_NAME_LENGTH] = { 0 };
- strcpy(fighters_new[num_fighters].name, null_str);
- strcpy(fighters_new[num_fighters].weapon, null_str);
- fighters_new[num_fighters].hp = 0;
- fighters_new[num_fighters].exp = 0;
- fighters_new[num_fighters].dmg = 0;
- printf("Reading was successful!\n");
- fclose(file);
- return fighters_new;
- }
- int main(void) {
- printf("%d", sizeof(fighter) + sizeof(char));
- fighter* fighters = malloc(sizeof(fighter));
- if (!fighters) {
- fprintf(stderr, "Memory Allocation Failed!");
- return -1;
- }
- fighter null_fighter = { {0}, {0}, 0, 0, 0 };
- fighters[0] = null_fighter;
- char line[MAX_LINE_LENGTH], command;
- int game_on = 1;
- while (game_on) {
- get_line(line, &command);
- switch (command) {
- case ('A'):
- fighters = add_fighter(fighters, line);
- break;
- case ('H'):
- attack(fighters, line);
- break;
- case ('L'):
- print_fighters(fighters);
- break;
- case ('W'):
- write_game(fighters, line);
- break;
- case ('O'):
- fighters = read_game(fighters, line);
- break;
- case ('Q'):
- free(fighters);
- game_on = 0;
- break;
- default:
- printf("Use a valid command!\n");
- break;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement