-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
201 lines (201 loc) · 6.41 KB
/
main.js
File metadata and controls
201 lines (201 loc) · 6.41 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
// Game Constants
let flag = 1;
const ROWS = 6;
const COLS = 7;
const PLAYER1_COLOR = 'red';
const PLAYER2_COLOR = 'yellow';
const sleep = async (ms) => {
return new Promise(
(resolve, reject) => setTimeout(
() => resolve(), ms * 1000));
};
const _awaitUnlock = async (mutex) => {
// 잠금 상태가 아님 -> 즉시 resolve
if (!mutex._locked) {
return Promise.resolve()
}
return new Promise((resolve) => {
// 0.1초 후에 다시 확인
setTimeout(() => {
_awaitUnlock(mutex).then(() => resolve())
}, 100)
})
}
class Mutex {
constructor() {
this._locked = false
}
async lock() {
// 잠금 상태가 풀릴 때 까지 대기
await _awaitUnlock(this)
this._locked = true
}
// 잠금 해제는 별도의 제약을 주지 않음.
unlock() {
this._locked = false
}
}
let board = [];
let currentPlayer = 1;
const mutex = new Mutex()
// Create the game board
const boardElement = document.querySelector('.board');
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.setAttribute('data-row', row);
cell.setAttribute('data-col', col);
boardElement.appendChild(cell);
}
}
// Add event listener to cells
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
flag = 0;
async function cpu_turn() {
let col;
try {
const response = await axios({
method: "POST",
url: 'http://pwnable.co.kr:10004/get_next_action/',
data: {
"list": board
},
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
'Content-Type': 'application/json'
}
});
col = response.data; // Assuming the response contains the assigned column value
console.log(col); // Do something with the received response
} catch (error) {
console.error('Error:', error);
}
// Find the lowest available row in the selected column
let row = ROWS - 1;
while (row >= 0 && board[row][col]) {
row--;
}
// Fall all the way down if there is no stone underneath
if (row < 0) {
row = 0;
while (row < ROWS && board[row][col]) {
row++;
}
}
// Update the game board
board[row][col] = currentPlayer;
// Update the cell color
const color = currentPlayer === 1 ? PLAYER1_COLOR : PLAYER2_COLOR;
//clickedCell.style.backgroundColor = color;
// Reflect the changes on the screen
const rowCell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
rowCell.style.backgroundColor = color;
await sleep(0.2);
// Check for a win
if (checkWin(row, col)) {
const winner = currentPlayer === 1 ? 'Player 1' : 'Player 2';
alert(`${winner} wins!`);
resetGame();
return;
}
// Switch players
currentPlayer = 1;
mutex.unlock()
}
// Handle cell click event
async function handleCellClick(e) {
await mutex.lock()
currentPlayer = 1;
const clickedCell = e.target;
const col = parseInt(clickedCell.getAttribute('data-col'));
// Find the lowest available row in the selected column
let row = ROWS - 1;
while (row >= 0 && board[row][col]) {
row--;
}
// Fall all the way down if there is no stone underneath
if (row < 0) {
row = 0;
while (row < ROWS && board[row][col]) {
row++;
}
}
// Update the game board
board[row][col] = currentPlayer;
// Update the cell color
const color = currentPlayer === 1 ? PLAYER1_COLOR : PLAYER2_COLOR;
//clickedCell.style.backgroundColor = color;
// Reflect the changes on the screen
const rowCell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
rowCell.style.backgroundColor = color;
//setTimeout(() => {}, 1000);
await sleep(0.2);
// Check for a win
if (checkWin(row, col)) {
const winner = currentPlayer === 1 ? 'Player 1' : 'Player 2';
alert(`${winner} wins!`);
resetGame();
return;
}
// Switch players
currentPlayer = 2;
cpu_turn();
}
// Check for a win
function checkWin(row, col) {
const color = currentPlayer === 1 ? PLAYER1_COLOR : PLAYER2_COLOR;
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
if (board[i][j] == currentPlayer) {
// horizontal
if ((j + 3 < COLS) && (board[i][j + 1] == currentPlayer) && (board[i][j + 2] == currentPlayer) && (board[i][j + 3] == currentPlayer)) {
console.log(1)
console.log("i")
console.log(i)
console.log("j")
console.log(j)
return true;
}
// vertical
if ((i + 3 < ROWS) && (board[i + 1][j] == currentPlayer) && (board[i + 2][j] == currentPlayer) && (board[i + 3][j] == currentPlayer)) {
console.log(2)
return true;
}
// diagonal (down right)
if ((i + 3 < ROWS) && (j + 3 < COLS) && (board[i + 1][j + 1] == currentPlayer) && (board[i + 2][j + 2] == currentPlayer) && (board[i + 3][j + 3] == currentPlayer)) {
console.log(3)
return true;
}
// diagonal (up right)
if ((i - 3 >= 0) && (j + 3 < COLS) && (board[i - 1][j + 1] == currentPlayer) && (board[i - 2][j + 2] == currentPlayer) && (board[i - 3][j + 3] == currentPlayer)) {
console.log(4)
return true;
}
}
}
}
return false;
}
// Reset the game
function resetGame() {
board = [];
currentPlayer = 1;
cells.forEach(cell => {
cell.style.backgroundColor = 'lightblue';
});
location.reload()
}
// Initialize the game board
function initializeBoard() {
for (let row = 0; row < ROWS; row++) {
board[row] = [];
for (let col = 0; col < COLS; col++) {
board[row][col] = 0;
}
}
}
initializeBoard();