forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
05week checkers #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cBoss1029
wants to merge
4
commits into
gh-pages
Choose a base branch
from
05week-checkers
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b07e774
initial commit
cBoss1029 3af1997
working move and jump function, successfully printing board
cBoss1029 b0083ee
FINALLY passing all tests, just needs checkForWin function
cBoss1029 2e8fea8
it's working. checkForWin function and all. I must sleep
cBoss1029 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,28 +8,46 @@ const rl = readline.createInterface({ | |
| }); | ||
|
|
||
|
|
||
| function Checker() { | ||
| class Checker { | ||
| // Your code here | ||
| constructor() { | ||
| this.symbol1 = 'B'; | ||
| this.symbol2 = 'A'; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function Board() { | ||
| this.grid = []; | ||
| // creates an 8x8 array, filled with null values | ||
| this.createGrid = function() { | ||
| class Board { | ||
| constructor() { | ||
|
|
||
| this.grid = []; | ||
| this.checkers = []; | ||
|
|
||
| // creates an 8x8 array, filled with null values | ||
|
|
||
| } | ||
| createGrid() { | ||
| // loop to create the 8 rows | ||
| for (let row = 0; row < 8; row++) { | ||
| this.grid[row] = []; | ||
| // push in 8 columns of nulls | ||
| for (let column = 0; column < 8; column++) { | ||
| const checker = new Checker(); | ||
|
|
||
|
|
||
| this.grid[row].push(null); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } | ||
| }; | ||
| } | ||
|
|
||
| // prints out the board | ||
| this.viewGrid = function() { | ||
| viewGrid() { | ||
| // add our column numbers | ||
| let string = " 0 1 2 3 4 5 6 7\n"; | ||
|
|
||
| for (let row = 0; row < 8; row++) { | ||
| // we start with our row number in our array | ||
| const rowOfCheckers = [row]; | ||
|
|
@@ -38,30 +56,198 @@ function Board() { | |
| // if the location is "truthy" (contains a checker piece, in this case) | ||
| if (this.grid[row][column]) { | ||
| // push the symbol of the check in that location into the array | ||
| rowOfCheckers.push(this.grid[row][column].symbol); | ||
|
|
||
| rowOfCheckers.push(this.grid[row][column]); | ||
|
|
||
|
|
||
| } else { | ||
| // just push in a blank space | ||
| rowOfCheckers.push(' '); | ||
| } | ||
| } | ||
|
|
||
| // join the rowOfCheckers array to a string, separated by a space | ||
| string += rowOfCheckers.join(' '); | ||
| // add a 'new line' | ||
| string += "\n"; | ||
| } | ||
| console.log(string); | ||
| }; | ||
| } | ||
|
|
||
| // Your code here | ||
| //Everything works, but I am aware it is in need of some serious refactoring. | ||
| //which I will do in the future. I am very tired. | ||
| playerStartingPositions() { | ||
| const checker = new Checker(); | ||
|
|
||
| this.grid.forEach((row, index1) => { | ||
| row.forEach((column, index2) => { | ||
| if (index1 % 2 === 0 && index1 < 3) { | ||
| if (index2 % 2 !== 0) { | ||
| this.grid[index1][index2] = checker.symbol1; | ||
| this.checkers.push(this.grid[index1][index2]); | ||
| } | ||
| } else if (index1 % 2 !== 0 && index1 < 3) { | ||
| if (index2 % 2 === 0) { | ||
| this.grid[index1][index2] = checker.symbol1; | ||
| this.checkers.push(this.grid[index1][index2]) | ||
| } | ||
| } else if (index1 % 2 === 0 && index1 > 4) { | ||
| if (index2 % 2 !== 0) { | ||
| this.grid[index1][index2] = checker.symbol2; | ||
| this.checkers.push(this.grid[index1][index2]) | ||
| } | ||
| } else if (index1 % 2 !== 0 && index1 > 4) { | ||
| if (index2 % 2 === 0) { | ||
| this.grid[index1][index2] = checker.symbol2; | ||
| this.checkers.push(this.grid[index1][index2]) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| } | ||
| } | ||
| function Game() { | ||
|
|
||
| this.board = new Board(); | ||
| class Game { | ||
| constructor() { | ||
|
|
||
| this.start = function() { | ||
| this.board = new Board(); | ||
| this.playerTurn = 'B'; | ||
| /*be aware (and I'm not sure how this happened) player 'B' is player 1 and is represented | ||
| on the board as 'A', and vice versa.*/ | ||
| this.player1ClaimedPieces = 0; | ||
| this.player2ClaimedPieces = 0; | ||
| this.player1IllegalMoves = 0; | ||
| this.player2IllegalMoves = 0; | ||
| } | ||
| start() { | ||
| this.board.createGrid(); | ||
|
|
||
| // Your code here | ||
| }; | ||
| this.board.playerStartingPositions(); | ||
|
|
||
| } | ||
|
|
||
| moveChecker(whichPiece, toWhere) { | ||
| whichPiece = whichPiece.split('').map(Number); | ||
| toWhere = toWhere.split('').map(Number); | ||
| if (this.isLegalMove(whichPiece, toWhere) && this.jumpedPiece(whichPiece, toWhere)) { | ||
| if (this.playerTurn === 'A') { //if player 2 jumps. . . | ||
| if (whichPiece[1] < toWhere[1]) { //. . .down and to the right | ||
| this.board.grid[toWhere[0] - 1][toWhere[1] - 1] = null; | ||
| } else if (whichPiece[1] > toWhere[1]) { //. . .or down and to the left | ||
| this.board.grid[toWhere[0] - 1][toWhere[1] + 1] = null; | ||
| } | ||
| this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; | ||
| this.board.grid[whichPiece[0]][whichPiece[1]] = null; | ||
| this.player2ClaimedPieces++; | ||
| this.board.checkers.shift(); | ||
| this.playerTurn = 'B' | ||
| this.player2IllegalMoves = 0; | ||
|
|
||
|
|
||
| } else { // player 1 | ||
| if (whichPiece[1] < toWhere[1]) { //. . .up and to the right | ||
| this.board.grid[toWhere[0] + 1][toWhere[1] - 1] = null; | ||
| } else if (whichPiece[1] > toWhere[1]) { //. . .up and to the left | ||
| this.board.grid[toWhere[0] + 1][toWhere[1] + 1] = null; | ||
| } | ||
| this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; | ||
| this.board.grid[whichPiece[0]][whichPiece[1]] = null; | ||
| this.player1ClaimedPieces++; | ||
| this.board.checkers.shift(); | ||
| this.playerTurn = 'A' | ||
| this.player1IllegalMoves = 0; | ||
|
|
||
|
|
||
| } | ||
| } else if (this.isLegalMove(whichPiece, toWhere)) { | ||
| if (this.playerTurn === 'A') { | ||
| this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; | ||
| this.board.grid[whichPiece[0]][whichPiece[1]] = null; | ||
| this.playerTurn = 'B' | ||
| this.player2IllegalMoves = 0; | ||
|
|
||
| } else { | ||
| this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; | ||
| this.board.grid[whichPiece[0]][whichPiece[1]] = null; | ||
| this.playerTurn = 'A' | ||
| this.player1IllegalMoves = 0; | ||
|
|
||
| } | ||
| } else { | ||
| console.log('Unable to move.'); | ||
|
|
||
| this.playerIllegalMoveCounter(whichPiece, toWhere); | ||
| this.checkForWin(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| jumpedPiece(whichPiece, toWhere) { | ||
| if (this.playerTurn === 'A' && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplify, just return the evaluation |
||
| toWhere[0] - whichPiece[0] === 2 && | ||
| !this.board.grid[toWhere[0]][toWhere[1]]) { | ||
| return true; | ||
| } else if (this.playerTurn === 'B' && | ||
| whichPiece[0] - toWhere[0] === 2 && | ||
| !this.board.grid[toWhere[0]][toWhere[1]]) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| isLegalMove(whichPiece, toWhere) { | ||
| if (this.playerTurn === 'A' && | ||
| whichPiece[0] < toWhere[0] && | ||
| !this.board.grid[toWhere[0]][toWhere[1]]) { | ||
| return true; | ||
| } else if (this.playerTurn === 'B' && | ||
| whichPiece[0] > toWhere[0] && | ||
| !this.board.grid[toWhere[0]][toWhere[1]]) { | ||
| return true; | ||
| } else { | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| playerIllegalMoveCounter(whichPiece, toWhere) { | ||
| const legalMove = this.isLegalMove(whichPiece, toWhere); | ||
| if (this.playerTurn === 'A' && !legalMove) { | ||
| this.player2IllegalMoves++; | ||
| console.log(this.player2IllegalMoves, 'p2 illegal moves'); | ||
| } else if (this.playerTurn === 'B' && !legalMove) { | ||
| this.player1IllegalMoves++; | ||
| console.log(this.player1IllegalMoves, 'p1 illegal moves'); | ||
|
|
||
| } | ||
| } | ||
| checkForWin() { | ||
| if (this.playerTurn === 'A' && this.player2ClaimedPieces === 12) { | ||
| console.log('Player 2 wins!!'); | ||
| this.resetGame(); | ||
| } else if (this.playerTurn === 'A' && this.player2IllegalMoves === 12 - this.player1ClaimedPieces) { | ||
| console.log('Player 1 wins!!'); | ||
| this.resetGame(); | ||
| } else if (this.playerTurn === 'B' && this.player1ClaimedPieces === 12) { | ||
| console.log('Player 1 wins!!'); | ||
| this.resetGame(); | ||
| } else if (this.playerTurn === 'B' && this.player1IllegalMoves === 12 - this.player2ClaimedPieces) { | ||
| console.log('Player 2 wins!!'); | ||
| this.resetGame(); | ||
| } | ||
| } | ||
| resetGame() { | ||
| this.playerTurn = 'B'; | ||
| this.player1ClaimedPieces = 0; | ||
| this.player2ClaimedPieces = 0; | ||
| this.player1IllegalMoves = 0; | ||
| this.player2IllegalMoves = 0; | ||
| this.board.createGrid(); | ||
| this.board.playerStartingPositions(); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
|
|
@@ -90,8 +276,8 @@ if (typeof describe === 'function') { | |
| }); | ||
| }); | ||
|
|
||
| describe('Game.moveChecker()', function () { | ||
| it('should move a checker', function () { | ||
| describe('Game.moveChecker()', function() { | ||
| it('should move a checker', function() { | ||
| assert(!game.board.grid[4][1]); | ||
| game.moveChecker('50', '41'); | ||
| assert(game.board.grid[4][1]); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be split up into smaller easier to maintain functions.