-
Notifications
You must be signed in to change notification settings - Fork 5
/
memory-game.c
179 lines (162 loc) · 4.04 KB
/
memory-game.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_SIZE 16
// Function declarations
void initialize_board(int board[], int board_size);
void print_board(int board[], int board_size);
void shuffle_board(int board[], int board_size);
int get_card_choice(int board[], int board_size, int choices[]);
int check_match(int board[], int card1, int card2);
void mark_cards(int board[], int card1, int card2);
// Main function
int main()
{
int board[BOARD_SIZE];
int choices[2];
int matches = 0;
int attempts = 0;
// Seed random number generator
srand(time(NULL));
// Initialize the board
initialize_board(board, BOARD_SIZE);
// Shuffle the board
shuffle_board(board, BOARD_SIZE);
// Play the game
while (matches < BOARD_SIZE / 2)
{
// Print the board
print_board(board, BOARD_SIZE);
// Get the player's card choices
choices[0] = get_card_choice(board, BOARD_SIZE, choices);
choices[1] = get_card_choice(board, BOARD_SIZE, choices);
// Check if the cards match
if (check_match(board, choices[0], choices[1]))
{
printf("Match!\n");
mark_cards(board, choices[0], choices[1]);
matches++;
}
else
{
printf("No match.\n");
}
// Increment the number of attempts
attempts++;
}
// Game over
printf("Congratulations! You won in %d attempts.\n", attempts);
return 0;
}
// Function to initialize the board with pairs of numbers
void initialize_board(int board[], int board_size)
{
int i;
for (i = 0; i < board_size; i += 2)
{
board[i] = i / 2 + 1;
board[i + 1] = i / 2 + 1;
}
}
// Function to print the board
void print_board(int board[], int board_size)
{
int i;
printf(" ");
for (i = 0; i < board_size; i++)
{
printf("----");
}
printf("\n");
for (i = 0; i < board_size; i++)
{
printf("|");
if (board[i] == 0)
{
printf(" ");
}
else
{
printf("%2d", board[i]);
}
printf(" ");
if (i == board_size - 1)
{
printf("|\n");
}
else if ((i + 1) % 4 == 0)
{
printf("|\n ");
int j;
for (j = 0; j < 4; j++)
{
printf("----");
}
printf("\n");
}
}
printf(" ");
for (i = 0; i < board_size; i++)
{
printf("----");
}
printf("\n");
}
// Function to shuffle the board
void shuffle_board(int board[], int board_size)
{
int i, j, temp;
for (i = board_size - 1; i > 0; i--)
{
j = rand() % (i + 1);
temp = board[i];
board[i] = board[j];
board[j] = temp;
}
}
// Function to get the player's card choice
int get_card_choice(int board[], int board_size, int choices[])
{
int card;
do
{
printf("Enter a card number (1-%d): ", board_size / 2);
scanf("%d", &card);
if (card < 1 || card > board_size / 2)
{
printf("Invalid card number. Please try again.\n");
}
else if (board[(card - 1) * 2] == 0 && board[(card - 1) * 2 + 1] == 0)
{
printf("Card already matched. Please choose another card.\n");
}
else if (choices[0] != -1 && choices[1] != -1 && card != choices[0] && card != choices[1])
{
printf("You can only choose two cards at a time. Please choose again.\n");
}
else
{
return card;
}
} while (1);
}
// Function to check if two cards match
int check_match(int board[], int card1, int card2)
{
if (board[(card1 - 1) * 2] == board[(card2 - 1) * 2])
{
return 1;
}
else
{
return 0;
}
}
// Function to mark cards as matched
void mark_cards(int board[], int card1, int card2)
{
board[(card1 - 1) * 2] = 0;
board[(card1 - 1) * 2 + 1] = 0;
board[(card2 - 1) * 2] = 0;
board[(card2 - 1) * 2 + 1] = 0;
}