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
113 changes: 104 additions & 9 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

// WHITEBOARDING:
// game - 3 columns / rows. Start with an array of 4 numbers in the first column / row.
// win - Move all 4 numbers into the last (or second) column / row:
// conditions -
// You can only move one number at a time
// You can only move the top number of any column / row
// You can only stack numbers on top of larger numbers

const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
Expand All @@ -13,34 +21,86 @@ let stacks = {
c: []
};

let moveCount = 0;

function printStacks() {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
}

function movePiece() {
// Your code here

function movePiece(startStack, endStack) {
// You can only move the top number (pop method) of any stack, and you can only move one number at a time, and you move the number the another stack with the push method.
// const userObj = stack[].pop();
if (isValid(startStack, endStack) && isLegal(startStack, endStack)) {
stacks[endStack].push(stacks[startStack].pop());
moveCount++;
} else {
console.log("Please try again!");
return false;
}
}

function isLegal() {
// Your code here
function isValid(startStack, endStack) {
if ((startStack === "a" || startStack === "b" || startStack === "c") && (endStack === "a" || endStack === "b" || endStack === "c")) {
return true;
} else {
console.log("This is an invalid entry, please try again!");
return false;
}
}

// function isLegal(startStack, endStack) {
// You can only move numbers to either empty rows, or on top of a larger numbers. if the last item of the array that you are moving from is less than the last item of the array you are moving to.
// You can only move the numbers that are part of the game, in this case 1 - 4.
// if (stacks[endStack].length === 0) {
// return true;
// } else if (stacks[startStack].pop() < stacks[endStack].pop()){ This is my code and I know it is what is messing up my game play but I do not understand why. Below is something I got from someone else
// } else if (stacks[startStack][(stacks[startStack].length) - 1] < stacks[endStack][(stacks[endStack].length) - 1]) {
// return true;
// } else if (stacks[startStack].length === 0) {
// console.log("This stack is empty, please choose another!");
// return false;
// } else {
// console.log("That is an illegal move, please try again!");
// return false;
// }
// }

function isLegal(startStack, endStack) {
if (stacks[startStack].length === 0) {
console.log("This stack is empty, please choose another!");
return false;
} else if (stacks[endStack].length === 0) {
return true;
} else if (stacks[startStack][(stacks[startStack].length) - 1] < stacks[endStack][(stacks[endStack].length) - 1]) {
return true;
} else {
console.log("That is an illegal move, please try again!");
return false;
}
}

function checkForWin() {
// Your code here

// You win the game if you have successfully moved all four numbers into either of the other two rows (in order).
if ((stacks.b.length === 4) || (stacks.c.length === 4)) {
console.log("You Won!");
return true;
// printStacks(); < this will print updated stacks? make new reset function?
} else {
return false;
}
}

function towersOfHanoi(startStack, endStack) {
// Your code here

// if function isLegal then movePiece and then checkWin until there is a win and then end game.
movePiece(startStack, endStack);
checkForWin();
}

function getPrompt() {
printStacks();
console.log("Moves: " + moveCount);
rl.question('start stack: ', (startStack) => {
rl.question('end stack: ', (endStack) => {
towersOfHanoi(startStack, endStack);
Expand All @@ -53,13 +113,39 @@ function getPrompt() {

if (typeof describe === 'function') {

// User can only move objects from stacks a b and c (returns an error message when a user inputs illegal character)

describe('#isValid()', () => {
it('should return error message when user inputs illegal move', () => {
stacks = {
a: [4, 3, 2, 1],
b: [],
c: []
};
assert.equal(isValid('a', 'd'), false);
});
});

describe('#towersOfHanoi()', () => {
it('should be able to move a block', () => {
towersOfHanoi('a', 'b');
assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] });
});
});

// should be able to move a block on top of another block of greater value

describe('#isLegal()', () => {
it('should be able to move a block on top of another block of greater value', () => {
stacks = {
a: [4, 3],
b: [1],
c: [2]
};
assert.equal(isLegal('b', 'c'), true);
});
});

describe('#isLegal()', () => {
it('should not allow an illegal move', () => {
stacks = {
Expand All @@ -86,6 +172,15 @@ if (typeof describe === 'function') {
assert.equal(checkForWin(), false);
});
});

// Should also detect win if all of the numbers are in the c column

describe('#checkForWin()', () => {
it('should also detect a win if all of the numbers are in c column', () => {
stacks = { a: [], b: [], c: [4, 3, 2, 1] };
assert.equal(checkForWin(), true);
});
});
} else {

getPrompt();
Expand Down
41 changes: 40 additions & 1 deletion 05week/spaceTravelToMars.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// https://github.com/deytonk/javascript-workbook/pull/11

let assert = require('assert');

let jobTypes = {
Expand All @@ -9,7 +11,44 @@ let jobTypes = {
programmer: 'Any Ship!'
};

// Your code here
class CrewMember {
constructor(name, job, specialSkill, ship) {
this.name = name;
this.job = job;
this.specialSkill = specialSkill;
this.ship = ship;
}

// Make a method that takes a new CrewMember and assigns it a ship (and adds the new crewMember to the ship's crew).
enterShip(ship) {
this.ship = ship;
ship.crew.push(this);
}
}

class Ship {
constructor(name, type, ability, crew) {
this.name = name;
this.type = type;
this.ability = ability;
this.crew = [];
}

// Make a method that returns the ship's ability ONLY IF a crew member enters
// the ship (enterShip), else it will return the message "Can't perform a mission yet."
missionStatement() {
if (this.crew.length < 1) {
return "Can't perform a mission yet.";
} else {
return this.ability;
}
}
}

// const mav = new Ship('Mars Ascent Vehicle', 'MAV', 'Ascend into low orbit');
// const crewMember1 = new CrewMember('Rick Martinez', 'pilot', 'chemistry');
// const hermes = new Ship('Hermes', 'Main Ship', 'Interplanetary Space Travel');
// const crewMember2 = new CrewMember('Commander Lewis', 'commander', 'geology');

//tests
if (typeof describe === 'function'){
Expand Down
77 changes: 58 additions & 19 deletions 07week/ticTacToe/script.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
'use strict';
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class TicTacToe extends React.Component {
constructor(props) {
super(props);
// Keep track of player turn, default is 'X'
// on click and if div is empty, then place player turn, check for win,
// if no win, switch player turn
state = {
playerTurn: 'X',
grid: {},
board: [
[0,1,2],
[3,4,5],
[6,7,8]
]
}

clickHandler(box) {
if (!this.state[box]) {
const newGrid = {...this.state.grid};
newGrid[box] = this.state.playerTurn;
const playerTurn = this.state.playerTurn === 'X' ? 'O' : 'X';
this.setState({grid: newGrid, playerTurn})
}
}

checkForWin() {

}

renderBoard() {
return this.state.board.map((row, key) =>{
return (
<div className="row">
{row.map(num, index) => {
<div key={index} className="square" onClick = {() => this.clickHandler(num)}>{this.state.grid[num]}</div>
})}
</div>
)
})
}

render() {
return (
<div>
<div className="row">
<div data-cell="0"></div>
<div data-cell="1"></div>
<div data-cell="2"></div>
</div>
<div className="row">
<div data-cell="3"></div>
<div data-cell="4"></div>
<div data-cell="5"></div>
</div>
<div className="row">
<div data-cell="6"></div>
<div data-cell="7"></div>
<div data-cell="8"></div>
</div>
{this.renderBoard()}
</div>
// <div>
// <div className="row">
// <div className="square" onClick = {() => this.clickHandler(0)}>{this.state.grid[0]}</div>
// <div className="square" onClick = {() => this.clickHandler(1)}>{this.state.grid[1]}</div>
// <div className="square" onClick = {() => this.clickHandler(2)}>{this.state.grid[2]}</div>
// </div>
// <div className="row">
// <div className="square" onClick = {() => this.clickHandler(3)}>{this.state.grid[3]}</div>
// <div className="square" onClick = {() => this.clickHandler(4)}>{this.state.grid[4]}</div>
// <div className="square" onClick = {() => this.clickHandler(5)}>{this.state.grid[5]}</div>
// </div>
// <div className="row">
// <div className="square" onClick = {() => this.clickHandler(6)}>{this.state.grid[6]}</div>
// <div className="square" onClick = {() => this.clickHandler(7)}>{this.state.grid[7]}</div>
// <div className="square" onClick = {() => this.clickHandler(8)}>{this.state.grid[8]}</div>
// </div>
// </div>
);
}
}

ReactDOM.render(<TicTacToe />, document.getElementById('tic-tac-toe'));
export default TicTacToe;
21 changes: 21 additions & 0 deletions 07week/tictactoe-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading