diff --git a/05week/checkers.js b/05week/checkers.js index 8f33a089c..eb0da804b 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -7,9 +7,26 @@ const rl = readline.createInterface({ output: process.stdout }); +let playerTurn = 'O'; -function Checker() { - // Your code here +/*function Checker() { + this.king = no; + this.symbol = + //define Checker attributes + //checker.create per checker +}*/ + +class Checker { + constructor(symbol, king){ + this.symbol = symbol; + this.king = king; + //this.row = game.board.grid[row]; + //this.column = game.board.grid[column]; + //this.where = this.row.tostring() + this.column.tostring(); + } + moveChecker(whichPiece, toWhere){ + + } } function Board() { @@ -21,7 +38,16 @@ function Board() { this.grid[row] = []; // push in 8 columns of nulls for (let column = 0; column < 8; column++) { - this.grid[row].push(null); + if (((row%2==0)&&(column%2!=0))||((row%2!=0)&&(column%2==0))) { + if (row<3){ + this.grid[row].push(new Checker('X',false)); + } + if (row>4){ + this.grid[row].push(new Checker('O',false)); + } + }else{ + this.grid[row].push(null); + } } } }; @@ -57,15 +83,100 @@ function Board() { function Game() { this.board = new Board(); + this.moveChecker = function(whichPiece, toWhere){ + let x1 = Number(whichPiece.charAt(0)); + let y1 = Number(whichPiece.charAt(1)); + let x2 = Number(toWhere.charAt(0)); + let y2 = Number(toWhere.charAt(1)); + //If the player enters an empty square as whichPiece + if (game.board.grid[y1][x1]===null){ + console.log("The square you specified in response to 'Which piece?' has no piece in it. Please try again.") + return; + } + + //If the 'O' player enters a square with the wrong piece as whichPiece + if (playerTurn==='O'){ + if(game.board.grid[y1][x1].symbol==='X'){ + console.log("The square you specified in response to 'Which piece?' has an 'X' piece in it. Please try again.") + return; + }else{ + if(game.board.grid[y2][x2]!=null){ + console.log("The square you are trying to move to is already occupied. Please try again.") + return; + }else{ +// if (((y2!=y1+1)&&(Math.abs(x2-x1)!=1))|| +// (((y2!=y1+2)&&(Math.abs(x2-x1)!=2))&& +// (((x2>x1)&&(game.board.grid[y1+1][x1+1].symbol!='X'))|| +// ((x1>x2)&&(game.board.grid[y1+1][x1-1].symbol!='X'))))){ +// console.log("The square you want to move to is not diagonal from the square with the piece in it. Please try again."); +// return; +// }else{ + let currentChecker = game.board.grid[y1][x1]; + game.board.grid[y2].splice(x2,1,currentChecker); + game.board.grid[y1].splice(x1,1,null); + if (Math.abs(x2-x1)==2){ + let deadx = (x1+x2)/2; + let deady = (y1+y2)/2; + game.board.grid[deady].splice(deadx,1,null); + } + +// } + } + + //if toWhere is diagonal and empty, move piece + //if toWhere is diagonal and occupied by same piece, console.log error + //if toWhere is diagonal and occupied by opposite piece and the space diagonal from THAT is empty, capture piece + //if toWhere row is 0, king + //if king, allow backwards moves + //repetitive captures (if possible) + } + playerTurn='X'; + return; + } + + //If the 'X' player enters a square with the wrong piece as whichPiece + if (playerTurn==='X'){ + if(game.board.grid[y1][x1].symbol==='O'){ + console.log("The square you specified in response to 'Which piece?' has an 'X' piece in it. Please try again.") + return; + }else{ + if(game.board.grid[y2][x2]!=null){ + console.log("The square you are trying to move to is already occupied. Please try again.") + return; + }else{ + let currentChecker = game.board.grid[y1][x1]; + game.board.grid[y2].splice(x2,1,currentChecker); + game.board.grid[y1].splice(x1,1,null); + if (Math.abs(x2-x1)==2){ + let deadx = (x1+x2)/2; + let deady = (y1+y2)/2; + game.board.grid[deady].splice(deadx,1,null); + } + } + //if toWhere is diagonal and empty, move piece + //if toWhere is diagonal and occupied by same piece, console.log error + //if toWhere is diagonal and occupied by opposite piece and the space diagonal from THAT is empty, capture piece + //if toWhere row is 0, king + //if king, allow backwards moves + //repetitive captures (if possible) + } + playerTurn='O'; + return; + } + } + //define moveChecker function + //flip between players this.start = function() { this.board.createGrid(); + console.log('Refer to each piece/space by the column number then row number such as "10" or "67"') // Your code here }; } function getPrompt() { game.board.viewGrid(); + console.log("It's Player " + playerTurn + "'s turn."); rl.question('which piece?: ', (whichPiece) => { rl.question('to where?: ', (toWhere) => { game.moveChecker(whichPiece, toWhere); diff --git a/05week/spaceTravelToMars.js b/05week/spaceTravelToMars.js index ce258a382..ef861aca7 100644 --- a/05week/spaceTravelToMars.js +++ b/05week/spaceTravelToMars.js @@ -2,6 +2,8 @@ let assert = require('assert'); +let good2go = null; + let jobTypes = { pilot: 'MAV', mechanic: 'Repair Ship', @@ -9,7 +11,62 @@ let jobTypes = { programmer: 'Any Ship!' }; -// Your code here +class CrewMember { + constructor(name, job, specialSkill){ + this.name = name; + this.job = job; + this.specialSkill = specialSkill; + this.ship = null; + } + enterShip(x){ + this.ship = x; + x.crew.push(this); + } +} + +class Ship { + constructor(name, type, ability){ + this.name = name; + this.type = type; + this.ability = ability; + this.crew = []; + } + missionStatement() { //This function returns the mission of the ship if properly crewed and "Can't perform a mission yet." if not + console.log("Begin Mission Statement"); + //console.log("Ship Type: "+(this.type)); + for (let i=0; i=90)&&(num<=100)){ + return 'A'; + }else if((num>=80)&&(num<90)){ + return 'B'; + }else if((num>=75)&&(num<80)){ + return 'C'; + }else if((num>=70)&&(num<75)){ + return 'D'; + }else if(num<70){ + return 'F'; + } } -function filter(arr, callback) { - // Your code here +let grades = [99,87,23,67,13,77]; + +let converted = myMapImpl(grades,gradeConverter); +console.log(converted); + +/* +*This function detects whether the input number is positive +* +* @param num the number to be tested on whether is positive or negative +*/ +let detectPositive = function(num){ + if(num>0){ + return true; + } } -function some(arr, callback) { - // Your code here +/* +*This function acts as the higher order function "some," returning true if at least one element in the input array fulfills the callback's requirements +* +* @param arr the input array +* @param callback the function being implemented on the input array +*/ +function mySomeImpl(arr, callback){ + for (let i=0; i { it('should call the callback the array.length number of times', () => { let count = 0; - forEach([1, 2, 3], () => { + myForEachImpl([1, 2, 3], () => { count++; }); assert.equal(count, 3); @@ -36,7 +177,7 @@ if (typeof describe === 'function') { describe('#map()', () => { const arr = [1, 2, 3]; - const mapped = map(arr, (num) => { + const mapped = myMapImpl(arr, (num) => { return num * num; }); it('should return new array with mapped items', () => { @@ -49,7 +190,7 @@ if (typeof describe === 'function') { describe('#filter()', () => { it('should return an array of items that pass the predicate test', () => { - const filtered = filter([1, 2, 3], (num) => { + const filtered = myFilterImpl([1, 2, 3], (num) => { return num % 2 === 0; }); assert.deepEqual(filtered, [2]); @@ -58,7 +199,7 @@ if (typeof describe === 'function') { describe('#some()', () => { let count = 0; - const somed = some([1, 2, 3, 4], (num) => { + const somed = mySomeImpl([1, 2, 3, 4], (num) => { count++; return num % 2 === 0; }); @@ -69,7 +210,7 @@ if (typeof describe === 'function') { assert.equal(count, 2); }); it('should return false if no items pass the predicate test', () => { - const somed = some([1, 3, 5], (num) => { + const somed = mySomeImpl([1, 3, 5], (num) => { return num % 2 === 0; }); assert.equal(somed, false); @@ -78,13 +219,13 @@ if (typeof describe === 'function') { describe('#every()', () => { it('should return true if at all passes the predicate test', () => { - const everied = every([2, 4, 6], (num) => { + const everied = myEveryImpl([2, 4, 6], (num) => { return num % 2 === 0; }); assert.equal(everied, true); }); let count = 0; - const everied = every([2, 3, 4, 5], (num) => { + const everied = myEveryImpl([2, 3, 4, 5], (num) => { count++; return num % 2 === 0; }); diff --git a/Checkpoints/checkpoint2.js b/Checkpoints/checkpoint2.js new file mode 100644 index 000000000..b5548aa43 --- /dev/null +++ b/Checkpoints/checkpoint2.js @@ -0,0 +1,51 @@ +'use strict'; + +let printStuff = function(x){ + console.log(x.customer.customerName+" paid $"+x.customer.productPrice+" for "+x.customer.product+" in "+x.customer.customerCity+", "+x.customer.customerState+"."); +}; + + +let userArray = [ + { + "customer": { + "id": 1, + "customerName":"Marilyn Monroe", + "customerCity":"New York City", + "customerState":"NY", + "product":"Yellow Chair", + "productPrice": 19.99 + } + }, + { + "customer": { + "id": 2, + "customerName":"Abraham Lincoln", + "customerCity":"Boston", + "customerState":"MA", + "product":"Movie Tickets", + "productPrice": 27.00 + } + }, + { + "customer": { + "id": 3, + "customerName":"John F. Kennedy", + "customerCity":"Dallas", + "customerState":"TX", + "product":"Mustang Convertible", + "productPrice": 24999.99 + } + }, + { + "customer": { + "id": 4, + "customerName":"Martin Luther King", + "customerCity":"Burmingham", + "customerState":"AL", + "product":"Sandwiches", + "productPrice": 7.99 + } + }, +]; + +const map1 = userArray.map(printStuff);