forked from mitchgre/life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameOfLife.cpp
154 lines (120 loc) · 2.99 KB
/
gameOfLife.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Author: Gregory Mitchell
Date Created: 11/20/14
Last Modified: 11/23/14
Filename: gameOfLife.cpp
Overview: this is a driver program for two implementations
of Conway's game of life
-------------------------------------------------------*/
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
/*------------------isInt---------------------------
Entry: send a string containing potential integers
Exit: returns true if integer or false if not
Purpose:
force user to enter a valid integer
--------------------------------------------------- */
bool isInt(string input)
{
int integer;
bool returnVal = true;
// initialize char array
char *inputArray = new char[input.length()+1];
strcpy(inputArray, input.c_str()); // copy input to char array
if ( (isdigit(inputArray[0])) || (inputArray[0] == '-') )
{
for (int i = 1; i < strlen(inputArray); i++)
{
if (isdigit(inputArray[i]))
returnVal = returnVal && true;
else
return false;
}
return returnVal;
}
else
{
return false;
}
}
/*------------------string2int---------------------------
Entry: send a string containing integers
Exit: returns a validated integer from string using atoi()
Purpose:
force user to enter a valid integer
--------------------------------------------------- */
int string2int (string input)
{
int integer;
if ( isInt(input) )
{
char *inputArray = new char[input.length()+1];
strcpy(inputArray, input.c_str()); // copy input to char array
integer = atoi(inputArray); // convertt string to int
return integer;
}
}
/*------------------forceInt--------------------------------
Entry: send a string containing a message to prompt for integer
prompts for integer until input is validated
Exit: returns a validated integer
Purpose:
force user to enter a valid integer
---------------------------------------------------------- */
int forceInt (string promptMessage)
{
int integer;
string buffer;
while (1)
{
cout << promptMessage << endl;
getline(cin,buffer);
if (isInt(buffer))
{
integer = string2int(buffer);
return integer; // breaks while loop
}
}
}
int displayMainMenu()
{
int choice;
cout << "1. Use Animations" << endl;
cout << "2. Avoid Animations" << endl;
cout << "3. exit program" << endl;
choice = forceInt("Please enter your choice:");
return choice;
}
void mainMenuLoop()
{
// run make here to ensure that all executable files are in place
system("make");
cout << "Good to go." << endl;
cout << endl;
while (1)
{
int choice = displayMainMenu();
switch (choice)
{
case 1:
system("./gameOfLife-ncurses-menu");
break;
case 2:
system("./gameOfLife-noAnim");
break;
case 3:
exit(1);
default:
cout << "That is not a valid option" << endl;
}
}
}
int main()
{
mainMenuLoop();
return 0;
}