Simon game function error

I was trying to make a simon memory game, when I noticed my code was turning to spaghetti. Trying to clean it up, I added more functions, and broke my code. My original code would compile, but my cleaner code has a reference error in regards to " playGame() ", and I can't tell why.

Difference in code is in playGame().

clean(er) code:
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
// Simon test

#include <stdio.h>
#include <stdlib.h> // For system
#include <unistd.h> // For sleep

#define MAXLEN 1024

// why is this looping twice on first iteration
int guess(int val[], int indexMax) {
    int i = 0;
    int guessVal = 0;
    for (i = 0; i <= indexMax; i++) {
        printf("i (low) val is %d\n", i);
        printf("max is %d\n", indexMax);
        printf("Guess %d >> ", i);
        scanf("%d", &guessVal);
        putc('\n', stdout);
        if (guessVal != val[i])
            return 0;
    }
    return 1;
}

void setVal(int val[], int index) {
    val[index] = rand() / (double)RAND_MAX * 4 + 1;
}

void playValueReel(int val[], double speed, int indexMax) {
    int j = 0;
    for (j = 0; j <= indexMax; j++) {
            printf("i (high) value is %d\n", indexMax);
            printf("Value %d is %d\n", j, val[j]);
            sleep(speed);
//            system("cls");
}

int playGame(double speed) {
    int i = 0; // for setting val
    int val[1024];
    for (i = 0; i < MAXLEN; i++) {
        setVal(val, i);
        playValueReel(val, speed, i);
            if (!(guess(val, j)))
                return i;
        }
    }
}

double diffSelect() {
    int dif = 0;
    printf("Select difficulty (speed):\n");
    puts("1. Easy   (1.0 sec)");
    puts("2. Medium (0.5 sec)");
    puts("3. Hard   (0.25 sec)");
    printf("Enter difficulty >> ");
    scanf("%d", &dif);
    system("cls");
    if (dif == 1)
        return 1;
    else if (dif == 2)
        return .5;
    else if (dif == 3)
        return .25;
    else
        return -1;
}

int main(void) {
    puts("Simon Game test V.1\n");
    printf("Score: %d", playGame(diffSelect())); //playGame() call here
    return EXIT_SUCCESS;
}


original code:
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
// Simon test

#include <stdio.h>
#include <stdlib.h> // For system
#include <unistd.h> // For sleep

#define MAXLEN 1024

// why is this looping twice on first iteration
int guess(int val[], int indexMax) {
    int i = 0;
    int guessVal = 0;
    for (i = 0; i <= indexMax; i++) {
        printf("i (low) val is %d\n", i);
        printf("max is %d\n", indexMax);
        printf("Guess %d >> ", i);
        scanf("%d", &guessVal);
        putc('\n', stdout);
        if (guessVal != val[i])
            return 0;
    }
    return 1;
}

void setVal(int val[], int index) {
    val[index] = rand() / (double)RAND_MAX * 4 + 1;
}

int playGame(double speed) {
    int i = 0; // for setting val
    int j = 0; // second counter for prints
    int val[1024];
    for (i = 0; i < MAXLEN; i++) {
        setVal(val, i);
                for (j = 0; j <= i; j++) {
            printf("i (high) value is %d\n", i);
            printf("Value %d is %d\n", j, val[j]);
            sleep(speed);
//            system("cls");
//            if (j <= i) // if guess number is less than total numbers
            if (!(guess(val, j)))
                return i;
        }
    }
}

double diffSelect() {
    int dif = 0;
    printf("Select difficulty (speed):\n");
    puts("1. Easy   (1.0 sec)");
    puts("2. Medium (0.5 sec)");
    puts("3. Hard   (0.25 sec)");
    printf("Enter difficulty >> ");
    scanf("%d", &dif);
    system("cls");
    if (dif == 1)
        return 1;
    else if (dif == 2)
        return .5;
    else if (dif == 3)
        return .25;
    else
        return -1;
}

int main(void) {
    puts("Simon Game test V.1\n");
    printf("Score: %d", playGame(diffSelect()));  //playGame call here
    return EXIT_SUCCESS;
}
Last edited on
closed account (E0p9LyTq)
Look at line 44 in your "cleaner" code. Where are you defining the variable j in your playGame() function?

There is also a serious mismatch of parentheses in the function. One too many closing }.
Last edited on
playValueReel is missing a brace (to end the for loop) and playGame has an extra one, so playGame is essentially being defined INSIDE of playValueReel (which is not allowed in standard C).

EDIT (after reading FurryGuy): Interestingly playGame is using playValueReel's j variable, too!
Last edited on
Oh geez, thanks guys. Was not finding the error, but these catches will help me resolve them.
Topic archived. No new replies allowed.