forked from Rishabh062/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIC TAC TOE PROJECT.cpp
164 lines (134 loc) · 3.65 KB
/
TIC TAC TOE PROJECT.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
#include <bits/stdc++.h>
using namespace std;
// A fun() which returns a 2D char array.
char **createBoard(int boardSize = 3)
{
cout << "Engagging board creation" << endl;
// Returns a dynamically allocated array.
char **board = new char *[boardSize];
for (int i = 0; i < boardSize; i++)
{
board[i] = new char[boardSize];
}
return board;
}
void labelBoard(char **board, int boardSize)
{
char counter = '0';
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
board[i][j] = counter++;
}
}
}
void displayBoard(char **board, int boardSize)
{
system("cls");
printf("\n\n");
int unitSize = boardSize;
//Logic goes here.
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
if (j < boardSize - 1)
cout << " " << board[i][j] << " |";
else
cout << " " << board[i][j] << " ";
}
cout << endl;
if (i < boardSize - 1)
for (int k = 0; k <= unitSize * boardSize; k++)
cout << "-";
cout << endl;
}
cout << endl;
}
void choiceToCords(int *x, int *y, int boardSize)
{
int ch;
cin >> ch;
*x = ch / boardSize;
*y = ch % boardSize;
}
int checkWinCondition(char **board, int boardSize, int playerTurn)
{
for (int i = 0; i < boardSize; i++)
{
char rowVal = board[i][0];
int rowMatch = 1;
char colVal = board[0][i];
int colMatch = 1;
for (int j = 0; j < boardSize; j++)
{
if (board[i][j] == rowVal)
rowMatch++;
if (board[j][i] == rowVal)
colMatch++;
}
if (rowMatch == boardSize + 1 || colMatch == boardSize + 1)
return playerTurn;
}
char leftD = board[0][0];
int lD_match = 1;
char rightD = board[boardSize - 1][0];
int rD_match = 1;
for (int i = 0; i < boardSize; i++)
{
if (board[i][i] == leftD)
lD_match++;
if (board[boardSize - 1][i] == rightD)
rD_match++;
}
if (leftD == boardSize || rightD == boardSize)
return playerTurn;
return -1;
}
void runGame(char **board, int boardSize, char **playerIds)
{
char weapon[] = {'X', 'O'};
int playerTurn = 0, rounds = 0;
displayBoard(board, boardSize);
while (rounds < 9)
{
playerTurn = (playerTurn) ? 0 : 1; // Ternary Operators.
int X, Y;
cout << playerIds[playerTurn] << ", your turn > \n";
choiceToCords(&X, &Y, boardSize);
board[X][Y] = weapon[playerTurn];
displayBoard(board, boardSize);
int winner = checkWinCondition(board, boardSize, playerTurn);
if (winner != -1)
{
cout << playerIds[winner] << " has won, bow down yall" << endl;
exit(0);
}
rounds++;
}
cout << " Issa draw!" << endl;
}
char **fetchPlayers()
{
char **playerNames = new char *[2];
for (int i = 0; i < 2; i++)
{
playerNames[i] = new char[30];
cout << "Player " << (i + 1) << " > ";
cin >> playerNames[i];
}
return playerNames;
}
int main(void)
{
int boardSize;
cout << "Boardsize? ";
cin >> boardSize;
char **board = createBoard(boardSize);
labelBoard(board, boardSize);
displayBoard(board, boardSize);
char **playerIds = fetchPlayers();
runGame(board, boardSize, playerIds);
return 0;
}