-
Notifications
You must be signed in to change notification settings - Fork 5
/
simon.c
91 lines (83 loc) · 1.87 KB
/
simon.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_BUTTONS 4
#define MAX_SEQUENCE 100
int simon_sequence[MAX_SEQUENCE];
int player_sequence[MAX_SEQUENCE];
int score;
void display_sequence(int sequence[], int length)
{
for (int i = 0; i < length; i++)
{
printf("%d ", sequence[i]);
}
printf("\n");
}
void generate_sequence(int sequence[], int length)
{
srand(time(NULL));
for (int i = 0; i < length; i++)
{
sequence[i] = rand() % NUM_BUTTONS;
}
}
void get_player_sequence(int sequence[], int length)
{
for (int i = 0; i < length; i++)
{
scanf("%d", &sequence[i]);
}
}
int compare_sequence(int seq1[], int seq2[], int length)
{
for (int i = 0; i < length; i++)
{
if (seq1[i] != seq2[i])
{
return 0;
}
}
return 1;
}
void play_game()
{
int sequence_length = 1;
int game_over = 0;
generate_sequence(simon_sequence, sequence_length);
while (!game_over)
{
printf("Simon's sequence:\n");
display_sequence(simon_sequence, sequence_length);
printf("Your turn:\n");
get_player_sequence(player_sequence, sequence_length);
if (compare_sequence(simon_sequence, player_sequence, sequence_length))
{
printf("Correct!\n");
score++;
sequence_length++;
if (sequence_length > MAX_SEQUENCE)
{
printf("You win!\n");
game_over = 1;
}
else
{
generate_sequence(simon_sequence, sequence_length);
}
}
else
{
printf("Incorrect. Final score: %d\n", score);
game_over = 1;
}
}
}
int main()
{
printf("Welcome to Simon!\n");
printf("Press any key to start.\n");
getchar();
play_game();
return 0;
}