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
58 changes: 53 additions & 5 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(team) {
if (team === "yellow") {
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 @@ -42,7 +49,7 @@ class Board {
} else {
// just push in a blank space
rowOfCheckers.push(' ');
}
}
}
// join the rowOfCheckers array to a string, separated by a space
string += rowOfCheckers.join(' ');
Expand All @@ -52,15 +59,56 @@ class Board {
console.log(string);
}

// Your code here
createCheckers() {
const yellowPos = [
[0,1], [0,3], [0,5], [0,7],
[1,0], [1,2], [1,4], [1,6],
[2,1], [2,3], [2,5], [2,7]
];
const bluePos = [
[5,0], [5,2], [5,4], [5,6],
[6,1], [6,3], [6,5], [6,7],
[7,0], [7,2], [7,4], [7,6]
];
yellowPos.forEach((position) => {
const checker = new Checker("yellow");
this.checkers.push(checker);
this.grid[position[0]][position[1]] = checker;
})
bluePos.forEach((position) => {
const checker = new Checker("blue");
this.checkers.push(checker);
this.grid[position[0]][position[1]] = checker;
})
}
}

class Game {
constructor() {
this.board = new Board;
}
moveChecker(position1, position2) {
const checker = this.board.grid[position1[0]][position1[1]];
this.board.grid[position2[0]][position2[1]] = checker;
this.board.grid[position1[0]][position1[1]] = null;
const row1 = Number(position1[0]);
const row2 = Number(position2[0]);
const column1 = Number(position1[1]);
const column2 = Number(position2[1]);
if (Math.abs(row2 - row1) > 1) {
const rowAvg = (row1 + row2) / 2;
const columnAvg = (column1 + column2) / 2;
const deadChecker = this.board.grid[rowAvg][columnAvg];
this.board.checkers = this.board.checkers.filter(checker => {
return checker !== deadChecker;
})
this.board.grid[rowAvg][columnAvg] = null;
}

}
start() {
this.board.createGrid();
this.board.createCheckers();
}
}

Expand Down