Repository navigation
Expand file tree
/
Copy pathchess.js
More file actions
178 lines (137 loc) · 4.2 KB
/
Copy pathchess.js
File metadata and controls
178 lines (137 loc) · 4.2 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
// create the board
// - 8 * 8 grid
// - alternate black and white cells
// - starts with white
// - Cell
// - color
// - id
// - piece?
// - Each square has an id
// - Arrangement of the board
//
// create a chess object
// - unique chess piece keys
// Pieces
// - currentPos (rows, cols)
// - possiblePos
// - name
// - color
// - uniqueMoveLogic??
// - move - func
// Conditions
// - kill
// - Check
// - checkmate
// - castling
var columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
var rows = [1, 2, 3, 4, 5, 6, 7, 8];
var INIT_WHITE = {
'A1': 'rook', 'B1': 'knight', 'C1': 'bishop', D1: 'king', 'E1': 'queen', 'F1': 'bishop', 'G1': 'knight', 'H1': 'rook',
'A2': 'pawn', 'B2': 'pawn', 'C2': 'pawn', 'D2': 'pawn', 'E2': 'pawn', 'F2': 'pawn', 'G2': 'pawn', 'H2': 'pawn',
}
var INIT_BLACK = {
'A8': 'rook', 'B8': 'knight', 'C8': 'bishop', D8: 'queen', 'E8': 'king', 'F8': 'bishop', 'G8': 'knight', 'H8': 'rook',
'A7': 'pawn', 'B7': 'pawn', 'C7': 'pawn', 'D7': 'pawn', 'E7': 'pawn', 'F7': 'pawn', 'G7': 'pawn', 'H7': 'pawn',
}
var currentMover = 'white';
var selectedPiece;
var cells = {}; // Store of truth
function assignPiece(row, col) {
var id = col+row;
if(INIT_WHITE[id]) {
return new Piece(INIT_WHITE[id], 'white', row, col)
} else if(INIT_BLACK[id]) {
return new Piece(INIT_BLACK[id], 'black', row, col)
} else {
return null;
}
}
rows.forEach((row, i) => {
columns.forEach((col, j) => {
var cell = {
color: (i+j) % 2 == 0 ? 'white' : 'black',
row: row,
col: col,
piece: assignPiece(row, col)
}
cells[col+row] = cell;
})
});
function findPossiblePawnPos(obj) {
}
// 2 col, 1 row
// 2 row, 1 col
function findPossibleKnightPos(obj) { // 'A4'
var potentialPos = [];
var indexOfObjCol = columns.indexOf(obj.col);
var potentialColumns = [];
columns.forEach((col, index) => {
if(Math.abs(index - indexOfObjCol) <= 2 && Math.abs(index - indexOfObjCol) !== 0) {
potentialColumns.push(col);
}
});
potentialColumns.forEach((col, index) => {
if(Math.abs(columns.indexOf(col) - indexOfObjCol) == 2) {
(rows.indexOf(obj.row-1) !== -1) ? potentialPos.push(col + (obj.row-1)) : null;
(rows.indexOf(obj.row+1) !== -1) ? potentialPos.push(col + (obj.row+1)) : null;
} else {
(rows.indexOf(obj.row-2) !== -1) ? potentialPos.push(col + (obj.row-2)) : null;
(rows.indexOf(obj.row+2) !== -1) ? potentialPos.push(col + (obj.row+2)) : null;
}
});
return potentialPos;
}
function findPossiblePawnPos(obj) {
}
function Piece(name, color, row, col) {
this.name = name;
this.color = color;
this.row = row;
this.col = col;
this.timesMoved = 0;
this.possiblePositions = ['A3', 'A4'];
// position - A1
// check for validity of move
// move.
this.move = function(pos) {
// TODO:check the validity of move.
var currentPos = this.col + this.row;
var nextPos = pos;
this.col = nextPos.split('')[0];
this.row = nextPos.split('')[1];
console.log('I am supposed to move to ', nextPos, 'and my current pos is', currentPos);
// empty the .piece property on the cell object
cells[currentPos].piece = null;
// set an new .piece prop on the nextPos cell obj
// kill logic
cells[nextPos].piece = this;
// flip the currentMover
currentMover = this.color == 'white' ? 'black' : 'white';
selectedPiece = null;
++this.timesMoved;
renderGame();
}
}
function renderGame() {
var htmlString = '';
for(var cell in cells) {
var pieceInfo = cells[cell].piece ? cells[cell].piece.color + '-' + cells[cell].piece.name : '';
htmlString += `<div id="${cell}" class="${cells[cell].color} cell" data-piece="${pieceInfo}"></div>`;
}
var root = document.getElementById('chess-board');
root.innerHTML = htmlString;
//addEventListeners
var allCells = document.querySelectorAll('.cell');
allCells.forEach((cell, index) => {
cell.addEventListener('click', () => {
if(cells[cell.id].piece && (cells[cell.id].piece.color == currentMover)) {
selectedPiece = cells[cell.id].piece;
return;
}
if(selectedPiece) {
selectedPiece.move(cell.id);
}
});
})
}
renderGame();