From 7d96c79f762bb07fc51a8e1324e6049de2e38e7d Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Wed, 1 Nov 2017 18:17:15 -0500 Subject: [PATCH 1/7] Space file started --- 05week/spaceTravelToMars.js | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/05week/spaceTravelToMars.js b/05week/spaceTravelToMars.js index ce258a382..92592796d 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,53 @@ 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() { + console.log("Begin Mission Statement"); + //console.log("Ship Type: "+(this.type)); + for (let i=0; i Date: Fri, 3 Nov 2017 20:49:22 -0500 Subject: [PATCH 2/7] Space Update --- 05week/spaceTravelToMars.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/05week/spaceTravelToMars.js b/05week/spaceTravelToMars.js index 92592796d..93b31f2c3 100644 --- a/05week/spaceTravelToMars.js +++ b/05week/spaceTravelToMars.js @@ -43,8 +43,10 @@ class Ship { } if (good2go==true){ return this.ability; + console.log(this.ability); }else{ return "Can't perform a mission yet." + console.log("Can't perform a mission yet."); } } // returns the ship's ability as a string if there is a crewmember whose job matches the ship, otherwise should return "Can't perform a mission yet." } From fa33e5d8426e1089d438ff3043200b77496201ae Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Fri, 3 Nov 2017 20:58:26 -0500 Subject: [PATCH 3/7] Space HW Finished --- 05week/spaceTravelToMars.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/05week/spaceTravelToMars.js b/05week/spaceTravelToMars.js index 93b31f2c3..ef861aca7 100644 --- a/05week/spaceTravelToMars.js +++ b/05week/spaceTravelToMars.js @@ -31,7 +31,7 @@ class Ship { this.ability = ability; this.crew = []; } - missionStatement() { + 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 Date: Wed, 8 Nov 2017 18:02:43 -0600 Subject: [PATCH 4/7] Checkers but thrown errors fail tests --- 05week/checkers.js | 117 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 3 deletions(-) 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); From dbc1996a5708952cb631d79d9147f4a4a3b31b3e Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Wed, 15 Nov 2017 15:39:44 -0600 Subject: [PATCH 5/7] Higher Order HW complete --- 06week/higherOrder.js | 175 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 158 insertions(+), 17 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..1c3e4a535 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -2,32 +2,173 @@ const assert = require('assert'); -function forEach(arr, callback) { - // Your code here +let array = [1,2,3,4,5]; + +/* +*This function returns true if the input is even +* +* @param num the input number +*/ +let evenFilter = function(num){ + return num%2 === 0; +}; + +/* +*This function returns true if the input is odd +* +* @param num the input number +*/ +let oddFilter = function(num){ + return !evenFilter(num); +} + +let expected = [2,4]; + +/* +*This function acts as the higher order function "filter," returning an array of values from the input array that satisfy the callback function +* +* @param arr the input array +* @param callback the function being implemented on the input array +*/ +function myFilterImpl(arr, callback){ + let blank = []; + 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; }); From 3109d5d8edcd94130b9f2ebbf39fc4668e21dc28 Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Sat, 25 Nov 2017 22:08:57 -0600 Subject: [PATCH 6/7] Checkpoint2 --- Checkpoints/checkpoint2.js | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Checkpoints/checkpoint2.js 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); From 751ecb79d568322484fc54ef8a4a8ac55ebfc1ac Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Mon, 27 Nov 2017 16:38:42 -0600 Subject: [PATCH 7/7] TicTacToe --- 07week/ticTacToe/script.js | 124 ++++++++++++++++++++++++++++++++++--- 1 file changed, 115 insertions(+), 9 deletions(-) diff --git a/07week/ticTacToe/script.js b/07week/ticTacToe/script.js index d0bc4bd52..8355e32a1 100644 --- a/07week/ticTacToe/script.js +++ b/07week/ticTacToe/script.js @@ -3,25 +3,131 @@ class TicTacToe extends React.Component { constructor(props) { super(props); + this.state = { + playerTurn: "X", + board: [null,null,null,null,null,null,null,null,null] + } + this.placeSymbol = this.placeSymbol.bind(this); + } + + +/* +*This function puts symbols on the Board +* +*@param event The object being clicked on +*/ + placeSymbol(event) { + console.log("The event object: ", event); + console.log("The element clicked: ", event.target); + console.log("The value is: ", event.target.getAttribute("data-cell")); + +/* +*This function checks for a horizontal win +* +*@param x The component to check for a win on +*/ + function horizontalWin(x) { + if (((x.state.board[0]!=null)&&((x.state.board[0]===x.state.board[1])&&(x.state.board[0]===x.state.board[2]))) || //top row + ((x.state.board[3]!=null)&&((x.state.board[3]===x.state.board[4])&&(x.state.board[3]===x.state.board[5]))) || //middle row + ((x.state.board[6]!=null)&&((x.state.board[6]===x.state.board[7])&&(x.state.board[6]===x.state.board[8])))){ //bottom row + return true + } + } + + +/* +*This function checks for a vertical win +* +*@param x The component to check for a win on +*/ + function verticalWin(x) { + if (((x.state.board[0]!=null)&&((x.state.board[0]===x.state.board[3])&&(x.state.board[0]===x.state.board[6]))) || //left column + ((x.state.board[1]!=null)&&((x.state.board[1]===x.state.board[4])&&(x.state.board[1]===x.state.board[7]))) || //middle column + ((x.state.board[2]!=null)&&((x.state.board[2]===x.state.board[5])&&(x.state.board[2]===x.state.board[8])))){ //right column + return true + } + } + +/* +*This function checks for a diagonal win +* +*@param x The component to check for a win on +*/ + function diagonalWin(x) { + if (((x.state.board[0]!=null)&&((x.state.board[0]===x.state.board[4])&&(x.state.board[0]===x.state.board[8]))) || + ((x.state.board[6]!=null)&&((x.state.board[6]===x.state.board[4])&&(x.state.board[6]===x.state.board[2])))){ + return true + } + } + +/* +*This function checks for a win on the Board +* +*@param y The component to check for a win on +*/ + function checkForWin(y) { + if (horizontalWin(y)||verticalWin(y)||diagonalWin(y)){ + let myFunction = function(){ + alert("Player "+y.state.playerTurn+" Wins!"); + } + setTimeout(myFunction,200); //this is so that the winning symbol shows up on the board before the alert + //y.state.board = [null,null,null,null,null,null,null,null,null]; Couldn't get this to easily reset the board after a win + return true; + } + } + + + let task = event.target.getAttribute("data-cell"); + + if ((this.state.playerTurn==="X")&&(this.state.board[task]===null)){ + console.log("Orig Board: "+this.state.board); + this.state.board[task] = "X"; + let newBoard = this.state.board; + const newState = { + board: newBoard + } + console.log("Task: "+task); + console.log("Board: "+this.state.board); + this.setState(newState); + checkForWin(this); + if (!((horizontalWin(this)||verticalWin(this)||diagonalWin(this)))) { //if no wins + this.state.playerTurn="O"; + } + + }else if ((this.state.playerTurn==="O")&&(this.state.board[task]===null)){ + this.state.board[task] = "O"; + let newBoard = this.state.board; + const newState = { + board: newBoard + } + console.log("Task: "+task); + console.log("Board: "+this.state.board); + this.setState(newState); + checkForWin(this); + if (!((horizontalWin(this)||verticalWin(this)||diagonalWin(this)))) { //if no wins + this.state.playerTurn="X"; + } + } + } render() { return (
-
-
-
+
{this.state.board[0]}
+
{this.state.board[1]}
+
{this.state.board[2]}
-
-
-
+
{this.state.board[3]}
+
{this.state.board[4]}
+
{this.state.board[5]}
-
-
-
+
{this.state.board[6]}
+
{this.state.board[7]}
+
{this.state.board[8]}
);