Skip to content

Instantly share code, notes, and snippets.

@seanpianka
Created May 13, 2018 04:50
Show Gist options
  • Save seanpianka/b33767ff5fec1c5cc8c0cffefb80b67e to your computer and use it in GitHub Desktop.
Save seanpianka/b33767ff5fec1c5cc8c0cffefb80b67e to your computer and use it in GitHub Desktop.
A game where spartans and skeletons fight! Idea from YouTube channel "MakingGamesWithBen" and his first challenge video!
/*
* Title: Spartan vs. Skeletons
* Author: Sean Pianka
* Date: November 3rd, 2015
* Description: A game where spartans and skeletons fight! Idea from
* YouTube channel "MakingGamesWithBen" and his first challenge video!
The game is based off of this tutorial, which I highly recommend to
anyone looking to get into programming and game development:
https://www.youtube.com/watch?v=TH7plF4UT_E
It's a randomized combat simulator between two factions: the Spartans
(a small, deadly group of powerful warriors) and the Skeletons (plentiful
ghouls and dangerous in numbers, but weak on their own). Enter the starting
amount of each faction, and watch the battle take place before your eyes!
I made this game in November 2015. This was my second semester in college
and the first several hundred-line project I made (and the first project
that both took a decent amount of time to build and was not homework).
At this point, I had been programming for about 3 months or so (started
at the beginning of the semester in August 2015) and was just starting to
get the hang of programming through my introduction to programming class
(COP3014) at F.S.U.. I began to learn more on my own because game
development seemed fun and I was enjoying learning how to program.
*/
#include <iostream>
#include <random>
#include <string>
#include <ctime>
int getNumSpartans();
int getNumSkeletons();
void simulateBattle(int, int);
void displayBattleResults(int, int, int, int);
struct army
{
float health;
float damage;
float chanceOfHit;
float currentHealth;
int turnNum;
int deathCount;
int remaining;
} spartan, skeleton;
int main()
{
int runRepeat = 1;
while (runRepeat == 1)
{
std::cout << "Welcome to the battle! Today's competitors are Spartans versus the Skeletons!\n\n";
simulateBattle(getNumSpartans(), getNumSkeletons());
std::cout << "\n\nWould you like to replay?\n\n1. Yes\n2. No\n\nChoice: ";
std::cin >> runRepeat;
if (runRepeat != 1 && runRepeat != 0){
runRepeat = 0;
}
std::cout << "\n";
}
return 0;
}
int getNumSpartans()
{
int spartanArmyCount;
std::cout << "Input the number of Spartans you wish to see fight: ";
std::cin >> spartanArmyCount;
return spartanArmyCount;
}
int getNumSkeletons()
{
int skeletonArmyCount;
std::cout << "Input the number of Skeletons you wish to see fight: ";
std::cin >> skeletonArmyCount;
return skeletonArmyCount;
}
void simulateBattle(int spartanArmyCount, int skeletonArmyCount)
{
// random number generation
static std::mt19937 RANDGEN(time(NULL));
static std::mt19937 randSkel(time(NULL) - 1);
static std::mt19937 randSpar(time(NULL) + 1);
std::uniform_int_distribution<int> turnGen(0, 1);
std::uniform_real_distribution<float> hitChanceGen(0.0f, 1.0f);
std::uniform_real_distribution<float> skelDmgGen(35.0f, 50.0f);
std::uniform_real_distribution<float> sparDmgGen(80.0f, 100.0f);
// Spartan Properties
spartan.health = 250.0f;
spartan.currentHealth = 250.0f;
spartan.chanceOfHit = .65f;
spartan.damage = 5;
spartan.turnNum = 1;
spartan.deathCount = 0;
// Skeleton Properties
skeleton.health = 145.0f;
skeleton.currentHealth = 145.0f;
skeleton.chanceOfHit = .35f;
skeleton.damage = 15;
skeleton.turnNum = 1;
skeleton.deathCount = 0;
// Starting team, Turn Counter, Attack Chance
int battleTurn, battleTurnCount = 1;
float attackChncResult;
battleTurn = turnGen(RANDGEN);
spartan.remaining = spartanArmyCount - spartan.deathCount;
skeleton.remaining = skeletonArmyCount - skeleton.deathCount;
// Start of Battle Simulation; battleTurn == 0 -> skeletons; battleTurn == 1 -> spartans
std::cout << "\n**The battle begins!**";
while ((spartan.remaining) > 0 && (skeleton.remaining) > 0)
{
std::cout << "\n\n|Swing " << battleTurnCount << "|\n\n";
if (battleTurn == 0)
{
attackChncResult = hitChanceGen(randSkel);
if (attackChncResult <= skeleton.chanceOfHit)
{
skeleton.damage = skelDmgGen(randSkel);
spartan.currentHealth -= skeleton.damage;
std::cout << "The skeleton hit the spartan for " << skeleton.damage << " damage";
if (spartan.currentHealth > 0)
{
std::cout << ", reducing his health to " << spartan.currentHealth << " hitpoints!";
}
else if (spartan.currentHealth <= 0)
{
spartan.deathCount++;
spartan.remaining = spartanArmyCount - spartan.deathCount;
spartan.currentHealth = 0;
if (spartan.remaining > 0)
{
std::cout << ", and killing the spartan!\n**crowd cheers**\n\n";
if (spartan.remaining == 1)
{
std::cout << "-- " << spartan.remaining << " spartan remains --";
}
else if (spartan.remaining > 1)
{
std::cout << "-- " << spartan.remaining << " spartans remains --";
}
spartan.currentHealth = spartan.health;
}
else if (spartan.remaining == 0)
{
std::cout << ", and killing the spartan and the last of the spartans!\n**crowd roars in applause**";
}
}
}
else
{
std::cout << "The skeleton missed the swing of his sword!";
}
battleTurn = 1;
}
else if (battleTurn == 1) // spartans attacking
{
attackChncResult = hitChanceGen(randSpar);
if (attackChncResult <= spartan.chanceOfHit)
{
spartan.damage = sparDmgGen(randSpar);
skeleton.currentHealth -= spartan.damage;
std::cout << "The spartan hit the skeleton for " << spartan.damage << " damage";
if (skeleton.currentHealth > 0)
{
std::cout << ", reducing his health to " << skeleton.currentHealth << " hitpoints!";
}
else if (skeleton.currentHealth <= 0)
{
skeleton.deathCount++;
skeleton.remaining = skeletonArmyCount - skeleton.deathCount;
skeleton.currentHealth = 0;
if (skeleton.remaining > 0)
{
std::cout << ", killing the skeleton!\n**crowd cheers**\n\n";
if (skeleton.remaining == 1)
{
std::cout << "-- " << skeleton.remaining << " skeleton remains --";
}
else if (skeleton.remaining > 1)
{
std::cout << "-- " << skeleton.remaining << " skeletons remains --";
}
skeleton.currentHealth = skeleton.health;
}
else if (skeleton.remaining == 0)
{
std::cout << ", killing the skeleton and the last of the skeletons!\n**crowd roars in applause**";
}
}
}
else
{
std::cout << "The spartan missed the swing of his sword!";
}
battleTurn = 0;
}
else {
std::cout << "turnGen error.";
}
battleTurnCount++;
}
displayBattleResults(spartan.remaining, skeleton.remaining, spartan.deathCount, skeleton.deathCount);
}
void displayBattleResults(int spartanRemaining, int skeletonRemaining, int spardeathCount, int skeldeathCount)
{
if (spartanRemaining <= 0)
{
std::cout << "\n\nAfter having killed " << spardeathCount << " spartans, the skeletons are victorious with " << skeletonRemaining;
if (skeleton.remaining == 1)
{
std::cout << " skeleton left standing!";
}
else
{
std::cout << " skeletons left standing!";
}
}
else if (skeletonRemaining <= 0)
{
std::cout << "\n\nAfter having killed " << skeldeathCount << " skeletons, the spartans are victorious with " << spartanRemaining;
if (spartan.remaining == 1)
{
std::cout << " spartan left standing!";
}
else
{
std::cout << " spartans left standing!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment