summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgrisBRC <[email protected]>2024-07-14 13:43:36 +0530
committerIgrisBRC <[email protected]>2024-07-14 13:43:36 +0530
commit4194b03e980ac68e0fd07e183189fbe93dfefdce (patch)
tree79bbce279fa277aa34cc53e4818eca59b54f8963
parent0f1250b0f879eb764559d80bbc8a54d53c221f68 (diff)
finnnallyyyy... added make move to the game, its a bit shitty, i'll make it prettier later
-rw-r--r--public/board.js36
-rw-r--r--public/index.html7
-rw-r--r--public/make_move.js5
-rw-r--r--public/move.js2
-rw-r--r--public/style.css (renamed from public/index.css)8
-rw-r--r--public/sync_board.js16
6 files changed, 47 insertions, 27 deletions
diff --git a/public/board.js b/public/board.js
index 2484136..ca46335 100644
--- a/public/board.js
+++ b/public/board.js
@@ -7,29 +7,53 @@ sync_board()
let previously_highlighted_squares = ['dummy']
for (let i = 0; i < squares.length; i++) {
- squares[i].addEventListener('click', () => {
+ squares[i].onclick = () => {
let square = []
let id = squares[i].id
square.push(8 - id.charAt(1))
square.push(id.charAt(0).charCodeAt(0) - 97)
-
let moves = move(board, square[0], square[1], to_move)
-
for (let i = 0; i < previously_highlighted_squares.length; i++) {
let prev = document.getElementById(previously_highlighted_squares[i])
prev.classList.remove('highlight')
}
-
for (let i = 0; i < moves.length; i++) {
+ let hlsquare = document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`)
+
+ hlsquare.classList.add('highlight')
+
+ hlsquare.onclick = handle_move(id, hlsquare.id)
- document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`).classList.add('highlight')
previously_highlighted_squares.push(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`)
}
- })
+ }
}
+//let handle_move = () => {
+// console.log('move')
+//}
+
+function handle_move(from_id, id) {
+ return function() {
+ let table = { 1: 'K', 2: 'Q', 3: 'R', 4: 'B', 5: 'N', 6: 'p' }
+ make_move(board, 8 - from_id.charAt(1), from_id.charCodeAt(0) - 97, 8 - id.charAt(1), id.charCodeAt(0) - 97)
+
+ let from_element = document.getElementById(from_id)
+ while (from_element.firstChild) {
+ from_element.removeChild(from_element.firstChild)
+ }
+
+ let new_element = document.getElementById(id)
+ let piece = document.createElement('p')
+ piece.classList.add(board[8 - id.charAt(1)][id.charCodeAt(0) - 97] > 0 ? 'w' : 'b', 'piece')
+ new_element.append(piece)
+ piece.append(table[Math.abs(board[8 - id.charAt(1)][id.charCodeAt(0) - 97])])
+
+ to_move = !to_move
+ }
+}
diff --git a/public/index.html b/public/index.html
index dcf7f5f..312eb76 100644
--- a/public/index.html
+++ b/public/index.html
@@ -4,14 +4,11 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess</title>
- <link rel="stylesheet" href="index.css">
+ <link rel="stylesheet" href="style.css">
</head>
<body>
<div id="dummy"></div>
-
-
-
<div class="chess-board">
<div>
@@ -128,7 +125,7 @@
</div>
<script rel="preconnect" src="move.js" crossorigin></script>
<script rel="preconnect" src="sync_board.js" crossorigin></script>
- <script rel="preconnect" src="board.js" crossorigin></script>
<script rel="preconnect" src="make_move.js" crossorigin></script>
+ <script rel="preconnect" src="board.js" crossorigin></script>
</body>
</html>
diff --git a/public/make_move.js b/public/make_move.js
index ec0956d..562505f 100644
--- a/public/make_move.js
+++ b/public/make_move.js
@@ -1,5 +1,6 @@
function make_move(board, y ,x, new_y, new_x) {
- let temp = board[y][x]
+ board[new_y][new_x] = board[y][x]
board[y][x] = 0
- board[new_y][new_x] = temp
}
+
+make_move(board,6,4,4,4)
diff --git a/public/move.js b/public/move.js
index 7626e5e..0c441c2 100644
--- a/public/move.js
+++ b/public/move.js
@@ -9,7 +9,7 @@ let board = [
[3, 5, 4, 2, 1, 4, 5, 3],
]
-let to_move = true
+let to_move = true //white to move
//console.log(move(board, 4, 3))
//move(board, 3, 3)
diff --git a/public/index.css b/public/style.css
index 1971278..7c0c693 100644
--- a/public/index.css
+++ b/public/style.css
@@ -57,14 +57,10 @@ body{
background-color: yellow;
}
-/*.wp {*/
-/* color: white;*/
-/*}*/
-
.w {
- color: white
+ color: white;
}
.b {
- color: black
+ color: black;
}
diff --git a/public/sync_board.js b/public/sync_board.js
index 629cae6..c56cb77 100644
--- a/public/sync_board.js
+++ b/public/sync_board.js
@@ -1,16 +1,18 @@
-function sync_board(){
- let table = {1:'K', 2:'Q', 3:'R', 4:'B', 5:'N', 6:'p'}
+function sync_board() {
+
+ let table = { 1: 'K', 2: 'Q', 3: 'R', 4: 'B', 5: 'N', 6: 'p' }
- for(let i = 0; i < 8; i++){
- for(let j = 0; j < 8; j++){
- if(board[i][j] == 0) continue
+ for (let i = 0; i < 8; i++) {
+ for (let j = 0; j < 8; j++) {
+ if (board[i][j] == 0) continue
let square = document.getElementById(`${alphabet.charAt(j)}${8 - i}`)
let piece = document.createElement('p')
- piece.classList.add(board[i][j] > 0 ? 'w' : 'b')
+ piece.classList.add(board[i][j] > 0 ? 'w' : 'b', 'piece')
square.append(piece)
piece.append(table[Math.abs(board[i][j])])
}
}
-} \ No newline at end of file
+ console.log(board)
+}