-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
264 lines (229 loc) · 8.73 KB
/
tictactoe.cpp
File metadata and controls
264 lines (229 loc) · 8.73 KB
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
#include <raylib.h>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#define RAYGUI_IMPLEMENTATION
using namespace std;
bool isInbox(int w, int h){
if(GetMouseX()>0 && GetMouseY()> 0){
if(GetMouseX()< w && GetMouseY() < h){
return true;
}
return false;
}
return false;
}
char AvailableWinner(char** game) {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if (game[i][0] == game[i][1] && game[i][1] == game[i][2] && game[i][0] != '\0')
return game[i][0];
if (game[0][i] == game[1][i] && game[1][i] == game[2][i] && game[0][i] != '\0')
return game[0][i];
}
// Check diagonals
if (game[0][0] == game[1][1] && game[1][1] == game[2][2] && game[0][0] != '\0')
return game[0][0];
if (game[0][2] == game[1][1] && game[1][1] == game[2][0] && game[0][2] != '\0')
return game[0][2];
bool isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (game[i][j] == '\0') {
isFull = false;
break;
}
}
}
return isFull ? 'D' : 'C';
}
int main(){
// window constants
const int w = 800;
const int h = 800;
const char* title = "Tic-Tac-Toe";
InitWindow(w,h,title);
SetTargetFPS(60);
//decision variables
bool mustDraw = false;
//vertical lines starting x
const int vertLine_1 = w * (0.35);
const int vertLine_2 = vertLine_1 + w/4;
// line constants int arr[2] = {start, end}
const int vertLineHeight[2] = {h/6, h-vertLine_1/2} ;
//horizontal lines starting x
const int horizLine_1_x_start = vertLineHeight[0];
const int horizLine_1_y_start = vertLine_1;
const int horizLine_1_x_end = vertLineHeight[1];
const int horizLine_1_y_end = vertLine_1;
const int horizLine_2_x_start = vertLineHeight[0];
const int horizLine_2_y_start = vertLine_2;
const int horizLine_2_x_end = vertLineHeight[1];
const int horizLine_2_y_end = vertLine_2;
//Color of lines
const Color lineColor = WHITE;
//set vline vectors
const Vector2 vLine1_startPos ={(float)vertLine_1,(float)vertLineHeight[0]};
const Vector2 vLine1_endPos ={(float)vertLine_1,(float)vertLineHeight[1]};
const Vector2 vLine2_startPos ={(float)vertLine_2,(float)vertLineHeight[0]};
const Vector2 vLine2_endPos ={(float)vertLine_2,(float)vertLineHeight[1]};
//set hline vectors Width
const Vector2 hLine1_startPos ={(float)horizLine_1_x_start,(float)horizLine_1_y_start,};
const Vector2 hLine1_endPos ={(float)horizLine_1_x_end,(float)horizLine_1_y_start};
const Vector2 hLine2_startPos ={(float)horizLine_2_x_start,(float)horizLine_2_y_start};
const Vector2 hLine2_endPos ={(float)horizLine_2_x_end,(float)horizLine_2_y_end};
//line width
const float line_width = 20.0f;
//play logic
int player = 0;
char isWinner ='C' ;
//player +position logic
//turn player position
vector<pair<char,Vector2>> playerMoves;
char** twoDimArr = new char*[3];
for(int i = 0; i < 3; i++){
twoDimArr[i] = new char[3];
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
twoDimArr[i][j] = '\0';
while (!WindowShouldClose()){
BeginDrawing();
ClearBackground(BLACK);
for (auto& x : playerMoves) {
char sym[2] = { x.first, '\0' };
DrawText(sym, (int)x.second.x, (int)x.second.y, 50, WHITE);
}
const char* mousePosText = TextFormat("Mouse: %i, %i", GetMouseX(), GetMouseY());
DrawText(mousePosText, 10, 10, 20, WHITE);
//Vertical lines {int startintX, int startintY}
DrawLineEx(vLine1_startPos,vLine1_endPos,line_width,lineColor);
DrawLineEx(vLine2_startPos,vLine2_endPos,line_width,lineColor);
//Horizontal lines
DrawLineEx(hLine1_startPos,hLine1_endPos,line_width,lineColor);
DrawLineEx(hLine2_startPos,hLine2_endPos,line_width,lineColor);
int mouseX = GetMouseX();
int mouseY = GetMouseY();
Vector2 currentMouse = GetMousePosition();
if(isWinner =='C'){
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && isInbox(w, h)) {
char playerSymbol = (player == 0) ? 'X' : 'O';
Vector2 location = {(float)mouseX, (float)mouseY };
//find top row
bool canplace = true;
const char* currWinner = TextFormat("");
if(mouseY <= horizLine_1_y_start){
if(mouseX <= vLine1_startPos.x){
if(twoDimArr[0][0] == '\0')
twoDimArr[0][0] = playerSymbol;
else{
canplace = false;
}
}
else if(mouseX >= vLine2_startPos.x){
if(twoDimArr[0][2] == '\0')
twoDimArr[0][2] = playerSymbol;
else{
canplace = false;
}
}
else{
if(twoDimArr[0][1] == '\0')
twoDimArr[0][1] = playerSymbol;
else{
canplace = false;
}
}
}
else if(mouseY > hLine1_startPos.y && mouseY <= hLine2_startPos.y){
//goes up going down
if(mouseX <= vLine1_startPos.x){
if(twoDimArr[1][0] == '\0')
twoDimArr[1][0] = playerSymbol;
else{
canplace = false;
}
}
else if(mouseX >= vLine2_startPos.x){
if(twoDimArr[1][2] == '\0')
twoDimArr[1][2] = playerSymbol;
else{
canplace = false;
}
}
else{
if(twoDimArr[1][1] == '\0')
twoDimArr[1][1] = playerSymbol;
else{
canplace = false;
}
}
}
else{
//goes up going down
if(mouseX <= vLine1_startPos.x){
if(twoDimArr[2][0] == '\0')
twoDimArr[2][0] = playerSymbol;
else{
canplace = false;
}
}
else if(mouseX >= vLine2_startPos.x){
if(twoDimArr[2][2] == '\0')
twoDimArr[2][2] = playerSymbol;
else{
canplace = false;
}
}
else{
if(twoDimArr[2][1] == '\0')
twoDimArr[2][1] = playerSymbol;
else{
canplace = false;
}
}
}
for(int i = 0,count = 1; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << count<< ":"<< twoDimArr[i][j] << "\t";
if(count %3 == 0){
cout << endl;
}
count++;
}
}
std::cout<< endl;
if(canplace){
playerMoves.push_back({ playerSymbol, location });
player = (player == 0) ? 1 : 0;
isWinner = AvailableWinner(twoDimArr);
}
}
}
if (isWinner != 'C') {
if (isWinner == 'D') {
DrawText("DRAW!", w/2 - 250, h/2, 60, RED);
} else {
DrawText(TextFormat("Winner is: %c", isWinner), w/2 - 200, h/2, 60, RED);
}
DrawText("Press ESC to exit", w/2 - 100, h/2 + 70, 20, GRAY);
DrawText ("Press R to reset", w/2 - 100, h/2 + 50, 20, GRAY);
}
if (IsKeyPressed(KEY_R)) { //reset board
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) twoDimArr[i][j] = '\0';
}
playerMoves.clear();
isWinner = 'C';
player = 0;
}
EndDrawing();
}
CloseWindow();
for(int i = 0; i < 3; i++){
delete[] twoDimArr[i];
}
delete[] twoDimArr;
return 0;
}