Skip to content

Instantly share code, notes, and snippets.

@josuegaleas
Last active August 17, 2018 20:22
Show Gist options
  • Save josuegaleas/c9227d9d67f227eedc3b106a50f23fc3 to your computer and use it in GitHub Desktop.
Save josuegaleas/c9227d9d67f227eedc3b106a50f23fc3 to your computer and use it in GitHub Desktop.
A silly combat game based off of a prompt given to a friend. Originally done in C, and then remade in C++.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playerHP = 1000;
int enemyHP = 1000;
int playerBase = 100;
int enemyBase = 125;
char c = '\0';
int playerMove = 0;
int enemyMove = 0;
int dealt = 0;
int playerUsed = 1;
int enemyUsed = 1;
int win = 0;
void attacks();
int whirling(int d);
int artery(int d, int u);
int vicious(int d, int h);
void printline();
void attacks()
{
printf("Pick an attack:\n");
printf("[1]: Whirling Blades (base damage + 10-65 additional damage)\n");
printf("[2]: Artery Slice (10%% base damage + 20 additional damage)\n");
printf(" (20 additional damage for each time used)\n");
printf("[3]: Vicious Blow (2 * base damage + 2%% attacker's HP)\n");
}
int whirling(int d)
{
int a1 = 0;
int add1 = 0;
srand(time(NULL));
add1 = rand() & 56;
add1 = add1 + 10;
a1 = d + add1;
return a1;
}
int artery(int d, int u)
{
int a1 = 0;
int add1 = 0;
int add2 = 0;
add1 = d * 0.1;
add2 = 20 * u;
a1 = add1 + add2;
return a1;
}
int vicious(int d, int h)
{
int a1 = 0;
int add1 = 0;
int add2 = 0;
add1 = d * 2;
add2 = h * 0.02;
a1 = add1 + add2;
return a1;
}
void printline()
{
int z = 0;
for(z = 0; z < 80; z++)
{
printf("-");
}
printf("\n");
}
int main(void)
{
printf("*** Welcome to Combat Simulator 2015! ***\n");
while(!((c == 'y') || (c == 'Y') || (c == 'n') || (c == 'N')))
{
printf("Would you like to start a battle? (y/n) ");
scanf(" %c", &c);
if((c == 'y') || (c == 'Y'))
{
printline();
printline();
printline();
printf("The battle begins!\n");
printf("You have %d base damage, and your opponent has %d base damage.\n", playerBase, enemyBase);
while((playerHP > 0) && (enemyHP > 0))
{
printf("You have %d HP, and your opponent has %d HP.\n", playerHP, enemyHP);
printline();
attacks();
printf("What is your next move? ");
scanf(" %d", &playerMove);
printline();
switch(playerMove)
{
case 1:
dealt = whirling(playerBase);
break;
case 2:
dealt = artery(playerBase, playerUsed);
playerUsed++;
break;
case 3:
dealt = vicious(playerBase, playerHP);
break;
default:
printf("What did you do!? That's not an option!\n");
break;
}
if(!((playerMove == 1) || (playerMove == 2) || (playerMove == 3)))
{
continue;
}
printf("You dealt %d damage to your opponent!\n", dealt);
enemyHP = enemyHP - dealt;
if(!(enemyHP > 0))
{
win = 0;
continue;
}
printf("Your opponent now has %d HP!\n", enemyHP);
printf("It is now your opponent's turn.\n");
srand(time(NULL));
enemyMove = rand() % 3;
enemyMove++;
switch(enemyMove)
{
case 1:
dealt = whirling(enemyBase);
printf("Your opponent attacked with Whirling Blades!\n");
break;
case 2:
dealt = artery(enemyBase, enemyUsed);
printf("Your opponent attacked with Artery Slice!\n");
enemyUsed++;
break;
case 3:
dealt = vicious(enemyBase, enemyHP);
printf("Your opponent attacked with Vicious Blow!\n");
break;
default:
printf("What did you do!? That's not an option you stupid machine!\n");
printf("Your opponent attacked with ?!\n");
break;
}
printf("Your opponent dealt %d damage on you!\n", dealt);
playerHP = playerHP - dealt;
if(!(playerHP > 0))
{
win = 1;
continue;
}
printf("You now have %d HP!\n", playerHP);
printline();
}
if(win == 0)
{
printf("You won! You are left with %d HP.\n", playerHP);
}
else if(win == 1)
{
printf("You lost! Your opponent is left with %d HP.\n", enemyHP);
}
else
{
printf("You shouldn't be seeing this message, if not something went terribly wrong.\n");
}
}
else if((c == 'n') || (c == 'N'))
{
printf("Fine, be that way!\n");
}
else
{
printf("That's not an option!\n");
}
}
printf("Goodbye!\n");
return 0;
}
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void print_horizontal_line();
void move_list();
int whirling(int);
int artery(int, int);
int vicious(int, int);
int move_selection(int, int, int, int &);
void enemy_attack(int);
int random_gen();
int main()
{
char choice = '\0';
int playerHP = 1000, enemyHP = 1000;
int playerBase = 100, enemyBase = 125;
int playerMove = 0, enemyMove = 0;
int playerUsed = 1, enemyUsed = 1;
int damage_dealt = 0;
bool win = false;
cout << "*** Welcome to Combat Simulator 2015! ***" << endl;
cout << "Would you like to start a battle? (y/n) ";
cin >> choice;
if(choice == 'y' || choice == 'Y')
{
print_horizontal_line();
print_horizontal_line();
print_horizontal_line();
cout << "The battle begins!" << endl;
cout << "You have " << playerBase << " base damage, ";
cout << "and your opponent has " << enemyBase << " base damage." << endl;
print_horizontal_line();
while(playerHP > 0 && enemyHP > 0)
{
cout << "You have " << playerHP << " HP, ";
cout << "and your opponent has " << enemyHP << " HP." << endl;
print_horizontal_line();
move_list();
cout << "What is your next move? (1/2/3) ";
cin >> playerMove;
print_horizontal_line();
damage_dealt = move_selection(playerMove, playerBase, playerHP, playerUsed);
enemyHP -= damage_dealt;
cout << "You dealt " << damage_dealt << " damage to your opponent!" << endl;
if(enemyHP <= 0)
{
win = true;
continue;
}
cout << "Your opponent now has " << enemyHP << " HP!" << endl;
cout << "It is your opponent's turn." << endl;
enemyMove = random_gen() % 3 + 1;
damage_dealt = move_selection(enemyMove, enemyBase, enemyHP, enemyUsed);
playerHP -= damage_dealt;
enemy_attack(enemyMove);
cout << "Your opponent dealt " << damage_dealt << " damage on you!" << endl;
if(playerHP <= 0)
{
win = false;
continue;
}
cout << "You now have " << playerHP << " HP!" << endl;
print_horizontal_line();
}
if(win)
cout << "You won! You are left with " << playerHP << " HP." << endl;
else
cout << "You lost! Your opponent is left with " << enemyHP << " HP." << endl;
print_horizontal_line();
}
else if (choice == 'n' || choice == 'N')
cout << "Goodbye!" << endl;
else
cout << "ERROR: Not a choice, must be (y/n)! Exiting..." << endl;
return 0;
}
void print_horizontal_line()
{
for (int x = 0; x < 80; x++)
cout << "-";
cout << endl;
}
void move_list()
{
cout << "Pick an attack:" << endl;
cout << "[1]: Whirling Blades (base damage + 10-65 additional damage)" << endl;
cout << "[2]: Artery Slice (10% base damage + 20 additional damage)" << endl;
cout << " (20 additional damage for each time used)" << endl;
cout << "[3]: Vicious Blow (2 * base damage - 2% attacker's HP)" << endl;
}
int whirling(int base)
{
int add = 0;
add = random_gen() % 56 + 10;
return base + add;
}
int artery(int base, int used)
{
int add1 = 0;
int add2 = 0;
add1 = base * 0.1;
add2 = used * 20;
return add1 + add2;
}
int vicious(int base, int hp)
{
int add1 = 0;
int add2 = 0;
add1 = base * 2;
add2 = hp * 0.02;
return add1 - add2;
}
int move_selection(int move, int base, int hp, int &used)
{
int dealt = 0;
switch(move)
{
case 1:
dealt = whirling(base);
break;
case 2:
dealt = artery(base, used);
used++;
break;
case 3:
dealt = vicious(base, hp);
break;
default:
cout << "ERROR: Not a move!" << endl;
}
return dealt;
}
void enemy_attack(int move)
{
switch(move)
{
case 1:
cout << "Your opponent attacked with Whirling Blades!" << endl;
break;
case 2:
cout << "Your opponent attacked with Artery Slice!" << endl;
break;
case 3:
cout << "Your opponent attacked with Vicious Blow!" << endl;
break;
default:
cout << "Your opponent attacked with ?!" << endl;
}
}
int random_gen()
{
unsigned seed = time(0);
srand(seed);
return rand();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment