-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictac.cpp
153 lines (145 loc) · 3.55 KB
/
tictac.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
// Code Last Compiled on 10 June, 2018
// tictac.cpp
// Contains Tic-Tac-Toe Game
// Code Developed By : Himesh Nayak, XII-A, JKPS
const int size = 3;
char grid[size][size] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
void printGrid()
{
clrscr();
cout<<endl;
for (int i = 0; i < size; i++)
{
cout<<"|";
for (int j = 0; j < size; j++)
cout<<""<<grid[i][j]<<"|";
cout<<endl<<endl;
}
cout<<endl;
}
int placeGrid(char place, int player)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (place == grid[i][j] && player % 2 != 0)
{
grid[i][j] = 'O';
return 0;
}
else if (place == grid[i][j] && player % 2 == 0)
{
grid[i][j] = 'X';
return 0;
}
}
}
return 1;
}
int whoWonFunc(int & win1, int & win2, int & whoWon)
{
int endGame = 0;
if (win1 == size)
whoWon = 1;
else if (win2 == size)
whoWon = 2;
win1 = 0;
win2 = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (grid[i][j] == 'O' || grid[i][j] == 'X')
++endGame;
}
}
if (endGame == 9)
whoWon = endGame;
return whoWon;
}
int checkGrid()
{
int win1 = 0, win2 = 0, whoWon = 0, i, j;
for (i = 0; i < size; i++)
{
if (grid[i][i] == 'O')
++win1;
else if(grid[i][i] == 'X')
++win2;
}
if (whoWonFunc(win1, win2, whoWon) != 0)
return whoWon;
for (i = 0; i < size; i++)
{
if (grid[i][size - i - 1] == 'O')
++win1;
else if(grid[i][size - i - 1] == 'X')
++win2;
}
if (whoWonFunc(win1, win2, whoWon) != 0)
return whoWon;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (grid[i][j] == 'O')
++win1;
else if(grid[i][j] == 'X')
++win2;
}
if (whoWonFunc(win1, win2, whoWon) != 0)
return whoWon;
for (j = 0; j < size; j++)
{ if (grid[j][i] == 'O')
++win1;
else if(grid[j][i] == 'X')
++win2;
}
if (whoWonFunc(win1, win2, whoWon) != 0)
return whoWon;
}
return whoWon;
}
void tictac()
{
char player1[15] = "", player2[15] = "", winner[15] = "";
int win = 0;
char point;
clrscr();
cout<<"Tic Tac Toe Game"<<endl<<endl;
cout<<"Player 1 : ";
gets(player1);
cout<<"Player 2 : ";
gets(player2);
printGrid();
for (int move = 1; win == 0; move++)
{
if (move % 2 == 0)
cout<<player2<<", Enter where you want to play your move : "<<endl;
else cout<<player1<<", Enter where you want to play your move : "<<endl;
cin>>point;
if (placeGrid(point, move) == 1)
{ cout<<"Wrong Move!!!"<<endl;
--move;
continue;
}
printGrid();
win = checkGrid();
}
if (win == 1)
strcpy(winner, player1);
else if (win == 2)
strcpy(winner, player2);
else if (win == 9)
{
cout<<"Better Luck Next Time! Match Drawed!!!\n";
cout<<"\nThe Game is developed by Himesh Nayak and Aaryan Aroara XII-A"<<endl<<endl;
}
if (win != 9)
{
cout<<"Congratulation "<<winner<<". You win!!!\n";
cout<<"\nThe Game is developed by Himesh Nayak and Aaryan Arora XII-A"<<endl<<endl;
}
getch();
}