Skip to content
Open
Show file tree
Hide file tree
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
89 changes: 85 additions & 4 deletions 05week/checkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ const rl = readline.createInterface({
});


function Checker() {
// Your code here
function Checker(color) {
if (color === 'white') {
this.symbol = String.fromCharCode(0x125CF);
this.color = 'white';
}
else {
this.symbol = String.fromCharCode(0x125CB);
this.color = 'black';
}
}

function Board() {
Expand Down Expand Up @@ -52,16 +59,90 @@ function Board() {
console.log(string);
};

// Your code here
this.populateGrid = () => {
// loops through the 8 rows
for (let row = 0; row < 8; row++) {
// ignores rows which should be empty
if (row === 3 || row === 4) continue;
// loops through the 8 columns
for (let col = 0; col < 8; col++) {
// sets current color based on the current row
let color = (row < 3 ? 'white' : 'black');
// alternates cells to populate with either white or black checkers
// then pushes checker to array named checkers
if (row % 2 === 0 && col % 2 === 1) {
this.grid[row][col] = new Checker(color);
} else if (row % 2 === 1 && col % 2 === 0) {
this.grid[row][col] = new Checker(color);
}
}
}
};
}

function Game() {

this.board = new Board();

this.start = function() {
this.board.createGrid();
// Your code here
this.board.populateGrid();
};

this.isLegal = (start, end) => {
// isValidInput function? : Is a two digit number (start and end numbers to sting method and then .length === 2)

// figure out how to call an array inide of an array and determine if start has a piece and end is empty.
// if checker = black : start[0] needs to be 1 less than end[0] && start[1] needs to be either +1 || -1 of end[1]
// if checker = white : start[0] needs to be 1 more than end[0] && start[1] needs to be either +1 || -1 of end[1]
// BONUS IF TIME:
// if checker = black : start[0] needs to be 2 less than end[0] && start[1] needs to be either +2 || -2 of end[1]
// if checker = white : start[0] needs to be 2 more than end[0] && start[1] needs to be either +2 || -2 of end[1]

// There is obviously a way to condense this but I am going to focus on making it work.
if (this.board.grid[start[0]][start[1]]) {
if (color === 'black') {
if ((([start[0]] - 1 === [end[0]]) && (([start[1]] - 1 === [end[1]]) || ([start[1]] + 1 === [end[1]]))) ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block seems repetitive, what would be a good way to reduce redundancies?

(([start[0]] - 2 === [end[0]]) && (([start[1]] - 2 === [end[1]]) || ([start[1]] + 2 === [end[1]])))) {
if (this.board.grid[end[0]][end[1]]) {
// moveChecker();
}
}
} if (color === 'white') {
if ((([start[0]] + 1 === [end[0]]) && (([start[1]] - 1 === [end[1]]) || ([start[1]] + 1 === [end[1]]))) ||
(([start[0]] + 2 === [end[0]]) && (([start[1]] - 2 === [end[1]]) || ([start[1]] + 2 === [end[1]])))) {
if (this.board.grid[end[0]][end[1]]) {
// moveChecker();
}
}
}
} else {
console.log("This is an illegal move, please try again.");
return false;
}
}

this.alternateTurns = (checker) => {

};

this.killChecker = () => {

};

this.moveChecker = (x,y) => {
// steralize the input data (Take the 4 numbers entered and turn them into 2)
const start = x.split('').map(str => Number(str));
const end = y.split('').map(str => Number(str));

// You take the value in the start checker position and you copy it into the end checker location, and then you delete that value from the start location.
// Why doesn't push() work? because it just adds that value to end of the value that is already there? not 100% sure.
// if (isLegal(x, y)) {
this.board.grid[end[0]][end[1]] = this.board.grid[start[0]][start[1]];
this.board.grid[start[0]][start[1]] = null;
// }
};

}

function getPrompt() {
Expand Down
Loading