Skip to content

Instantly share code, notes, and snippets.

@sagebind
Created January 10, 2014 17:32
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save sagebind/8358734 to your computer and use it in GitHub Desktop.
Save sagebind/8358734 to your computer and use it in GitHub Desktop.
A simple game in C++ for a class exercise.
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
using namespace std;
int createZombie() {
if (rand() % 67 < 10)
return 11;
else
return rand() % 10 + 1;
}
int main() {
srand(time(NULL));
char enter;
// game stats
int playerAlive = true;
int playerSkill = 9;
int playerScore = 1;
string playerName = "";
int zombieCount = 0;
int zombiesKilled = 0;
// title
cout << "Welcome to Zombie War." << endl << "Press [ENTER] to start.";
cin.get();
// player name
cout << "Please enter your name: ";
cin >> playerName;
// ask how many zombies
cout << "How many zombies do you wish to fight? ";
cin >> zombieCount;
cout << "Get ready to fight for your life, " << playerName << "!" << endl;
// main game loop
while (playerAlive && zombiesKilled < zombieCount) {
// create a random zombie
int zombieSkill = createZombie();
// battle sequence
if (zombieSkill > 10) {
cout << endl << "Here comes a huge zombie!" << endl;
}
else {
cout << endl << "Here comes zombie " << zombiesKilled + 1 << endl;
}
cout << "Fighting..." << endl;
sleep(2);
// zombie killed the player
if (playerSkill < zombieSkill) {
playerAlive = false;
cout << "You have died." << endl;
}
// player killed the zombie
else {
if (playerSkill - zombieSkill > 7) {
cout << "You wasted the zombie!" << endl;
playerScore = playerScore * 2;
}
else if (playerSkill - zombieSkill > 5) {
cout << "You decapitated the zombie!" << endl;
playerScore = playerScore * 2;
}
else if (playerSkill - zombieSkill > 0) {
cout << "You killed the zombie!" << endl;
playerScore = playerScore * 2;
}
else {
cout << "You killed the zombie, but suffered injuries." << endl;
}
zombiesKilled++;
}
cout << endl;
sleep(1);
}
// end game
if (zombiesKilled == zombieCount) {
// victory
cout << "You have survived the onslaught!" << endl;
}
else {
// lost
cout << "You did not survive the zombie war." << endl;
}
cout << "Zombies killed: " << zombiesKilled << endl;
cout << "Final score: " << playerScore << endl << endl;
}
@usernamewastakenz
Copy link

I did not survive the zombie war!?!?

@gakkkkkkki
Copy link

I did also.

@jodros
Copy link

jodros commented Apr 26, 2018

código bosta seu lixo game o crl

@eckmo
Copy link

eckmo commented Jul 13, 2018

Definitely not necessary, but for a simple adjustment you could add getline (cin, playerName); instead of just a cin in case the user enters their first and last name?

@Johncron
Copy link

This worked mostly fine aside from sleep, it was not declared as anything but just deleting the lines with sleep in it made it work fine.

@Pacific06
Copy link

what is line no 8 to 12 doing?

@Paull0
Copy link

Paull0 commented Nov 6, 2018

I lost however how can you "object orient" this program

@GitGoodAvik
Copy link

Thank you for this. I am trying to learn C++ and going through this was really fun way to learn the use.

@xnorcat
Copy link

xnorcat commented Oct 8, 2019

Thank you, it worked! I want to learn c++ but I don't know how start.

@siddharth952
Copy link

Thanks!

@epic-programmer99
Copy link

it never worked for me but nice 👍

@CyberPunk87
Copy link

Im learning at least.. giving me an idea. Good stuff and thanks.

@alonelywolf014
Copy link

Works for me (if i remove the sleep statements at 55 and 88 )
Good Work
:>

@RusLanParty
Copy link

Sleep is supposed to be with captital S!
Other than that cool project, works great

@AlLiSoNsuvarna
Copy link

how do you work thiss
kinda new here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment