-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
70 lines (67 loc) · 2.25 KB
/
main.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
#include <stdio.h>
#include "functions.h"
#include <time.h>
int main() {
char game_board[7][6];
int choice;
Coordinates input_user;
User info;
clock_t start, end;
play:
drain_grid(game_board);
printf("\n");
printf("\t\t************************************ Bienvenue au Puissance 4 ************************************\n\n");
display_winners();
displayGrid(game_board, ' ');
start = clock(); //Timer starts
while (1) {
input_user = input_joueur(game_board, 'X');
displayGrid(game_board, 'X');
hasWon(game_board, input_user, 'X');
if (hasWon(game_board, input_user, 'X') == 1) {
end = clock() - start; //Timer ends
float timer = ((float) end) / CLOCKS_PER_SEC;
printf("Player 1 won!\n");
break;
}
input_user = input_joueur(game_board, 'O');
displayGrid(game_board, 'O');
hasWon(game_board, input_user, 'O');
if (hasWon(game_board, input_user, 'O') == 1) {
end = clock() - start; //Timer ends
printf("Player 2 won!\n");
break;
}
if (tab_full(game_board) == 1) {
end = clock() - start; //Timer ends
printf("Tie match.\n");
break;
}
}
if ((hasWon(game_board, input_user, 'X') == 1) || hasWon(game_board, input_user, 'O') == 1) {
float timer = ((float) end) / CLOCKS_PER_SEC;
float *p_timer = &timer;
printf("Congratulation!\n");
printf("The game lasted %.3f seconds.\n", timer);
register_winner(info, p_timer);
} else if (hasWon(game_board, input_user, 'X') == 2 || hasWon(game_board, input_user, 'O') == 2) {
printf("\nNo one won :(\n");
end = clock() - start; //Timer ends
float timer = ((float) end) / CLOCKS_PER_SEC;
printf("The game lasted %.3f seconds.\n", timer);
} else {
printf("\nError.\n");
}
printf("Would you like to play again? [1] Yes [2] No\nYour choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
goto play;
case 2:
printf("\nGoodbye! :)\n");
return 0;
default:
printf("The entered number is incorrect.\n");
}
return 0;
}