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
|
#include <iostream>
#include <vector>
using namespace std ;
int main (int argc, char * const argv [] ) {
// Method declarations
void pseudoMain () ;
pseudoMain () ;
// Return before end of script
cout << endl << endl ;
return 0 ;
}
void pseudoMain () {
// Variable declarations
char compFirstChar ;
bool compFirst ;
bool gameOver ;
int poisonNum ;
int max ;
int lastNum ;
int midlNum ;
int enteredNum = 1 ;
vector <int> stepStones ;
// Method declarations
void restart () ;
void error (int errorCode) ;
int determineStepStones (int stepStonesArray [] , int poisonNum, int max) ;
int display (int lastNum, int enteredNum) ;
int compTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, vector <int> stepStones) ;
int userTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, vector <int> stepStones) ;
// Set the variables
cout << "Would you like the computer to go first (y|n) ? " ;
cin >> compFirstChar ;
switch (compFirstChar) {
case 'y' :
compFirst = true ;
break ;
case 'n' :
compFirst = false ;
break ;
default:
error (1) ;
break ;
}
cout << "Please enter a positive number to be the “poison” number: " ;
cin >> poisonNum ;
cout << "Please enter a positive number to be the maximum number of numbers that a player may go through at any one time: " ;
cin >> max ;
stepStones.resize (poisonNum / max) ;
determineStepStones (vector <int> stepStones, poisonNum, max) ;
// Start the game
system ( "clear" ) ;
if (compFirst == true) { lastNum = compTurn (poisonNum, max, lastNum, midlNum, enteredNum, vector <int> stepStones) ; }
while (gameOver == false) {
if (gameOver == false) { lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, vector <int> stepStones) ; }
else { break ; }
if (gameOver == false) { lastNum = compTurn (poisonNum, max, lastNum, midlNum, enteredNum, vector <int> stepStones) ; }
else { break ; }
}
}
void restart () {
cout << endl << "Restarting script…" << endl ;
system ( "clear" ) ;
pseudoMain () ;
}
void error (int errorCode) {
switch (errorCode) {
case 0:
cout << "Error 0 (983-853) : an unknown error has occured." ;
restart () ;
break ;
case 1:
cout << "Error 1 (96-483.23) : you did not enter a lowercase 'y' (for yes) or a lowercase 'n' (for no) when I asked you if you wanted the computer to go first. Remember, computers are picky, so you must be pseudoperfect!" << endl ;
restart () ;
break ;
case 2:
cout << "Error 2 (46-3662) : the number you have entered is not acceptable. You must enter a number between the two numbers given." ;
break ;
default:
error (0) ;
break ;
}
}
int determineStepStones (vector <int> stepStones, int poisonNum, int max) {
// This is backwards, but the resulting vector makes more sense.
// Sets the last “stepping stone” number to what it is (the “poison” number minus one)
stepStones [poisonNum / max] = poisonNum - 1 ;
// Sets all of the rest of the “stepping stone” numbers to what they are (the previous “stepping stone” number minus max)
for (int i = poisonNum / max ; i >= 0 ; i -- ) { stepStones [i] = stepStones [i - 1] - (max + 1) ; }
}
int display (int lastNum, int enteredNum) {
cout << endl ;
for (int i = lastNum ; i <= enteredNum ; i ++ ) {
cout << i << ", " ;
}
cout << enteredNum << endl ;
}
int compTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, vector <int> stepStones) {
enteredNum = midlNum ;
for (int i = enteredNum ; i < enteredNum + max ; i ++ ) {
for (int j = 0 ; j < poisonNum / max ; j ++ ) {
if (i == stepStones [j] ) {
display (lastNum, enteredNum) ;
break ;
}
}
}
midlNum = lastNum ;
return lastNum ;
}
int userTurn (int poisonNum, int max, int lastNum, int midlNum, int enteredNum, vector <int> stepStones) {
enteredNum = midlNum ;
cout << "Enter a number above " << lastNum << " and below " << lastNum + max + 1 << " (exclusive) : " ;
cin >> enteredNum ;
if (lastNum < enteredNum <= lastNum + max) { display (lastNum, enteredNum) ; }
else {
error (2) ;
midlNum = enteredNum ;
lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, vector <int> stepStones) ;
return lastNum ;
}
midlNum = lastNum ;
return lastNum ;
}
|