-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication1.c
More file actions
142 lines (106 loc) · 2.94 KB
/
Copy pathConsoleApplication1.c
File metadata and controls
142 lines (106 loc) · 2.94 KB
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define TAILLETABLEAU 21
bool verifgagnant(char tableau[3][3]) {
// Checker les lignes
for (int i = 0; i < 3; i++) {
if (tableau[i][0] == tableau[i][1] && tableau[i][1] == tableau[i][2] && tableau[i][0] != ' ') {
printf("Gagne sur la ligne %d\n", (i + 1));
return true;
}
}
// Checker les colonnes
for (int i = 0; i < 3; i++) {
if (tableau[0][i] == tableau[1][i] && tableau[1][i] == tableau[2][i] && tableau[0][i] != ' ') {
printf("Gagne sur la colonne %d\n", (i + 1));
return true;
}
}
// Checker les diagonales
if (tableau[0][0] == tableau[1][1] && tableau[1][1] == tableau[2][2] && tableau[0][0] != ' ') {
printf("Gagne sur la diagonale 1\n");
return true;
}
if (tableau[0][2] == tableau[1][1] && tableau[1][1] == tableau[2][0] && tableau[0][2] != ' ') {
printf("Gagne sur la diagonale 2\n");
return true;
}
return false;
}
int afficherjeux(char table[3][3]) {
printf("y\\x 1 2 3\n");
printf(" /-----------\\\n");
printf("1. | %c | %c | %c |\n", table[0][0], table[0][1], table[0][2]);
printf(" |-----------|\n");
printf("2. | %c | %c | %c |\n", table[1][0], table[1][1], table[1][2]);
printf(" |-----------|\n");
printf("3. | %c | %c | %c |\n", table[2][0], table[2][1], table[2][2]);
printf(" \\-----------/\n");
return 0;
}
bool verifcorrect(int y, int x, char cadrillage[3][3]) {
if (y >= 1 && y <= 3) {
if (x >= 1 && x <= 3) {
if (cadrillage[y - 1][x - 1] == ' ') {
return true;
}
else {
printf("La case est deja occupee !\n");
return false;
}
}
else {
printf("La coordonnee X n'est pas correct");
return false;
}
}
else {
printf("La coordonee Y n'est pas correct");
return false;
}
}
int main() {
bool gagnant;
char tour = 'x';
int coordoneeX;
int coordoneeY;
bool correct;
char cadrillage[3][3] = { {' ', ' ', ' '}, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
int joueurActuel = 0;
char pseudos[2][TAILLETABLEAU];
printf("Nom joueur x : ");
gets_s(pseudos[0], 20);
printf("Nom joueur o : ");
gets_s(pseudos[1], 20);
afficherjeux(cadrillage);
do
{
printf("Au tour du joueur : ");
puts(pseudos[joueurActuel]);
do
{
printf("\nEntrez une coordone X (colone) entre 1 et 3: ");
scanf_s("%d", &coordoneeX);
printf("Entrez une coordone Y (ligne) entre 1 et 3: ");
scanf_s("%d", &coordoneeY);
correct = verifcorrect(coordoneeY, coordoneeX, cadrillage);
} while (!correct);
system("clear");
cadrillage[coordoneeY - 1][coordoneeX - 1] = tour;
afficherjeux(cadrillage);
gagnant = verifgagnant(cadrillage);
if (gagnant) {
printf("Joueur gagnant : %c", tour);
}
if (joueurActuel == 0) {
joueurActuel++;
tour = 'o';
}
else {
joueurActuel == 1;
joueurActuel--;
tour = 'x';
}
} while (!gagnant);
}