school/cs235/notes/game.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

2016-04-06 20:46:10 -07:00
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
using namespace std;
vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
vector<string> results;
string s;
for(unsigned int i = 0; i < expression.length(); i++) {
char c = expression[i];
if(c != ' ') {
s += c;
}
else {
if(s != "") {
results.push_back(s);
s.clear();
}
}
}
if(s != "") {
results.push_back(s);
}
return results;
}
bool has_a(vector<string> list, string name) {
for(unsigned int i = 0; i < list.size(); i++) {
if(list[i] == name)
return true;
}
return false;
}
int main() {
srand(time(NULL));
cout << "Please enter names with a space inbetween each player: ";
string names;
getline(cin, names);
vector<string> list = parse_expression(names);
vector<string> result;
while(result.size() != list.size()) {
int rand_m;
while(rand_m != 50) {
rand_m = rand() % 10000;
}
int rand_n = rand() % list.size();
string adding = list[rand_n];
if(!has_a(result, adding)) {
result.push_back(adding);
}
}
cout << "This is the order: " << endl;
for(unsigned int i = 0; i < result.size(); i++) {
cout << i + 1 << ") " << result[i] << endl;
}
}