-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.cpp
executable file
·480 lines (461 loc) · 12.8 KB
/
tic tac toe.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*#######################################################################
#///////////////////////////////////////////////////////////////////////#
#/////////|-------------------[TIC TAC TOE]-------------------|/////////#
#/////////| Developer: Christopher Lindblom [BMCE] |/////////#
#/////////| E-mail: christopher.lindblom@spray.se |/////////#
#/////////| Year: 2002 |/////////#
#/////////| Comment: I'm sorry for my swedish comments. |/////////#
#/////////| Feel free to change and use this code as much as |/////////#
#/////////| you please. If you have any questions, just drop |/////////#
#/////////| a mail in my mail box and I'll try to help U. |/////////#
#/////////|---------------------------------------------------|/////////#
#///////////////////////////////////////////////////////////////////////#
#######################################################################*/
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
void server(); // Server delens huvuddel.
void client(); // Client delens huvuddel.
void clear(); // Rensar spelbrädan.
void paint(); // Ritar ut spelbrädan.
void inputter(); // Tar emot inmatning.
void move(); // Flyttar spelbrickorna på spelbrädan.
void quitter(); // Sätter spelloopen till noll.
void check_win(); // Ser efter om man har fått en rad än.
void wait_turn(); // Väntar tills min tur.
void init_server(); // Allt server start skit.
void init_client(); // Allt client start skit.
void move_opponent(); // Flyttar motspelarens gubbar.
void change_player(); // Byter spelare.
void winner_sign();
ifstream read_file; // Read from file-strömmen.
ofstream write_file; // Write to file-strömmen.
char address[5]; // Filnamnet på filen som hinnehåller speldatan.
char player[2]; // Vem som är vem, 0=Spelare 1=Motståndare.
char winner[1]; // Vinnaren.
char board[3][3]; // Vad som är plaserat var på brädan.
char command[5]; // Vilken man vill flytta och vart +vems tur det är.
int command_num[4]; // Kommand fast i intar.
int varv; // Hur många gånger du har mixtrat med dina gubbar.
int rotate; // Ser efter om man ska rotera eller inte.
int counter_move; // En räknare.
int playing; // Spelloopen. 1=Spela 0=Avsluta
int main()
{
cout<<"+--------------------------------------------------+"<<endl;
cout<<"| ### # ## ### # ## ### # ## |"<<endl;
cout<<"| # # # # # # # # # # # # # |"<<endl;
cout<<"| ### # # # # ### # # # # ## ### |"<<endl;
cout<<"| # # # # # # # # # # # # # |"<<endl;
cout<<"| # # ## # # # ## # # ## |"<<endl;
cout<<"| ####### LUFFAR--SCHACK ####### |"<<endl;
cout<<"| ## ########################## ## |"<<endl;
cout<<"+--------------------------------------------------+"<<endl;
char choise[1]; // Svaret på valet sparas i denna choise.
varv=0; // Vi har inte gått nått varv än.
clear();
address[0]='P';
address[1]='L';
address[2]='A';
address[3]='Y';
int scq;
scq=1;
while(scq==1)
{
cout<<"What would you like to be?"<<endl;
cout<<"[S]erver"<<endl;
cout<<"[C]lient"<<endl;
cout<<"[Q]uit"<<endl<<">";
cin>>choise[0];
cout<<endl;
if(choise[0]=='s'||choise[0]=='S')
{
scq=0;
server(); // Startar Tic Tac Toe i Server läge.
}
else if(choise[0]=='c'||choise[0]=='C')
{
scq=0;
client(); // Startar TTT i Client läge.
}
else if(choise[0]=='q'||choise[0]=='Q')
{
scq=0;
}
}
remove("PLAY");
return 0;
}
void client() // Client delens huvud del.
{
init_client();
if(playing==1) // Starten gick bra.
{
paint(); // Så att man slipper sitta och titta på en tom skärm.
}
while(playing==1)
{
wait_turn();
move_opponent();
check_win();
if(winner[0]==player[0]||winner[0]==player[1]) // Om man har vunnit.
{
paint();
quitter();
}
else
{
inputter();
check_win();
if(winner[0]==player[0]||winner[0]==player[1]) // Om man har vunnit.
{
paint();
quitter();
}
else
{
paint();
}
change_player();
}
winner_sign();
}
}
void server() // Server delens huvud del.
{
init_server(); // Gör alla start grejer.
while(playing==1)
{
inputter(); // Tar emot inmatning, flyttar.
check_win(); // Ser efter om man har vunnit.
if(winner[0]==player[0]||winner[0]==player[1]) // Om man har vunnit.
{
paint();
quitter();
}
else
{
paint();
}
change_player(); // Byter spelare.
if(playing==1) // Om jag har vunnit.
{
wait_turn(); // Väntar på att det ska blir jag.
move_opponent(); // Flyttar motspelaren.
check_win(); // Ser efter om motspelaren har vunnit
if(winner[0]==player[0]||winner[0]==player[1]) // Om man har vunnnit.
{
paint();
quitter();
}
}
}
winner_sign();
}
void clear() // Rensar hela banan.
{
int clearer[2];
for(clearer[0]=0;clearer[0]<3;clearer[0]++)
{
for(clearer[1]=0;clearer[1]<3;clearer[1]++)
{
board[clearer[0]][clearer[1]]=' ';
}
}
}
void inputter() // Har hand om inmatning
{
rotate=1;
while(rotate==1)
{
rotate=0;
paint();
if(varv<3) // Kollar om man har placerat ut alla sina brickor.
{
counter_move=2;
cout<<"Where would you like to put your brick?"<<endl<<">XX";
cin>>command[2]>>command[3];
command[0]='0';
command[1]='0';
}
else
{
counter_move=0;
cout<<"Which one and where would you like to move? ([Q]uit)"<<endl<<">";
cin>>command;
}
if(command[0]=='q'||command[0]=='Q') // Ser efter om man har skrivit q.
{
quitter(); // Avlutar spellopen.
rotate=2; // Bara för att han inte ska rammla in i move().
}
else
{
for(;counter_move<4;counter_move++) // Gör om så att det skrivs in rätt siffror i command_num.
{
if(command[counter_move]=='1')
{
command_num[counter_move]=0;
}
else if(command[counter_move]=='2')
{
command_num[counter_move]=1;
}
else if(command[counter_move]=='3')
{
command_num[counter_move]=2;
}
else // Fel aktig inmatning gå ett varv till.
{
rotate=1;
}
}
}
if(rotate==0) // Inmatning korrekt gjord, flytta spelarna.
{
move();
}
}
varv++;
}
void move() // Flyttar om brickorna på spelplanen.
{
if(varv<3) // Om man har gått mindre än 3 varv.
{
if(!(board[command_num[2]][command_num[3]]==' '))
{
rotate=1;
}
else
{
board[command_num[2]][command_num[3]]=player[0];
}
}
else
{
if(!(board[command_num[0]][command_num[1]]==player[0]))
{
rotate=1;
}
else if(!(board[command_num[2]][command_num[3]]==' '))
{
rotate=1;
}
else
{
board[command_num[0]][command_num[1]]=' ';
board[command_num[2]][command_num[3]]=player[0];
}
}
}
void quitter() // Sätter spelloopen till noll.
{
playing=0;
}
void paint() // Ritar ut brädan
{
cout<<endl<<" - The board -"<<endl;
cout<<" =====["<<player[0]<<"]====="<<endl;
cout<<"\t 1 2 3 "<<endl;
cout<<"\t -------"<<endl;
cout<<"\t1|"<<board[0][0]<<"|"<<board[0][1]<<"|"<<board[0][2]<<"|"<<endl;
cout<<"\t -------"<<endl;
cout<<"\t2|"<<board[1][0]<<"|"<<board[1][1]<<"|"<<board[1][2]<<"|"<<endl;
cout<<"\t -------"<<endl;
cout<<"\t3|"<<board[2][0]<<"|"<<board[2][1]<<"|"<<board[2][2]<<"|"<<endl;
cout<<"\t -------"<<endl;
}
void check_win() // Ser efter om man har fått en rad än.
{
for(counter_move=0;counter_move<2;counter_move++)
{
if(board[0][0]==player[counter_move]&&board[0][1]==player[counter_move]&&board[0][2]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[1][0]==player[counter_move]&&board[1][1]==player[counter_move]&&board[1][2]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[2][0]==player[counter_move]&&board[2][1]==player[counter_move]&&board[2][2]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[0][0]==player[counter_move]&&board[1][1]==player[counter_move]&&board[2][2]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[0][0]==player[counter_move]&&board[1][0]==player[counter_move]&&board[2][0]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[0][1]==player[counter_move]&&board[1][1]==player[counter_move]&&board[2][1]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[0][2]==player[counter_move]&&board[1][2]==player[counter_move]&&board[2][2]==player[counter_move])
{
winner[0]=player[counter_move];
}
else if(board[0][2]==player[counter_move]&&board[1][1]==player[counter_move]&&board[2][0]==player[counter_move])
{
winner[0]=player[counter_move];
}
}
}
void wait_turn() // Väntar tills det blir min tur.
{
counter_move=0;
cout<<"Wait.."<<endl;
while(counter_move==0)
{
read_file.open(address);
read_file>>command;
read_file.close();
if(command[4]==player[0])
{
counter_move=1;
}
}
}
void init_server() // Start grejerna.
{
playing=1; // Sätter spelloopen till ett.
while(!(player[0]=='X'||player[0]=='O'||player[0]=='x'||player[0]=='o')) // Val av spelare
{
cout<<"Which character would you like to be?"<<endl;
cout<<"[X]"<<endl;
cout<<"[O]"<<endl<<">";
cin>>player[0];
}
cout<<"Waiting for other player to connect."<<endl;
if(player[0]=='x') // Storbokstav - X
{
player[0]='X';
player[1]='O';
}
if(player[0]=='o') // Storbokstav - O
{
player[0]='O';
player[1]='X';
}
write_file.open(address);
write_file<<"0000"<<player[1];
write_file.close();
counter_move=1;
while(counter_move==1) // Väntar på att motståndare ska joina.
{
read_file.open(address);
read_file>>command;
read_file.close();
if(command[4]==player[0])
{
counter_move=0;
}
}
cout<<endl;
}
void move_opponent() // Flyttar motståndarens gubbar.
{
if(command[0]=='0'&&command[1]=='0')
{
counter_move=2;
command_num[0]=4;
}
else
{
counter_move=0;
}
if(!(counter_move==3)) // Om han inte har vunnit så matas data in i command_num.
{
for(;counter_move<4;counter_move++) // Gör om så att det skrivs in rätt siffror i command_num.
{
if(command[counter_move]=='1')
{
command_num[counter_move]=0;
}
else if(command[counter_move]=='2')
{
command_num[counter_move]=1;
}
else if(command[counter_move]=='3')
{
command_num[counter_move]=2;
}
}
if(command_num[0]==4) // Placerar ut ny gubbe.
{
board[command_num[2]][command_num[3]]=player[1];
}
else // Flyttar gubbe.
{
board[command_num[0]][command_num[1]]=' ';
board[command_num[2]][command_num[3]]=player[1];
}
}
check_win();
}
void init_client() // Allt som har med uppstarten av clienten.
{
read_file.open(address);
read_file>>command;
read_file.close();
if(command[0]=='0'&&command[1]=='0'&&command[2]=='0'&&command[3]=='0')
{
playing=1;
player[0]=command[4];
if(player[0]=='O')
{
player[1]='X';
}
else
{
player[1]='O';
}
write_file.open(address);
write_file<<"0000"<<player[1];
write_file.close();
}
else
{
cout<<"Unable to connect to server..\a"<<endl;
playing=0;
}
}
void change_player()
{
command[4]=player[1];
write_file.open(address);
write_file<<command;
write_file.close();
}
void winner_sign()
{
char winner_char[1];
if(winner[0]==player[0]) // Skriver ut text om nån förlorat.
{
cout<<"+--------------------------------------------------+"<<endl;
cout<<"| ## ## ## ### ## ### ## ##### #### ## |"<<endl;
cout<<"| ## ## #### ## #### ## # ## ## ## |"<<endl;
cout<<"| ## ## ## ## ## ## ## ## ## ### ##### ## |"<<endl;
cout<<"| ## ## ## ## ## #### ## #### # ### ## |"<<endl;
cout<<"| ### ### ## ## ### ## ### ##### #### |"<<endl;
cout<<"| ## ## ## ## ## ## ## ## ## ## |"<<endl;
cout<<"+--------------------------------------------------+"<<endl;
cout<<"Type something and press [ENTER] to end game..";
cin>>winner_char[0];
}
else if(winner[0]==player[1])
{
cout<<"+--------------------------------------------------+"<<endl;
cout<<"| ### #### #### ####### #### ## |"<<endl;
cout<<"| ### ## ## ## ## #### ## ## ## ## |"<<endl;
cout<<"| ### ## ## ## ## ## #### ##### ## |"<<endl;
cout<<"| ### ## ## ## ## #### #### ### ## |"<<endl;
cout<<"| ####### ## ## ## ## ## ## #### |"<<endl;
cout<<"| ####### #### #### #### ####### ## ## ## |"<<endl;
cout<<"+--------------------------------------------------+"<<endl;
cout<<"Type something and press [ENTER] to end game..";
cin>>winner_char[0];
}
}