Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions 05week/checkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ const rl = readline.createInterface({
});


function Checker() {
// Your code here
class Checker {
constructor(color) {
if (color === 'white') {
this.symbol = '○';
} else {
this.symbol = '●';
}
}
}

class Board {
constructor() {
this.grid = []
this.grid = [];
this.checkers = [];
}
// method that creates an 8x8 array, filled with null values
createGrid() {
Expand Down Expand Up @@ -51,8 +58,46 @@ class Board {
}
console.log(string);
}
createCheckers() {
let whitePosition = [
[0, 1], [0, 3], [0, 5], [0, 7],
[1, 0], [1, 2], [1, 4], [1, 6],
[2, 1], [2, 3], [2, 5], [2, 7]];
let blackPosition = [
[5, 0], [5, 2], [5, 4], [5, 6],
[6, 1], [6, 3], [6, 5], [6, 7],
[7, 0], [7, 2], [7, 4], [7, 6]
];
for (let i=0;i<12;i++){
// whitePosition[i].push(this.grid);
// whitePosition[i].push(this.checkers);
let whiteChecker = new Checker('white');
let whiteRow = whitePosition[i][0];
let whiteCol = whitePosition[i][1];

blackPosition[i].push(this.grid);
// blackPosition[i].push(this.checkers);
let blackChecker = new Checker ('black');
let blackRow = blackPosition[i][0];
let blackCol = blackPosition[i][1];

this.grid[whiteRow][whiteCol] = whiteChecker;
this.grid[blackRow][blackCol] = blackChecker;
this.checkers.push(whiteChecker, blackChecker);
}
}
selectChecker(row, col) {
// this.board.grid = [start][end];
return this.grid[row][col];
}
killChecker(position){
// this.selectChecker = (position[0], position[1]);
let checker = this.selectChecker(position[0], position[1])
let indexChecker = this.checkers.indexOf(checker)
this.checkers.splice(indexChecker, 1);

// Your code here
this.grid[position[0]][position[1]] = null;
}
}

class Game {
Expand All @@ -61,6 +106,23 @@ class Game {
}
start() {
this.board.createGrid();
this.board.createCheckers();
}
moveChecker(start, end) {
const startRow= start.charAt(0);
const startCol = start.charAt(1);
const endRow = end.charAt(0);
const endCol = end.charAt(1);
console.log('start: ' + start);
console.log('end: ' + end);
const checker = this.board.selectChecker(start[0], start[1]);
this.board.grid[endRow][endCol] = checker;
// this.board.grid[endRow][endCol] = this.board.grid[startRow][startCol]
this.board.grid[startRow][startCol] = null;
if (Math.sqrt((endRow-startRow)^2 + (endCol-startCol)^2) >= 2){
// const position = [((startRow + endRow)/2),((startCol+endCol)/2)];
this.board.killChecker[(startRow + endRow)/2,(startCol+endCol)/2];
}
}
}

Expand Down