school/cs142/test02/Circle.cpp
2016-04-06 20:45:34 -07:00

127 lines
2.8 KiB
C++

#include <iostream>
#include <vector>
using namespace std;
#include "Circle.h"
Circle::Circle(){}
vector<string> Circle::getNames() { //used to populate a vector with names
string special = "Josephus";
vector<string> names = {{
"Jacob",
"Isabella",
"Ethan",
"Sophia",
"Michael",
"Emma",
"Jayden",
"Olivia",
"William",
"Ava",
"Alexander",
"Emily",
"Noah",
"Abigail",
"Daniel",
"Madison",
"Aiden" ,
"Chloe",
"Anthony",
}};
return names;
}
vector<string> Circle::playGame(int n, int m) {
vector<string> names = getNames();
vector<string> temp;
if(n < 10 or n > 20){
names = {};
return names;
}
if(m < 0){
names = {};
return names;
}
for(int i = 0; i < 20 - n; i++) {
names.pop_back();
}
names.push_back("Josephus"); //insures that Josephus will be in the game
int count = 0;
int threshold = 0;
for(int i = 0; i < n; i++) {
int hits = 0;
while(hits < m) {
if(names[count] == ""){
}
else {
hits++;
}
if(hits == m) {
temp.push_back(names[count]);
names[count] = "";
hits = 100;
}
count++;
if(count+1 > names.size()) {
count = 0;
threshold++;
}
if(threshold > 100) { //threshold against infinite loop
hits = 100;
}
}
}
return temp;
}
int Circle::reportSafeIndex(int n, int m) { //uses similar function as playGame() to determine index
vector<string> names = getNames();
vector<string> temp;
int count = 0;
int threshold = 0;
if(n < 10 or n > 20){
return -1;
}
if(m < 0){
return -1;
}
for(int i = 0; i < 20 - n; i++) {
names.pop_back();
}
names.push_back("Josephus");
for(int i = 0; i < n; i++) {
int hits = 0;
while(hits < m) {
if(names[count] == ""){
}
else {
hits++;
}
if(hits == m) {
temp.push_back(names[count]);
names[count] = "";
hits = 100;
}
count++;
if(count+1 > names.size()) {
count = 0;
threshold++;
}
if(threshold > 100) { //incase of infinite loop due to improper input
hits = 100;
}
}
}
string index_holder = temp[n-1];
int index = 0;
names = getNames();
for(unsigned int i = 0; i < names.size(); i++){
if(names[i] == index_holder) {
index = i+1;
}
}
return index;
}