Llama513
2017-08-02, 04:18 PM
I made some code for C that allows the DM to enter the number of combatants, their initiative, their health, and give a name to each of them, it then sorts the entries by highest initiative, it doesn't handle ties, so you will want to satisfy those before you enter initiatives. It will then run you through combat keeping track of whos turn it is, and damage/healing done, when a combatant is reduced to 0 hitpoints it gives the option to exit combat.
Header File (combat.h)
struct combatant{
char name[25];
int initiative;
int health;
};
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "combat.h"
int main()
{
int howMany, i, swapped;
puts("How many combatants are there?");
scanf(" %d", &howMany);
struct combatant id[howMany];
struct combatant temp;
for(i = 0; i < howMany; i++){
puts("Enter combatants name");
scanf(" %s", id[i].name);
}
for(i = 0;i < howMany;i++){
printf("Enter %s initiative \n", id[i].name);
scanf(" %d", &id[i].initiative);
}
for(i = 0;i < howMany;i++){
printf("Enter %s health \n", id[i].name);
scanf(" %d", &id[i].health);
}
while(1){
swapped = 0;
for(i = 0; i < howMany-1; i++){
if(id[i].initiative < id[i+1].initiative){
strcpy(temp.name, id[i].name);
temp.initiative = id[i].initiative;
temp.health = id[i].health;
strcpy(id[i].name, id[i+1].name);
id[i].initiative = id[i+1].initiative;
id[i].health = id[i+1].health;
strcpy(id[i+1].name, temp.name);
id[i+1].initiative = temp.initiative;
id[i+1].health = temp.health;
swapped = 1;
}
}
if(swapped == 0){
break;
}
}
char enter[5], done[5], attack, heal, target[25];
int amount, creatures;
i=0;
while(1){
if(i >= howMany){
i = 0;
}else{
if(id[i].health > 0){
printf("\n");
printf("%s \t",id[i].name);
printf("HP: %d \n",id[i].health);
printf("Did %s attack? (Y/N) ",id[i].name);
scanf(" %c", &attack);
if(toupper(attack)=='Y'){
printf("How many people did %s attack? ",id[i].name );
scanf(" %d", &creatures);
int r;
for(r = 0; r < creatures; r++){
printf("Who did they attack? ");
scanf(" %s",target);
printf("How much damage did they do? ");
scanf(" %d", &amount);
int x;
for(x = 0; x < howMany; x++)
if(strcmp(id[x].name,target) == 0 ){
id[x].health -= amount;
if(id[x].health <= 0){
printf("%s is dead\n", id[x].name);
puts("If combat is over enter exit");
}else{
printf("%s is at %d health \n", id[x].name, id[x].health);
}
}
}i++;
gets(enter);
if(strcmp(enter, "exit") == 0){
break;
}
}
else{
printf("Did %s heal? (Y/N) ", id[i].name);
scanf(" %c", &heal);
if(toupper(heal) == 'Y'){
printf("How many people did %s heal? ",id[i].name);
scanf(" %d", &creatures);
int r;
for(r = 0; r < creatures; r++){
printf("Who did they heal? ");
scanf(" %s", target);
printf("How much did they heal? ");
scanf(" %d", &amount);
int x;
for(x = 0; x < howMany; x++){
if(strcmp(id[x].name,target) == 0 ){
id[x].health += amount;
printf("%s is at %d health \n", id[x].name, id[x].health);
}
}
}
}i++;
}
}
else{
printf("%s is dead\n", id[i].name);
puts("If combat is over enter exit");
i++;
}
gets(enter);
}if(strcmp(enter, "exit") == 0){
break;
}
}
return 0;
}
Header File (combat.h)
struct combatant{
char name[25];
int initiative;
int health;
};
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "combat.h"
int main()
{
int howMany, i, swapped;
puts("How many combatants are there?");
scanf(" %d", &howMany);
struct combatant id[howMany];
struct combatant temp;
for(i = 0; i < howMany; i++){
puts("Enter combatants name");
scanf(" %s", id[i].name);
}
for(i = 0;i < howMany;i++){
printf("Enter %s initiative \n", id[i].name);
scanf(" %d", &id[i].initiative);
}
for(i = 0;i < howMany;i++){
printf("Enter %s health \n", id[i].name);
scanf(" %d", &id[i].health);
}
while(1){
swapped = 0;
for(i = 0; i < howMany-1; i++){
if(id[i].initiative < id[i+1].initiative){
strcpy(temp.name, id[i].name);
temp.initiative = id[i].initiative;
temp.health = id[i].health;
strcpy(id[i].name, id[i+1].name);
id[i].initiative = id[i+1].initiative;
id[i].health = id[i+1].health;
strcpy(id[i+1].name, temp.name);
id[i+1].initiative = temp.initiative;
id[i+1].health = temp.health;
swapped = 1;
}
}
if(swapped == 0){
break;
}
}
char enter[5], done[5], attack, heal, target[25];
int amount, creatures;
i=0;
while(1){
if(i >= howMany){
i = 0;
}else{
if(id[i].health > 0){
printf("\n");
printf("%s \t",id[i].name);
printf("HP: %d \n",id[i].health);
printf("Did %s attack? (Y/N) ",id[i].name);
scanf(" %c", &attack);
if(toupper(attack)=='Y'){
printf("How many people did %s attack? ",id[i].name );
scanf(" %d", &creatures);
int r;
for(r = 0; r < creatures; r++){
printf("Who did they attack? ");
scanf(" %s",target);
printf("How much damage did they do? ");
scanf(" %d", &amount);
int x;
for(x = 0; x < howMany; x++)
if(strcmp(id[x].name,target) == 0 ){
id[x].health -= amount;
if(id[x].health <= 0){
printf("%s is dead\n", id[x].name);
puts("If combat is over enter exit");
}else{
printf("%s is at %d health \n", id[x].name, id[x].health);
}
}
}i++;
gets(enter);
if(strcmp(enter, "exit") == 0){
break;
}
}
else{
printf("Did %s heal? (Y/N) ", id[i].name);
scanf(" %c", &heal);
if(toupper(heal) == 'Y'){
printf("How many people did %s heal? ",id[i].name);
scanf(" %d", &creatures);
int r;
for(r = 0; r < creatures; r++){
printf("Who did they heal? ");
scanf(" %s", target);
printf("How much did they heal? ");
scanf(" %d", &amount);
int x;
for(x = 0; x < howMany; x++){
if(strcmp(id[x].name,target) == 0 ){
id[x].health += amount;
printf("%s is at %d health \n", id[x].name, id[x].health);
}
}
}
}i++;
}
}
else{
printf("%s is dead\n", id[i].name);
puts("If combat is over enter exit");
i++;
}
gets(enter);
}if(strcmp(enter, "exit") == 0){
break;
}
}
return 0;
}