Original Game Idea (Simon Says Maths)

Hey, i'm fairly new to programming but I haven't seen this one before,
i'm just posting for some constructive criticism, any ideas for things to add or things that should probably be done differently?

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
//Simon Says Maths!
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>

using namespace std;

int simonChecks(int siSays, int userGuess, int sum);
void simonSays(int siSays);
int sumNumbers(int num1,int num2,int operation);
void operatorSelection(int opSelect);

int main() {
    const int MAX_TURNS = 5;
    int lives = 3, num1, num2, opSelect, currentTurns, siSays, score = 0, extraLife = 0;
    int sum, userGuess;
    srand(time(0));
    cout << "You are starting with " << lives << " lives.\n";
    Sleep(2000);
    cout << "Follow Simons orders carefully!\n";
    Sleep(2000);
    cout << "Enter '0' if the answer is Against Simons Rule!\n";
    Sleep(2000);
    cout << "When you think you're Ready! Hit Enter to Start! ;)";
    cin.ignore();
    for(;;) {
        Sleep(500);
        system("CLS");
        if (lives == 0) break;
        currentTurns = (rand()%MAX_TURNS)+1;
        siSays = rand()%2;
        simonSays(siSays);
        for(int i = 0;i < currentTurns;i++) {
            num1 = rand()%10;
            num2 = rand()%10;
            opSelect = rand()%3;
            sum = sumNumbers(num1,num2,opSelect);
            cout << "What is " << num1;
            operatorSelection(opSelect);
            cout << num2 << " = ";
            cin >> userGuess;
            userGuess = simonChecks(siSays, userGuess, sum);
            if (userGuess == sum){
               cout << "Correct!\n"; 
               extraLife++;
               score++;    
               Sleep(500);
               if (extraLife == 15) {
                  lives++;
                  cout << "You gained an Extra Life!\n";
                  Sleep(1500);
                  cout << "You now have " << lives << " lives!\n";
                  Sleep(2500);
                  extraLife = 0;         
               }
            } else {  
               lives--;
               cout << "You have " << lives << " lives remaining!\n";
               Sleep(500);
               if (lives == 0) break;
            }
        }
    }
    cout << "GAME OVER! You Have No Lives Left!\n";
    Sleep(2000);
    cout << "Your final score is: " << score;
    Sleep(3000);
}

int sumNumbers(int a, int b, int op) {
    int sum;
    switch (op) {
        case 0:
             sum = a*b;
             break;
        case 1:
             sum = a+b;
             break;
        case 2:
             sum = a-b;
             break;
        default:
             cout << "Error: Sum calculator broken!";       
    }    
    return sum;
}

void operatorSelection(int opSelect) {
     switch (opSelect) {
         case 0:
              cout <<  " * ";
              break;
         case 1:
              cout << " + ";
              break;
         case 2:
              cout << " - ";
              break;
         default:
              cout << "Error: Broken Operator!";
     }     
}

int simonChecks(int siSays, int userGuess, int sum) {
    if (siSays == 1) { // Odds
       if (sum%2 != 0) { //if it is an Odd answer
          return userGuess;                
       } else if (sum%2 == 0 && userGuess == 0) {
          return sum; 
       } else if (sum%2 == 0 && userGuess != 0) {
          cout << "You went against Simons Rule!\n";  
          if (sum != -1) {
             return -1;       
          } else {
             return -2;       
          }
       } else if (sum != -1) {
          return -1;       
       } else {
          return -2;       
       }
    }
    if (siSays == 0) { // Evens
       if (userGuess%2 == 0) {
          return userGuess;                
       } else if (sum%2 != 0 && userGuess == 0){
          return sum;
       } else if (sum%2 != 0 && userGuess != 0){
          cout << "You went against Simons Rule!\n";
          if (sum != -1) {
             return -1;       
          } else {
             return -2;       
          }       
       } else if (sum != -1){
          return -1;       
       } else {
          return -2;       
       }
    }
}

void simonSays(int siSays) {
     if (siSays == 1) {
       cout << "SIMON SAYS: ANSWER ODDS ONLY!\n";           
    } else if (siSays == 0) {
       cout << "SIMON SAYS: ANSWER EVENS ONLY!\n";       
    } else {
       cout << "Error: Odd/Even Selection!\n";       
    }
}


Thanks for reading,
please go easy on me, Lostsoulparty.
It is a bad style of programming to use such numerous declarations. Usually Straustrup demonstrates such a bad style in his books on programming.:)

1
2
    int lives = 3, num1, num2, opSelect, currentTurns, siSays, score = 0, extraLife = 0;
    int sum, userGuess;


These declarations say nothing to the user that reads the code. Are all declared variables used in the same algorithmic task of the program or they are used in different algorithmic tasks of the program? Why is score initialized by 0? What is the magic number 3?
You should declare variables before their immediate usage.
Last edited on
Thanks for the advice and I hadn't even noticed that i'd put 2 different lines of int declarations haha. I will improve the code! :)
Topic archived. No new replies allowed.