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
117 changes: 114 additions & 3 deletions 05week/checkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
}
}
};
Expand Down Expand Up @@ -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);
Expand Down
59 changes: 58 additions & 1 deletion 05week/spaceTravelToMars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,71 @@

let assert = require('assert');

let good2go = null;

let jobTypes = {
pilot: 'MAV',
mechanic: 'Repair Ship',
commander: 'Main Ship',
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<this.crew.length; i++){
let crewMemberJob = this.crew[i].job;
console.log("Crew Member Job Type: "+(jobTypes[crewMemberJob]));
if (((jobTypes[crewMemberJob])==(this.type))||(this.crew[i].job=='programmer')){
good2go = true;
}
}
if (good2go==true){
//console.log(this.ability);
good2go=null; //need to reset because good2go is a global variable
return this.ability;
}else{
//console.log("Can't perform a mission yet.");
return "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."
}

/*
*
* Test code to verify everything works. Everything works as of 8:55PM CT 2017-11-3.
*
let TestCrew = new CrewMember('Joe Doe', 'programmer', 'botany');
let TestShip = new Ship('Janus','Repair Ship','Start shoveling bodies.');
TestShip.missionStatement();
TestCrew.enterShip(TestShip);
console.log(TestShip.crew[0]);
let crewMemberJob = TestShip.crew[0].job;
console.log("Crew Member Job Type: "+crewMemberJob);
TestShip.missionStatement();
*/



//tests
if (typeof describe === 'function'){
Expand Down
Loading