From e317d53ed3afec469cf87729436460c001474636 Mon Sep 17 00:00:00 2001 From: Laura Colten Date: Mon, 12 Nov 2018 22:19:37 -0600 Subject: [PATCH] checkers --- 05week/checkers.js | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index 15d9953d1..8b4f11048 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -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() { @@ -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(' '); @@ -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(); } }