Oct 16, 2016 at 11:01pm UTC
So im writing a program ( a "game" if you will) for fun and I ran into this issue. The program compiles, but it seems like everyone is surviving. Im not sure if its an issue with a random number, or the conditional statement.(fixed)
The other problem i have is that the program crashes after a number of loops.
And my last problem is that i get blank names displaying when running the program, but I cant figure out why.
Thanks for any input!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
//
// main.cpp
// WalkingDeadSimDate 10-16-16
//
// Created by on 10/16/16.
// Copyright © 2016 All rights reserved.
//
//Includes
#include<string>
#include<ctime>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include <unistd.h>
using namespace std;
int randomPick()
{
int randomInt;//int for return value
int total = 6;
randomInt = rand() % total + 0; //between 0 and 5
return randomInt;
}
int decideFate()
{
int randNum; //To return
randNum = rand() % 11+1;
return randNum;
}
int main()
{
//Variables
int randomNumber;
int userInput;
//Rand ints of Death
int deadNum1;
int deadNum2;
int deadNum3;
int deadNum4;
//Vector of Living Characters
vector <string> livingVector;
livingVector.push_back("Rick" );
livingVector.push_back("Michonne" );
livingVector.push_back("Carl" );
livingVector.push_back("Lori" );
livingVector.push_back("Maggi" );
livingVector.push_back("Glen" );
//Vector of Dead Characters
vector <string> dead;
cout << "Welcome to the Walking Dead Text Game!" << endl;
cout << "Press a number between 1 and 10." << endl;
cout << "There will be a 60/40 chance at life/death." << endl;
cout << "Last character alive is the winner!" << endl;
cout << "Unless it's Lori, then you lose." << endl;
usleep(999999);
while (livingVector.size() > 2)
{
//seed random num generator
srand(time(NULL));
randomNumber = randomPick();
//Person Selected
cout << "The Selected Person is: " << livingVector[randomNumber] << endl;
cout << "Select a number between 1 and 10 to decide " << livingVector[randomNumber] << "'s fate!" << endl;
//Get user input
cin >> userInput;
//Seed generator
srand(time(NULL));
deadNum1 = decideFate();
//Seed generator again
srand(time(NULL));
deadNum2 = decideFate();
//Seed generator again
srand(time(NULL));
deadNum3 = decideFate();
//Seed generator for last time
srand(time(NULL));
deadNum4 = decideFate();
//Decide character's fate
if (userInput == deadNum1 || userInput == deadNum2 || userInput == deadNum3 || userInput == deadNum4)
{
cout << livingVector[randomNumber] << " has been killed!" << endl;
cout << "Zombie ate " << livingVector[randomNumber] << "'s Face!" << endl;
dead.push_back(livingVector[randomNumber]);
livingVector.erase(livingVector.begin() + randomNumber);
}
else
{
cout << livingVector[randomNumber] << " has survived!" << endl;
}
}
}
Last edited on Oct 16, 2016 at 11:14pm UTC
Oct 16, 2016 at 11:22pm UTC
You only need to srand() once, at the start of your program.
cout << "There will be a 60/40 chance at life/death." << endl;
Not necessarily.
if (userInput == deadNum1 || userInput == deadNum2 || userInput == deadNum3 || userInput == deadNum4)
There is chance that two or more of the randomly generated numbers are the same.
1 2 3 4 5 6
int decideFate()
{
int randNum; //To return
randNum = rand() % 11+1;
return randNum;
}
You can shorten it to this:
1 2 3 4
int decideFate()
{
return rand() % 11+1;
}
Likewise with randomPick().
randomInt = rand() % total + 0; //between 0 and 5
Purpose of
+ 0
?
livingVector.erase(livingVector.begin() + randomNumber);
needs to be
livingVector.erase(livingVector.begin() + randomNumber - 1);
1 2 3
livingVector.erase(livingVector.begin() + 1); // 2nd element
livingVector.erase(livingVector.begin() + 2); // 3rd element
// etc...
1 2 3 4 5
//Rand ints of Death
int deadNum1;
int deadNum2;
int deadNum3;
int deadNum4;
Maybe use an array or vector, so that you only have to call decideFate() once and return an array/vector of 4 ints?
Last edited on Oct 16, 2016 at 11:44pm UTC
Oct 17, 2016 at 12:12am UTC
ahhhh I did not even think of two randomly generated number being the same!
For + 0 part i wasnt sure if 0 would be included in the random generation or if it started from 1 so i thought that would be the way to declare it. I also tried to make it so that the -1 would already be acounted for which is why i wanted 0 to be accounted for.
I also commented out the extra srands, but the program didnt run as well and crashes
Thank you so much for your thorough answer! I really appreciate it!!
Last edited on Oct 17, 2016 at 1:12am UTC