-->





Tic Tac Toe c++ game project source code and documentation

Here we will learn how to develop a tic tac toe multiplayer game project in ะก++ programming language with source code

Introduction
Let's assume you are a beginner in the c++ programming language and looking for a detailed guide with source code for the tic tac toe game.

This post will definitely help you to guide the complete process of development with a detailed explanation of code.

This C++ Game project is developed using C++ code blocks IDE. It has a defined board size of 5X5 with two modes.

1. Player vs Player. 2. Player vs Computer

C++ Concepts used in this c++ code project:

Code Explanation
In this c++ game, there are 5 global variables and 11 functions. Every function defines a separate different action in the game. The practice of defining functions for a separate action or related work always helps to keep the code clean, readable, and also trackable.

Variables
#define BoardSize 5
char Player1Move = 'X';
char Player2Move = 'O';
int p1 = 1;
int p2 = 2;

Functions

void menu();
void printBoard();
void input(int);
void player1();
void player2();
void p2p();
bool check();
bool defend(int, int, int);
bool attack();
void computerMoves(int);
void p2c();


2D Arrays

char board[BoardSize][BoardSize];


int b[BoardSize][BoardSize] = { 0 }; How code works?
"board" and "b" are two arrays, the board represents X for player 1 and O for player 2. 'b' is used only in player vs Computer game represents 1 for the first player and 2 for the second player which helps to build logic for computer moves.

Global variables description
  1. BoardSize defines the tic tac toe game board size of 5
  2. player1Move defines the symbol for player 1 which is 'X'
  3. player2Move defines the symbol for player 2 which is 'O'
  4. p1 represents value for the first player, it will be 1 or 2
  5. p2 represents value for the second player, it will be 1 or 2
  6. p1 and p2 are the variables that help to build the logic for the player to computer module
  7. player will select as a 1st or 2nd turn

Functions description

menu
simple menu function shows and manage three options

player vs player
player vs computer
exit
according to the user choice call-related function.

printBoard
print the 5-by-5 board on the console.

input(int)
Takes input from user in both cases, player to player(form both players) and in the player to computer(only form player).
Asks the user to enter row number and column number for example 1 and 1 mean first cell of the board

player1
Calls in the case of player to player for player 1 move simply clear the screen first and then print the marker sign of player and then call above described input function.

player2
Calls in the case of a player to player for player 2 move simply clear the screen first and then print the marker sign of player and then call above described input function.

p2p
Calls player1 and player2 functions one by one and calls check function after every turn of the player e.g check function call when player1 place his/her marker on the board.

check

Checks the status of the game after every placement of the new marker this function will return true when all marks of the same type are in any row, column, or diagonally, otherwise, this function will return false.

defend
This function is for computer vs player move and called for computer move in this function computer will check the first all rows, columns and diagonals.

If the computer is having a possible winning move. Take the step and wins and also Checks if the player (opponent) has a possible winning move, takes a step to block the player.

attack aka Attacking move

Take a step such that it has winning possibilities in the next moves. place the marker in such a row or column which contains computer's marker more.

computerMove

Calls first defend function, check any possibility to win in this move, then check any possibility for the player wins.

If find any possibility to win or defend, place the marker at that place but if couldn't find then return false and if defend function return false then call the attack function.

if any cell is empty which may help the computer to win in the next moves then place a marker at that point but if not found then place any cell empty in the board.

p2c
first asks the user to play as a first or second player then according to the user's choice call the first turn for computer or player after every turn call the check function to determine the winning of any player, and when board filled, draw the game and return to the main menu.

C++ Source Code
  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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include<iostream>
#include<Windows.h>
#include<time.h>
#include<cstdlib>
#define BoardSize 5
char Player1Move = 'X';
char Player2Move = 'O';
int p1 = 1;
int p2 = 2;
void menu();
void printBoard();
void input(int);
void player1();
void player2();
void p2p();
bool check();
bool defend(int, int, int);
bool attack();
void computerMoves(int);
void p2c();
using namespace std;
char board[BoardSize][BoardSize];
int b[BoardSize][BoardSize] = { 0 }; // additional array for efficient computer Vs Human Game.
int main()
{
	menu();
	system("pause");
	return 0;
}
//Main Menu Function
void menu()
{
	for (int i = 0; i < BoardSize; i++)
	{
		for (int j = 0; j < BoardSize; j++)
		{
			board[i][j] = '\0';
		}
	}
	system("CLS");
	int choice;
	cout << "\n\t\t\t\t\tTic Tac Toe Game";
	cout << "\n\n\t\t\t\t1.Human Player VS Human Player";
	cout << "\n\t\t\t\t2.Human Player VS Computer";
	cout << "\n\t\t\t\t3.Exit\n";
	cin >> choice;
	switch (choice)
	{
	case 1:
		printBoard();
		p2p();
		break;
	case 2:
		p2c();
		break;
	case 3:
		exit(1);
	default:
		cout << "Please enter a valid choice(1, 2 or 3)\n";
		system("pause");
		menu();
	}
}
//Function for display board
void printBoard()
{
	system("CLS");
	cout << "\n\n\n\n";
	cout << "\t\t\t\t   1     2     3     4     5   \n";
	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t      1 |  " << board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] << "  |  " << board[0][3] << "  |  " << board[0][4] << "  |\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";

	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t      2 |  " << board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] << "  |  " << board[1][3] << "  |  " << board[1][4] << "  |\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t      3 |  " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] << "  |  " << board[2][3] << "  |  " << board[2][4] << "  |\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t      4 |  " << board[3][0] << "  |  " << board[3][1] << "  |  " << board[3][2] << "  |  " << board[3][3] << "  |  " << board[3][4] << "  |\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t      5 |  " << board[4][0] << "  |  " << board[4][1] << "  |  " << board[4][2] << "  |  " << board[4][3] << "  |  " << board[4][4] << "  |\n";
	cout << "\t\t\t\t|     |     |     |     |     |\n";
	cout << "\t\t\t\t+-----+-----+-----+-----+-----+\n";
}
//Input Function
void input(int turn)
{
	int row, col;
	cout << "Please enter Row Number:\t";
	cin >> row;
	cout << "Please enter Column Number:\t";
	cin >> col;
	row--;
	col--;
	if (row < 5 && col < 5)
	{
		if (board[row][col] == '\0')
		{
			if (turn == 1)
			{
				board[row][col] = Player1Move;
				b[row][col] = p1;
			}
			else
			{
				board[row][col] = Player2Move;
				b[row][col] = p2;
			}
		}
		else
		{
			cout << "This Cell is already fill\n";
			input(turn);
		}
	}
	else
	{
		cout << "Please enter an Valid cell Number\n";
		input(turn);
	}
}

//Player1 Turn Function
void inline player1()
{
	system("CLS");
	printBoard();
	cout << "Player1 [X] Your Turn\n";
	input(1);
}
//Player2 Function
void inline player2()
{
	system("CLS");
	printBoard();
	cout << "Player2 [O] Your Turn\n";
	input(2);
}
//Player VS Player Game Funcation
void inline p2p()
{
	for (int i = 0; i < 13; i++)
	{
		player1();
		if (i > 3)
		{
			if (check())
			{
				system("CLS");
				printBoard();
				cout << "\n\t\t\t\tCongrats!! Player1 Won the Game!!\n";
				cout << "\n\t\t\t\t";
				system("pause");
				menu();
			}
		}
		if (i == 12)
		{
			system("CLS");
			printBoard();
			cout << "\n\t\t\t\tOopss!! Game Drawn!!\n";
			system("pause");
			menu();
		}
		player2();
		if (i > 3)
		{
			if (check())
			{
				system("CLS");
				printBoard();
				cout << "\n\t\t\t\tCongrats!! Player2 Won the Game!!\n";
				cout << "\n\t\t\t\t";
				system("pause");
				menu();
			}
		}

	}
}
//Check the Status Of Game
bool check()
{
	for (int i = 0; i < 5; i++)
	{
		if ((board[i][0] != '\0') && (board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][2] == board[i][3]) && (board[i][3] == board[i][4]) || (board[0][i] != '\0') && (board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[2][i] == board[3][i]) && (board[3][i] == board[4][i]))
		{
			return true;
		}
	}
	if ((board[0][0] != '\0') && (board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) && (board[3][3] == board[4][4]))
	{
		return true;
	}
	else if ((board[0][4] != '\0') && (board[0][4] == board[1][3]) && (board[1][3] == board[2][2]) && (board[2][2] == board[3][1]) && (board[3][1] == board[4][0]))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//defend function for Computer
bool defend(int comp, int player, int brd)
{
	if (brd < 5)
	{
		return false;
	}
	else if (b[0][0] + b[1][1] + b[2][2] + b[3][3] + b[4][4] == 4 * comp && b[0][0] != player && b[1][1] != player && b[2][2] != player && b[3][3] != player && b[4][4] != player)
	{
		for (int i = 0; i < 5; i++)
		{
			if (b[i][i] == 0)
			{
				b[i][i] = p2;
				board[i][i] = Player2Move;
				return true;
			}
		}
	}
	else if (b[0][4] + b[1][3] + b[2][2] + b[3][1] + b[4][0] == 4 * comp && b[0][4] != player && b[1][3] != player && b[2][2] != player && b[3][1] != player && b[4][0] != player)
	{
		for (int i = 0; i < 5; i++)
		{
			if (b[i][4 - i] == 0)
			{
				b[i][4 - i] = p2;
				board[i][4 - i] = Player2Move;
				return true;
			}
		}
	}
	else
	{
		for (int i = 0; i < 5; i++)
		{
			if (b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == 4 * comp && b[i][0] != player && b[i][1] != player && b[i][2] != player && b[i][3] != player && b[i][4] != player)
			{
				for (int j = 0; j < 5; j++)
				{
					if (b[i][j] == 0)
					{
						b[i][j] = p2;
						board[i][j] = Player2Move;
						return true;
					}
				}
			}
			else if (b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == 4 * comp && b[0][i] != player && b[1][i] != player && b[2][i] != player && b[3][i] != player && b[4][i] != player)
			{
				for (int j = 0; j < 5; j++)
				{
					if (b[j][i] == 0)
					{
						b[j][i] = p2;
						board[j][i] = Player2Move;
						return true;
					}
				}
			}
		}
	}
	return false;
}
//attack -- take a move as to win in future
bool attack()
{
	if (b[0][0] + b[0][4] + b[4][0] + b[4][4] == p1 || b[0][0] + b[0][4] + b[4][0] + b[4][4] == 4 * p1)
	{
		for (int i = 0; i < 5; i++)
		{
			if (b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)
			{
				if (i == 3)
				{
					for (int j = 0; j < 5; j++)
					{
						if (b[i][j] == 0)
						{
							b[i][j] = p2;
							board[i][j] = Player2Move;
							return true;
						}
					}
				}
				else
				{
					for (int j = 4; j >= 0; j--)
					{
						if (b[i][j] == 0)
						{
							b[i][j] = p2;
							board[i][j] = Player2Move;
							return true;
						}
					}
				}
			}
			if (b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p2 && b[0][i] == p2 || b[1][i] == p2 || b[2][i] == p2 || b[3][i] == p2 || b[4][i] == p2)
			{
				if (i == 3)
				{
					for (int j = 0; j < 5; j++)
					{
						if (b[j][i] == 0)
						{
							b[j][i] = p2;
							board[j][i] = Player2Move;
							return true;
						}
					}
				}
				else
				{
					for (int j = 4; j >= 0; j--)
					{
						if (b[j][i] == 0)
						{
							b[j][i] = p2;
							board[j][i] = Player2Move;
							return true;
						}
					}
				}
			}
		}
		if (b[0][0] + b[1][1] + b[2][2] + b[3][3] + b[4][4] == p2 && b[0][0] == p2 || b[1][1] == p2 || b[2][2] == p2 || b[3][3] == p2 || b[4][4] == p2)
		{
			for (int i = 4; i >= 0; i--)
			{
				if ((b[i][i] == 0) && ((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && (b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)) || ((b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p2) && (b[0][i] == p2 || b[1][i] == p2 || b[2][i] == p2 || b[3][i] == p2 || b[4][i] == p2))))
				{
					b[i][i] = p2;
					board[i][i] = Player2Move;
					return true;
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][i] == 0)
				{
					if (((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p1 && (b[i][0] == p1 || b[i][1] == p1 || b[i][2] == p1 || b[i][3] == p1 || b[i][4] == p1)) || ((b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p1) && (b[0][i] == p1 || b[1][i] == p1 || b[2][i] == p1 || b[3][i] == p1 || b[4][i] == p1))))
					{
						b[i][i] = p2;
						board[i][i] = Player2Move;
						return true;
					}
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][i] == 0)
				{
					b[i][i] = p2;
					board[i][i] = Player2Move;
					return true;
				}
			}
		}
		else if (b[0][4] + b[1][3] + b[2][2] + b[3][1] + b[4][0] == p2 && b[0][4] == p2 || b[1][3] == p2 || b[2][2] == p2 || b[3][1] == p2 || b[4][0] == p2)
		{
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0 && ((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && (b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)) || ((b[0][4 - i] + b[1][4 - i] + b[2][4 - i] + b[3][4 - i] + b[4][4 - i] == p2) && (b[0][4 - i] == p2 || b[1][4 - i] == p2 || b[2][4 - i] == p2 || b[3][4 - i] == p2 || b[4][4 - i] == p2))))
				{
					b[i][4 - i] = p2;
					board[i][4 - i] = Player2Move;
					return true;
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0)
				{
					if (((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p1 && (b[i][0] == p1 || b[i][1] == p1 || b[i][2] == p1 || b[i][3] == p1 || b[i][4] == p1)) || ((b[0][4 - i] + b[1][4 - i] + b[2][4 - i] + b[3][4 - i] + b[4][4 - i] == p1) && (b[0][4 - i] == p1 || b[1][4 - i] == p1 || b[2][4 - i] == p1 || b[3][4 - i] == p1 || b[4][4 - i] == p1))))
					{
						b[i][4 - i] = p2;
						board[i][4 - i] = Player2Move;
						return true;
					}
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0)
				{
					b[i][4 - i] = p2;
					board[i][4 - i] = Player2Move;
					return true;
				}
			}
		}
	}
	else
	{
		if(b[0][0]+b[1][1]+b[2][2]+b[3][3]+b[4][4]==p2 && (b[0][0] == p2 || b[1][1] == p2 || b[2][2] == p2 || b[3][3] == p2 || b[4][4] == p2))
		{
			for (int i = 4; i >= 0; i--)
			{
				if ((b[i][i] == 0) && ((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && (b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)) || ((b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p2) && (b[0][i] == p2 || b[1][i] == p2 || b[2][i] == p2 || b[3][i] == p2 || b[4][i] == p2))))
				{
					b[i][i] = p2;
					board[i][i] = Player2Move;
					return true;
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][i] == 0)
				{
					if (((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p1 && (b[i][0] == p1 || b[i][1] == p1 || b[i][2] == p1 || b[i][3] == p1 || b[i][4] == p1)) || ((b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p1) && (b[0][i] == p1 || b[1][i] == p1 || b[2][i] == p1 || b[3][i] == p1 || b[4][i] == p1))))
					{
						b[i][i] = p2;
						board[i][i] = Player2Move;
						return true;
					}
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][i] == 0)
				{
					b[i][i] = p2;
					board[i][i] = Player2Move;
					return true;
				}
			}
		}
		else if (b[0][4] + b[1][3] + b[2][2] + b[3][1] + b[4][0] == p2 && b[0][4] == p2 || b[1][3] == p2 || b[2][2] == p2 || b[3][1] == p2 || b[4][0] == p2)
		{
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0 && ((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && (b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)) || ((b[0][4 - i] + b[1][4 - i] + b[2][4 - i] + b[3][4 - i] + b[4][4 - i] == p2) && (b[0][4 - i] == p2 || b[1][4 - i] == p2 || b[2][4 - i] == p2 || b[3][4 - i] == p2 || b[4][4 - i] == p2))))
				{
					b[i][4 - i] = p2;
					board[i][4 - i] = Player2Move;
					return true;
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0)
				{
					if (((b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p1 && (b[i][0] == p1 || b[i][1] == p1 || b[i][2] == p1 || b[i][3] == p1 || b[i][4] == p1)) || ((b[0][4 - i] + b[1][4 - i] + b[2][4 - i] + b[3][4 - i] + b[4][4 - i] == p1) && (b[0][4 - i] == p1 || b[1][4 - i] == p1 || b[2][4 - i] == p1 || b[3][4 - i] == p1 || b[4][4 - i] == p1))))
					{
						b[i][4 - i] = p2;
						board[i][4 - i] = Player2Move;
						return true;
					}
				}
			}
			for (int i = 4; i >= 0; i--)
			{
				if (b[i][4 - i] == 0)
				{
					b[i][4 - i] = p2;
					board[i][4 - i] = Player2Move;
					return true;
				}
			}
		}
		else
		{
			for (int i = 0; i < 5; i++)
			{
				if (b[i][0] + b[i][1] + b[i][2] + b[i][3] + b[i][4] == p2 && b[i][0] == p2 || b[i][1] == p2 || b[i][2] == p2 || b[i][3] == p2 || b[i][4] == p2)
				{
					if (i == 3)
					{
						for (int j = 0; j < 5; j++)
						{
							if (b[i][j] == 0)
							{
								b[i][j] = p2;
								board[i][j] = Player2Move;
								return true;
							}
						}
					}
					else
					{
						for (int j = 4; j >= 0; j--)
						{
							if (b[i][j] == 0)
							{
								b[i][j] = p2;
								board[i][j] = Player2Move;
								return true;
							}
						}
					}
				}
				else if (b[0][i] + b[1][i] + b[2][i] + b[3][i] + b[4][i] == p2 && b[0][i] == p2 || b[1][i] == p2 || b[2][i] == p2 || b[3][i] == p2 || b[4][i] == p2)
				{
					if (i == 3)
					{
						for (int j = 0; j < 5; j++)
						{
							if (b[j][i] == 0)
							{
								b[j][i] = p2;
								board[j][i] = Player2Move;
								return true;
							}
						}
					}
					else
					{
						for (int j = 4; j >= 0; j--)
						{
							if (b[j][i] == 0)
							{
								b[j][i] = p2;
								board[j][i] = Player2Move;
								return true;
							}
						}
					}
				}
			}
		}
	}
	return false;
}
//Computer Moves
void computerMoves(int x)
{
	if (!defend(p2, p1, x))
	{
		if (!defend(p1, p2, x))
		{
			if ((b[0][0] + b[0][4] + b[4][0] + b[4][4] == p1 + p2 || b[0][0] + b[0][4] + b[4][0] + b[4][4] == p1 + 2 * p2) && b[2][2] == 0)
			{
				for (int i = 0; i < 5; i += 2)
				{
					for (int j = 0; j < 5; j += 2)
					{
						if (b[i][j] == 0)
						{
							b[i][j] = p2;
							board[i][j] = Player2Move;
							goto end;
						}
					}
				}
			}
			else if (x == 2 && b[2][2] == 0)
			{
				b[2][2] = p2;
				board[2][2] = Player2Move;
				goto end;
			}
			if (!attack())
			{
				if (x == 0)
				{
					time_t seconds;
					time(&seconds);
					srand((unsigned int)seconds);
					int i, j;
					i = (rand() % 2) * 4;
					j = (rand() % 2) * 4;
					b[i][j] = p2;
					board[i][j] = Player2Move;
				}
				else if (b[2][2] == 0)
				{
					b[2][2] = p2;
					board[2][2] = Player2Move;
				}
				else
				{
					for (int i = 0; i < 5; i++)
					{
						for (int j = 0; j < 5; j++)
						{
							if (b[i][j] == 0)
							{
								b[i][j] = p2;
								board[i][j] = Player2Move;
								goto end;
							}
						}
					}
				}
			}
		}
	}
end:
	cout << endl;
}
//Player VS Computer Funcation
void p2c()
{
	system("CLS");
	cout << "\n\n\n\t\t\t\tYou wanna go 1st or 2nd(1 or 2)\n";
	int choice;
	cin >> choice;
	switch (choice)
	{
	case 1:
		cout << "\t\t\t\tYour symbol is X and Computer's symbol is O\n";
		cout << "\t\t\t\t";
		system("pause");
		for (int i = 0; i < 25;)
		{
			system("CLS");
			printBoard();
			input(p1);
			system("CLS");
			printBoard();
			i++;
			if (i > 8)
			{
				if (check())
				{
					system("CLS");
					printBoard();
					cout << "\n\t\t\t\tCongrats!! You Won the Game!!\n";
					cout << "\n\t\t\t\t";
					system("pause");
					menu();
				}
			}
			if (i >= 24)
			{
				system("CLS");
				printBoard();
				cout << "\n\t\t\t\tOopss!! Game Drawn!!\n";
				system("pause");
				menu();
			}
			computerMoves(i);
			system("CLS");
			printBoard();
			i++;
			if (i > 9)
			{
				if (check())
				{
					system("CLS");
					printBoard();
					cout << "\n\t\t\t\Oopss!! Computer Won the Game!!\n";
					cout << "\n\t\t\t\t";
					system("pause");
					menu();
				}
			}
		}
		break;
	case 2:
	{
		p2 = 1;
		p1 = choice;
		Player1Move = 'O';
		Player2Move = 'X';

		cout << "\t\t\t\tYour symbol is O and Computer's symbol is X\n";
		cout << "\t\t\t\t";
		system("pause");
		for (int i = 0; i < 25;)
		{
			system("CLS");
			printBoard();
			computerMoves(i);
			system("CLS");
			printBoard();
			i++;
			if (i > 8)
			{
				if (check())
				{
					system("CLS");
					printBoard();
					cout << "\n\t\t\t\Oopss!! Computer Won the Game!!\n";
					cout << "\n\t\t\t\t";
					system("pause");
					menu();
				}
			}
			if (i >= 24)
			{
				system("CLS");
				printBoard();
				cout << "\n\t\t\t\tOopss!! Game Drawn!!\n";
				system("pause");
				menu();
			}
			input(p2);
			system("CLS");
			printBoard();
			i++;
			if (i > 9)
			{
				if (check())
				{
					system("CLS");
					printBoard();
					cout << "\n\t\t\t\tCongrats!! You Won the Game!!\n";
					cout << "\n\t\t\t\t";
					system("pause");
					menu();
				}
			}
		}
		break;

	}
	default:
		cout << "Invalid Input..!!\n ";
		system("pause");
		p2c();
		break;
	}
}



Check more c++ examples here C++ Simple Examples

1 comment: