Best JavaScript code snippet using stryker-parent
rules.js
Source:rules.js
1/* 2 * The rules of checkers.3 * Ted Benson <eob@csail.mit.edu>4 * 5 * This class implements the rules of American checkers. Note that we6 * use the variant that does not require to "eat" another checker if that7 * is a possibility.8 *9 */10var Rules = function(board) {11 /***************************************************************12 * "Public" functions (if such a thing could be enforced)13 * Everything you should need as the user of Rules.14 * 15 * - makeMove(checker, turnDirection, playerDirection, toRow, toCol)16 * - makeRandomMove(checker, playerDirection)17 *18 **************************************************************/19 /**20 * Attempts to make a move. If the move is valid, this will mutate21 * the board object and return a list of jumped pieces that were removed22 * from the board as a result.23 *24 * Input25 * - checker: the checker object you would like to move26 * - turnDirection: which direction (+1, -1) represents the current turn27 * - playerDirection: which direction (+1, -1) represents the color of `checker` (first argument)28 * - toRow: which row would you like to move checker to29 * - toCol: which column would you like to move checker to30 *31 * Note about directions:32 * For rule checking, the Rules object represents both turns and players by directions on the33 * board, either +1 or -1, not by piece coloring. If the turn is +1 and the checker moved represents34 * player -1 for exampe, the Rules object will reject this move and return null. Don't worry about35 * kings being bidirectional -- this object knows how to take this into account.36 *37 * Success Return: 38 * { 'from_col': X,39 * 'to_col': XPrime,40 * 'from_row': Y,41 * 'to_row':Z,42 * 'made_king':false,43 * 'removed':[44 * {'col':X, 'row':Y, 'color':Z, 'isKing':true},45 * {'col':X, 'row':Y, 'color':Z, 'isKing':false}46 * ]47 * }48 *49 * Error Return: (when the move is invalid)50 * null51 */ 52 this.makeMove = function(checker, turnDirection, playerDirection, toRow, toCol) {53 var ramifications = this.ramificationsOfMove(checker,54 turnDirection,55 playerDirection,56 toRow,57 toCol);58 var wasKingBefore = checker.isKing;59 // Invalid move?60 if (ramifications == null) {61 return null;62 }63 // If valid, remember where we started 64 ramifications['from_col'] = checker.col;65 ramifications['from_row'] = checker.row;66 67 // Then mutate the board68 69 // 1. Move the piece70 board.moveTo(checker, ramifications['to_row'], ramifications['to_col']); 71 ramifications['made_king'] = ((! wasKingBefore) && (checker.isKing));72 //if (ramifications['made_king']) alert("made king: " + ramifications['made_king']);73 74 // 2. Remove dead checkers75 for (var i=0; i<ramifications['remove'].length; i++) {76 //if (ramifications['remove'][i]['isKing']) alert("removed king");77 board.remove(78 board.getCheckerAt(79 ramifications['remove'][i]['row'],80 ramifications['remove'][i]['col']81 )82 );83 }84 return ramifications;85 }86 /**87 * Makes a random move. If no random move can be found for the player,88 * returns null. If a random move can be found, provides a result object89 * in the shape of that from makeMove(..)90 */91 this.makeRandomMove = function(playerColor, playerDirection) {92 // Get all checkers of my color93 var checkers = board.getAllCheckers();94 var myCheckers = [];95 for (var i=0; i<checkers.length; i++) {96 if (checkers[i].color == playerColor) {97 myCheckers.push(checkers[i]);98 }99 }100 // now randomly sort101 myCheckers = this.shuffle(myCheckers);102 103 for (var i=0; i<myCheckers.length; i++) {104 var validMoves = this.validMovesFor(myCheckers[i], playerDirection);105 if (validMoves.length > 0) {106 // Randomize the moves107 validMoves = this.shuffle(validMoves);108 // Make the move!109 var move = validMoves[0];110 return this.makeMove(111 myCheckers[i],112 playerDirection,113 playerDirection,114 move.to_row,115 move.to_col116 );117 }118 }119 // If were still here, no move is possible120 return null;121 }122 123 /***************************************************************124 * "Private" functions (if such a thing could be enforced)125 * You probably don't need to call these126 **************************************************************/127 // Returns null on an invalid move128 this.ramificationsOfMove= function(checker, turnDirection, playerDirection, toRow, toCol) {129 if (playerDirection != turnDirection) {130 return null;131 }132 var validMoves = this.validMovesFor(checker, playerDirection);133 for (var i = 0; i<validMoves.length; i++) {134 if ((validMoves[i].to_col == toCol) && (validMoves[i].to_row == toRow)) {135 return validMoves[i];136 }137 }138 return null;139 }140 /**141 * Returns a list of valid moves.142 */143 this.validMovesFor = function(checker, playerDirection) {144 assertTrue(((playerDirection== 1) || (playerDirection== -1)), "Direction must be 1 or -1");145 var validMoves = [];146 /** A checker can move forward if:147 * 1. The space is valid148 * 2. The space isn't occupied149 */150 for (var i = -1; i < 2; i++) {151 if (i != 0) {152 if (board.isValidLocation(checker.row + playerDirection, checker.col + i) &&153 board.isEmptyLocation(checker.row + playerDirection, checker.col + i)) {154 validMoves.push({'to_col': checker.col + i,155 'to_row': checker.row + playerDirection,156 'remove': []});157 }158 }159 }160 /** A checker can move backward if:161 * 1. The space is valid162 * 2. The space isn't occupied163 * 3. The checker is a king164 */165 for (var i = -1; i < 2; i++) {166 if (i != 0) {167 if (board.isValidLocation(checker.row - playerDirection, checker.col + i) &&168 board.isEmptyLocation(checker.row - playerDirection, checker.col + i) &&169 (checker.isKing == true)) {170 validMoves.push({'to_col': checker.col + i,171 'to_row': checker.row - playerDirection,172 'remove': []});173 }174 }175 }176 /** A checker can jump177 */178 var jumps = this.validJumpsFor(checker, playerDirection, [], checker.row, checker.col);179 for (var i=0; i<jumps.length; i++) {180 validMoves.push(this.collapseJumpSequence(jumps[i]));181 }182 return validMoves;183 }184 this.collapseJumpSequence = function(jumpSequence) {185 var move = {186 'to_col':jumpSequence[jumpSequence.length - 1].col,187 'to_row':jumpSequence[jumpSequence.length - 1].row,188 'remove':[]189 };190 191 for (var j=0; j<jumpSequence.length; j++) {192 move['remove'].push({193 'row':jumpSequence[j].killedRow,194 'col':jumpSequence[j].killedCol,195 'color':board.getCheckerAt(jumpSequence[j].killedRow, jumpSequence[j].killedCol).color,196 'isKing':board.getCheckerAt(jumpSequence[j].killedRow, jumpSequence[j].killedCol).isKing197 });198 }199 200 return move;201 }202 this.alreadyJumpedPiece = function(jumpSeq, row, col) {203 var alreadyJumped = false;204 for (j=0; j<jumpSeq.length; j++) {205 if ((col == jumpSeq[j].killedCol) && (row == jumpSeq[j].killedRow)) {206 alreadyJumped = true;207 }208 }209 return alreadyJumped;210 }211 this.copyJumpSequence = function(jumpSeq) {212 var newJumpSeq = [];213 for (var j=0; j<jumpSeq.length; j++) {214 newJumpSeq.push(jumpSeq[j]);215 }216 return newJumpSeq;217 }218 this.validJumpsFor = function(checker, playerDirection, jumpSeq, curRow, curCol) {219 var possibleDestinations = [220 [curRow + 2, curCol + 2],221 [curRow + 2, curCol - 2],222 [curRow - 2, curCol + 2],223 [curRow - 2, curCol - 2]224 ];225 var retSeqs = [];226 for (var i=0; i<possibleDestinations.length; i++) {227 var toRow = possibleDestinations[i][0];228 var toCol = possibleDestinations[i][1];229 if (this.isValidJump(checker, curRow, curCol, toRow, toCol, playerDirection)) {230 // Add this jump to the list of jumps to return231 var jumped = {232 'killedCol':(curCol + toCol)/2,233 'killedRow':(curRow + toRow)/2,234 'col':toCol,235 'row':toRow}236 if (! this.alreadyJumpedPiece(jumpSeq, jumped.killedRow, jumped.killedCol)) {237 // Copy the jumpSeq array to pass in238 var newJumpSeq = this.copyJumpSequence(jumpSeq);239 newJumpSeq.push(jumped);240 var futureJumps = this.validJumpsFor(checker, playerDirection, newJumpSeq, toRow, toCol);241 for (j = 0; j<futureJumps.length; j++) {242 retSeqs.push(futureJumps[j]);243 }244 // This is the terminal leaf245 var lastCopy = this.copyJumpSequence(jumpSeq);246 lastCopy.push(jumped);247 retSeqs.push(lastCopy);248 } // if wasn't jumping existing piece249 } // if is valid jump250 } // for each possibnle destination251 return retSeqs; // No valid seqs.252 }253 this.shuffle = function(array) {254 var tmp, current, top = array.length;255 if(top) while(--top) {256 current = Math.floor(Math.random() * (top + 1));257 tmp = array[current];258 array[current] = array[top];259 array[top] = tmp;260 }261 return array;262 }263 this.isValidJump = function(checker, fromRow, fromCol, toRow, toCol, playerDirection) {264 // Is the to-location a valid one?265 if (! board.isValidLocation(toRow, toCol)) return false;266 267 // Is the to-location occupied?268 if (! board.isEmptyLocation(toRow, toCol)) return false;269 // Normal players must not jump backward270 if ((((toRow - fromRow) * playerDirection) < 0) && (! checker.isKing)) return false; 271 // Jump must be two spaces horizontally272 if (Math.abs(toRow - fromRow) != 2) return false;273 if (Math.abs(toCol - fromCol) != 2) return false;274 // A piece must jump another275 if (board.isEmptyLocation((toRow+fromRow)/2, (toCol+fromCol)/2)) return false;276 // A piece must not jump its own kind277 if (board.getCheckerAt((toRow+fromRow)/2, (toCol+fromCol)/2).color == checker.color) return false;278 return true; 279 }280 /**281 * UTIL282 */283 function assertTrue(f, s){284 if (!f) {285 alert(s);286 }287 }288 ...
initial.js
Source:initial.js
1'use strict';2const assert = require('assert');3const { kill } = require('process');4const readline = require('readline');5const rl = readline.createInterface({6 input: process.stdin,7 output: process.stdout8});9class Checker {10 constructor(isWhite) {11 this.isWhite = isWhite12 if (isWhite) {13 this.symbol = String.fromCharCode(0x125CB)14 } else {15 this.symbol = String.fromCharCode(0x125CF)16 }17 }18}19class Board {20 constructor() {21 this.grid = []22 for (let i = 0; i < 8; i++) {23 let row = []24 for (let j = 0; j < 8; j++) {25 row.push[null]26 }27 this.grid.push(row)28 }29 }30 createCheckers() {31 const whitePositions = [[0, 1], [0, 3], [0, 5], [0, 7],32 [1, 0], [1, 2], [1, 4], [1, 6],33 [2, 1], [2, 3], [2, 5], [2, 7]];34 const blackPositions = [[5, 0], [5, 2], [5, 4], [5, 6],35 [6, 1], [6, 3], [6, 5], [6, 7],36 [7, 0], [7, 2], [7, 4], [7, 6]];37 for (let i = 0; i < whitePositions.length; i++) {38 let white = new Checker(true)39 let pos = whitePositions[i]40 let row = pos[0]41 let col = pos[1]42 this.grid[row][col] = white43 }44 for (let i = 0; i < blackPositions.length; i++) {45 let black = new Checker(false)46 let pos = blackPositions[i]47 let row = pos[0]48 let col = pos[1]49 this.grid[row][col] = black50 }51 }52}53class Game {54 constructor() {55 this.board = new Board()56 }57 printBoard() {58 let string = " 0 1 2 3 4 5 6 7\n";59 for (let i = 0; i < this.board.grid.length; i++) {60 let row = this.board.grid[i]61 let rowString = `${i} `62 for (let j = 0; j < row.length; j++) {63 const val = row[j]64 if (val) {65 rowString += `${val.symbol} `66 } else {67 rowString += " "68 }69 }70 rowString += "\n"71 string += rowString72 }73 console.log(string)74 }75 start() {76 this.board.createCheckers()77 }78 move(origRow, origCol, newRow, newCol) {79 const checker = this.board.grid[origRow][origCol]80 if (!checker) {81 console.log("Error: No checker on square")82 return83 }84 if (Math.abs(origCol - newCol) === 2) {85 // Killing86 if (checker.isWhite) {87 if (newRow !== origRow + 2) {88 console.log("Error: invalid row")89 return90 }91 } else {92 if (newRow !== origRow - 2) {93 console.log("Error: invalid row")94 return95 }96 }97 const killedRow = checker.isWhite ? origRow + 1 : origRow - 198 const killedCol = newCol < origCol ? origCol - 1 : origCol + 199 const killedChecker = this.board.grid[killedRow][killedCol]100 if (killedChecker === null) {101 console.log("Error: no checker to kill")102 return103 }104 if (killedChecker.isWhite === checker.isWhite) {105 console.log("Error: wrong checker to kill")106 return107 }108 this.board.grid[killedRow][killedCol] = null109 } else {110 // Moving111 if (Math.abs(origCol - newCol) !== 1) {112 console.log("Error: invalid column")113 return114 }115 if (checker.isWhite) {116 if (newRow !== origRow + 1) {117 console.log(`Error: invalid row ${newRow} for white, must be ${origRow + 1}`)118 return119 }120 } else {121 if (newRow !== origRow - 1) {122 console.log(`Error: invalid row ${newRow} for black, must be ${origRow - 1}`)123 return124 }125 }126 }127 this.board.grid[origRow][origCol] = null128 this.board.grid[newRow][newCol] = checker129 }130}131function getPrompt() {132 game.printBoard()133 rl.question('from which row,col?: ', (origRowCol) => {134 rl.question('to which row,col?: ', (newRowCol) => {135 const [origRow, origCol] = origRowCol.split(",")136 const [newRow, newCol] = newRowCol.split(",")137 game.move(parseInt(origRow), parseInt(origCol), parseInt(newRow), parseInt(newCol))138 getPrompt()139 })140 })141}142let game = new Game()143game.start()...
CalcBoardChanges.js
Source:CalcBoardChanges.js
1export default {2 calcPieceNumJumped: (board, validSpot, activeSquare) => {3 let pieceNumJumped4 if (Math.abs(validSpot.row - activeSquare.row) > 1) {5 let killedRow = (validSpot.row + activeSquare.row) / 26 let killedCol = (validSpot.col + activeSquare.col) / 27 pieceNumJumped = board[killedRow][killedCol].id8 } else {9 pieceNumJumped = 010 }11 return pieceNumJumped;12 },13 calcNewBoardMatrix: (board, validSpot, activeSquare) => {14 let pieceNumJumped15 let killedRow16 let killedCol17 if (Math.abs(validSpot.row - activeSquare.row) > 1) {18 killedRow = (validSpot.row + activeSquare.row) / 219 killedCol = (validSpot.col + activeSquare.col) / 220 pieceNumJumped = board[killedRow][killedCol].id21 }22 let boardMatrix = JSON.parse(JSON.stringify(board))23 let dataToUpdate = { row: validSpot.row, col: validSpot.col }24 //if you get to the end of the board, make the piece a queen25 if (validSpot.row === 7 || validSpot.row === 0) {26 dataToUpdate = { ...dataToUpdate, queen: true }27 }28 //if you moved two squares, it was an attack - kill the jumped piece29 if (pieceNumJumped > 0) {30 boardMatrix[killedRow][killedCol] = { active: 0, row: killedRow, col: killedCol }31 }32 //copy the old piece into the new location33 boardMatrix[validSpot.row][validSpot.col] = {34 ...boardMatrix[activeSquare.row][activeSquare.col],35 ...dataToUpdate36 }37 //delete the old piece from the old location38 boardMatrix[activeSquare.row][activeSquare.col] = { active: 0, row: activeSquare.row, col: activeSquare.col }39 return boardMatrix40 }...
Using AI Code Generation
1var killedRow = require('stryker-parent').killedRow;2var killedRow = require('stryker-parent').killedRow;3var killedRow = require('stryker-child').killedRow;4var killedRow = require('stryker-child').killedRow;5var killedRow = require('stryker-grandchild').killedRow;6var killedRow = require('stryker-grandchild').killedRow;7var killedRow = require('stryker-greatgrandchild').killedRow;8var killedRow = require('stryker-greatgrandchild').killedRow;9var killedRow = require('stryker-greatgreatgrandchild').killedRow;10var killedRow = require('stryker-greatgreatgrandchild').killedRow;11var killedRow = require('stryker-greatgreatgreatgrandchild').killedRow;12var killedRow = require('stryker-greatgreatgreatgrandchild').killedRow;13var killedRow = require('stryker-greatgreatgreatgreatgrandchild').killedRow;14var killedRow = require('stryker-greatgreatgreatgreatgrandchild').killedRow;15var killedRow = require('stryker-greatgreatgreatgreatgreatgrandchild').killedRow;16var killedRow = require('stryker-greatgreatgreatgreatgreatgrandchild').killedRow;17var killedRow = require('stryker-greatgreatgreatgreatgreatgreatgrandchild').killedRow;18var killedRow = require('stryker-greatgreatgreatgreatgreatgreatgrandchild').killedRow;
Using AI Code Generation
1const killedRow = require('stryker-parent').killedRow;2killedRow(2);3killedRow(3);4killedRow(4);5const killedRow = require('stryker-child').killedRow;6killedRow(2);7killedRow(3);8killedRow(4);9const killedRow = require('stryker-grandchild').killedRow;10killedRow(2);11killedRow(3);12killedRow(4);13const killedRow = require('stryker-greatgrandchild').killedRow;14killedRow(2);15killedRow(3);16killedRow(4);
Using AI Code Generation
1var killedRow = require('stryker-parent').killedRow;2killedRow(0, 0, 'foo');3module.exports = {4 killedRow: function (row, column, source) {5 console.log('killed row ' + row + ' column ' + column + ' source ' + source);6 }7};
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!