-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_code.js
219 lines (193 loc) · 4.38 KB
/
main_code.js
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
let w;
let h;
let bigw;
let bigh;
let board = Array.from({ length: 9 }, () => Array(9).fill(' ')); // 9x9 2D array
let bigboard = Array.from({ length: 3 }, () => Array(3).fill(' ')); // 3x3 2D array
let human='O';
let bot='X';
let current_player=human;
function setup() {
createCanvas(600, 600);
w=width/9;
h=height/9;
bigw=width/3;
bigh=height/3;
best_move();
}
function mousePressed() {
// if (current_player == human )
// Human turn or bot turn
let i = floor(mouseX / w);
let j = floor(mouseY / h);
//If the turn is valid
if (board[i][j] == ' ') {
board[i][j] = current_player;
small_checkwinner(floor(i/3),floor(j/3));
if(current_player==human)
{
current_player=bot;
}
else
{
current_player=human;
}
// best_move();
}
//
}
function best_move()
{
//bot turn
let bestscore=-Infinity;
}
function equals(a,b,c)
{
return a==b && b==c && a !=' ';
}
function big_checkwinner() {
let winner = null;
// horizontal
for (let i = 0; i < 3; i++) {
if (equals(bigboard[i][0], bigboard[i][1], bigboard[i][2])) {
winner = bigboard[i][0];
}
}
// Vertical
for (let i = 0; i < 3; i++) {
if (equals(bigboard[0][i], bigboard[1][i], bigboard[2][i])) {
winner = bigboard[0][i];
}
}
// Diagonal
if (equals(bigboard[0][0], bigboard[1][1], bigboard[2][2])) {
winner = bigboard[0][0];
}
if (equals(bigboard[2][0], bigboard[1][1], bigboard[0][2])) {
winner = bigboard[2][0];
}
let openSpots = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (bigboard[i][j] == ' ') {
openSpots++;
}
}
}
if (winner == null && openSpots == 0) {
return 'tie';
} else if(winner==bot || winner==human) {
return winner;
}
else{
return null;
}
}
function small_checkwinner(a,b) {
let winner = null;
// horizontal
for (let i = 0; i < 3; i++) {
if (equals(board[i+3*a][0+3*b], board[i+3*a][1+3*b], board[i+3*a][2+3*b])) {
winner = board[i+3*a][0+3*b];
}
}
// Vertical
for (let i = 0; i < 3; i++) {
if (equals(board[0+3*a][i+3*b], board[1+3*a][i+3*b], board[2+3*a][i+3*b])) {
winner = board[0+3*a][i+3*b];
}
}
// Diagonal
if (equals(board[0+3*a][0+3*b], board[1+3*a][1+3*b], board[2+3*a][2+3*b])) {
winner = board[0+3*a][0+3*b];
}
if (equals(board[2+3*a][0+3*b], board[1+3*a][1+3*b], board[0+3*a][2+3*b])) {
winner = board[2+3*a][0+3*b];
}
let open_spots = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i+3*a][j+3*b] == ' ') {
open_spots++;
}
}
}
if (winner == null && open_spots==0 ) {
bigboard[a][b]='=';
} else if(winner==human) {
bigboard[a][b]=human;
}
else if(winner==bot){
bigboard[a][b]=bot;
}
else
{
bigboard[a][b]=' ';
}
}
function draw() {
background(240);
strokeWeight(1);
stroke(0);
for(let i=1;i<9;i++)
{
line(i*w,0,i*w,height);
line(0,i*h,width,i*h);
}
strokeWeight(5);
stroke(255, 0, 0);
for(let i=1;i<3;i++)
{
line(i*bigw,0,i*bigw,height);
line(0,i*bigh,width,i*bigh);
}
for (let j = 0; j < 9; j++) {
for (let i = 0; i < 9; i++) {
let x = w * i + w / 2;
let y = h * j + h / 2;
let spot = board[i][j];
textSize(10);
strokeWeight(1);
let r = w / 4;
if (spot == human) {
noFill();
ellipse(x, y, r * 2);
} else if (spot == bot) {
line(x - r, y - r, x + r, y + r);
line(x + r, y - r, x - r, y + r);
}
}
}
for (let j = 0; j < 3; j++) {
for (let i = 0; i < 3; i++) {
let x = bigw * i + bigw / 2;
let y = bigh * j + bigh / 2;
let spot = bigboard[i][j];
textSize(32);
strokeWeight(5);
let r = bigw / 4;
if (spot == human) {
noFill();
ellipse(x, y, r * 2);
} else if (spot == bot) {
line(x - r, y - r, x + r, y + r);
line(x + r, y - r, x - r, y + r);
}
else if(spot=='=')
{
square(i*bigw,j*bigh,bigw);
}
}
}
let result = big_checkwinner();
if (result != null) {
noLoop();
let resultP = createP('');
resultP.style('font-size', '32pt');
if (result == 'tie') {
resultP.html('Tie!');
} else {
resultP.html(`${result} wins!`);
}
}
}