Bonjour a toutes et a tous ,
J'aurai voulu qu'a chaque debut de partie , la personne qui commence soit différente . J'ai fait une fonction pour que cela soit aleatoire mais je n'obtiens pas le resultat escompté : Au lieu du 'T' ou du 'L' en retour de ma fonction , j'obtiens un point d'interogation dans un hexagone.
Ma fonction gen_random ne doit pas etre bien construite et peut etre est il possible de faire plus simple .
Merci de votre aide .
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char player; int count; char gen_random(){ char choix[2] = { 'L', 'T' }; char res; for (int i = 0; i < 2; i++) res = res + choix[rand() % 2]; return res; } void Draw() { cout << " | | " << endl; cout << " " << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << matrix[1][0] << " | " << matrix[1][1] << " | " <<matrix[1][2] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << matrix[2][0] << " | " << matrix[2][1] << " | " << matrix[2][2] << endl; cout << " | | " << endl << endl; } void Input() { srand(time(NULL)); player=gen_random(); cout << "Its "<<player<<" turn's Enter the number of the field: "; int a; cin >> a; if (a == 1) if (matrix[0][0]=='1') matrix[0][0] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 2) if (matrix[0][1]=='2') matrix[0][1] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 3) if (matrix[0][2]=='3') matrix[0][2] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 4) if (matrix[1][0]=='4') matrix[1][0] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 5) if (matrix[1][1]=='5') matrix[1][1] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 6) if (matrix[1][2]=='6') matrix[1][2] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 7) if (matrix[2][0]=='7') matrix[2][0] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 8) if (matrix[2][1]=='8') matrix[2][1] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } else if (a == 9) if (matrix[2][2]=='9') matrix[2][2] = player; else { cout << "\nInvalid move :-( \n"; count=count-1; } } void cuser() // { if (player == 'L') //condition { player = 'T'; } else { player = 'L'; } } char Winner() { if (matrix[0][0] == 'L' && matrix[0][1] == 'L' && matrix[0][2] == 'L') return 'L'; if (matrix[1][0] == 'L' && matrix[1][1] == 'L' && matrix[1][2] == 'L') return 'L'; if (matrix[2][0] == 'L' && matrix[2][1] == 'L' && matrix[2][2] == 'L') return 'L'; if (matrix[0][0] == 'L' && matrix[1][0] == 'L' && matrix[2][0] == 'L') return 'L'; if (matrix[0][1] == 'L' && matrix[1][1] == 'L' && matrix[2][1] == 'L') return 'L'; if (matrix[0][2] == 'L' && matrix[1][2] == 'L' && matrix[2][2] == 'L') return 'L'; if (matrix[0][0] == 'L' && matrix[1][1] == 'L' && matrix[2][2] == 'L') return 'L'; if (matrix[2][0] == 'L' && matrix[1][1] == 'L' && matrix[0][2] == 'L') return 'L'; if (matrix[0][0] == 'T' && matrix[0][1] == 'T' && matrix[0][2] == 'T') return 'T'; if (matrix[1][0] == 'T' && matrix[1][1] == 'T' && matrix[1][2] == 'T') return 'T'; if (matrix[2][0] == 'T' && matrix[2][1] == 'T' && matrix[2][2] == 'T') return 'T'; if (matrix[0][0] == 'T' && matrix[1][0] == 'T' && matrix[2][0] == 'T') return 'T'; if (matrix[0][1] == 'T' && matrix[1][1] == 'T' && matrix[2][1] == 'T') return 'T'; if (matrix[0][2] == 'T' && matrix[1][2] == 'T' && matrix[2][2] == 'T') return 'T'; if (matrix[0][0] == 'T' && matrix[1][1] == 'T' && matrix[2][2] == 'T') return 'T'; if (matrix[2][0] == 'T' && matrix[1][1] == 'T' && matrix[0][2] == 'T') return 'T'; return '/'; //fun } int main() { count=0; cout << "*****************************************************************************\n"; cout << "\n Tic Tac Game \n\n"; cout << "*****************************************************************************\n\n"; Draw(); while (1) { count++; Input(); Draw(); if (Winner() == 'L') { cout << "_____________________________RESULT_________________________________________\n\n"; cout << " Player Lauralee wins! :-)\n\n"; break; } else if (Winner() == 'T') { cout << "_____________________________RESULT___________________________________________\n\n"; cout << " Player Théo wins! :-) \n\n"; break; } else if (Winner() == '/'&& count==9) { cout << "_____________________________RESULT___________________________________________\n\n"; cout << "Draw please try again :-( \n\n"; break; } cuser(); } return 0; }
Partager