diff --git a/game.js b/game.js index c4cc7d8..b7997a3 100644 --- a/game.js +++ b/game.js @@ -35,10 +35,10 @@ var BRANCHDOWN = {type:"BRANCH",direction:DOWN,color1:RED,color2:BLUE} //color 1 var BRANCHLEFT = {type:"BRANCH",direction:LEFT,color1:RED,color2:BLUE} var BRANCHRIGHT = {type:"BRANCH",direction:RIGHT,color1:RED,color2:BLUE} var BRANCHUP = {type:"BRANCH",direction:UP,color1:RED,color2:BLUE} -var WRITERDOWN = {type:"WRITER",direction:DOWN,color1:RED,color2:BLUE} //color 1 points left, color 2 points right -var WRITERLEFT = {type:"WRITER",direction:LEFT,color1:RED,color2:BLUE} -var WRITERRIGHT = {type:"WRITER",direction:RIGHT,color1:RED,color2:BLUE} -var WRITERUP = {type:"WRITER",direction:UP,color1:RED,color2:BLUE} +var WRITERDOWN = {type:"WRITER",direction:DOWN,color:RED/*overwrite - if turned on, the writer overwrites the current tape position instead of appending.*/} +var WRITERLEFT = {type:"WRITER",direction:LEFT,color:RED} +var WRITERRIGHT = {type:"WRITER",direction:RIGHT,color:RED} +var WRITERUP = {type:"WRITER",direction:UP,color:RED} var lastGameUpdate = 0; @@ -64,6 +64,12 @@ var LEVEL2 = [ [{},{...BRANCHRIGHT},{},{},{},], [{},{...BELTRIGHT},{},{},{},], //RED [{},{},{},{},{},],] +var LEVEL3 = [ + [{},{},{},{},{},], + [{},{...WRITERUP,overwrite:true},{},{},{},], + [{},{...WRITERDOWN},{},{},{},], + [{},{...BELTRIGHT},{},{},{},], + [{},{},{},{},{},],] var gameGrid= [] @@ -92,23 +98,29 @@ function runBot(testing) { case DOWN:{nextSquare = gameGrid[++BOT_Y][BOT_X];}break; } if (nextSquare.direction!==undefined) { - - if (nextSquare.type==="BRANCH") { - //console.log("Branch found") - if (BOT_TAPE[0].color===nextSquare.color1) { - //console.log("Matches color1") - //Move towards left side of the branch. - BOT_DIR = LeftOf(nextSquare.direction) - ConsumeTape() - } else - if (BOT_TAPE[0].color===nextSquare.color2) { - //console.log("Matches color2") - //Move towards left side of the branch. - BOT_DIR = RightOf(nextSquare.direction) - ConsumeTape() + switch (nextSquare.type) { + case "BRANCH":{ + //console.log("Branch found") + if (BOT_TAPE[0].color===nextSquare.color1) { + //console.log("Matches color1") + //Move towards left side of the branch. + BOT_DIR = LeftOf(nextSquare.direction) + ConsumeTape() + } else + if (BOT_TAPE[0].color===nextSquare.color2) { + //console.log("Matches color2") + //Move towards left side of the branch. + BOT_DIR = RightOf(nextSquare.direction) + ConsumeTape() + } + }break; + case "WRITER":{ + AppendTape(nextSquare.color) + BOT_DIR = nextSquare.direction + }break; + default:{ + BOT_DIR = nextSquare.direction } - } else { - BOT_DIR = nextSquare.direction } //console.log("Direction is now "+BOT_DIR) } else { @@ -176,6 +188,9 @@ function draw() { function ConsumeTape() { BOT_TAPE.shift() } +function AppendTape(col) { + BOT_TAPE.push({color:col}) +} function LeftOf(dir) { return (dir+1)%4 diff --git a/game.test.js b/game.test.js index eb9ba12..fd78cef 100644 --- a/game.test.js +++ b/game.test.js @@ -10,12 +10,16 @@ function loadScript(url, callback) } var testsPass=undefined; +var TestSuite; class describe { constructor(testname) { this.testname=testname this.beforecb = undefined; this.cb = undefined; + this.totaltests = 0; + this.passedtests = 0; + this.starttime = 0; console.log(this.testname) } @@ -25,6 +29,7 @@ class describe { } it = (checkname,cb)=>{ + this.starttime = new Date().getTime() console.log("->"+checkname) this.beforecb() this.cb=cb; @@ -35,11 +40,14 @@ class describe { function expect(testval1,testval2,test) { if (testval1!==testval2) { - console.log(" Test Failed!") + console.log(" Test Failed!"+" ("+(new Date().getTime()-TestSuite.starttime)+"ms)") + TestSuite.totaltests++ testsPass=false } else { - console.log(" Test Passed!") + TestSuite.totaltests++ + TestSuite.passedtests++ + console.log(" Test Passed!"+" ("+(new Date().getTime()-TestSuite.starttime)+"ms)") } } @@ -56,7 +64,8 @@ function AllBlankSpaces(level) { function runTests() { console.log("Running test suite...") - new describe("Bot moving") + TestSuite = new describe("Bot moving") + TestSuite .beforeEach(()=>{ gameGrid=[] gameState=WAITING @@ -68,12 +77,9 @@ function runTests() { lastGameUpdate=0 }) .it("Blank level exists.",()=>{ - var starttime = new Date().getTime() expect(AllBlankSpaces(LEVEL0),true) - console.log(" ("+(new Date().getTime()-starttime)+"ms)") }) .it("Bot moves to the right initially.",()=>{ - var starttime = new Date().getTime() expect(function(){ gameGrid=createGrid(5,5) placeBot(0,2) @@ -84,10 +90,8 @@ function runTests() { return false } }(),true) - console.log(" ("+(new Date().getTime()-starttime)+"ms)") }) .it("Bot obeys conveyor belt rules",()=>{ - var starttime = new Date().getTime() expect(function(){ loadLevel(LEVEL1,0,2) for (var i=0;i<11;i++) {runBot(true)} @@ -97,10 +101,8 @@ function runTests() { return false } }(),true) - console.log(" ("+(new Date().getTime()-starttime)+"ms)") }) .it("Bot obeys branch rules",()=>{ - var starttime = new Date().getTime() expect(function(){ loadLevel(LEVEL2,0,2) for (var i=0;i<3;i++) {runBot(true)} @@ -110,10 +112,8 @@ function runTests() { return false } }(),true) - console.log(" ("+(new Date().getTime()-starttime)+"ms)") }) .it("Bot obeys branch rules with different colored tape.",()=>{ - var starttime = new Date().getTime() expect(function(){ loadLevel(LEVEL2,0,2) BOT_TAPE = [{color:BLUE}] @@ -124,9 +124,57 @@ function runTests() { return false } }(),true) - console.log(" ("+(new Date().getTime()-starttime)+"ms)") }) + .it("Bot tape is reduced by 1 when passing through a branch.",()=>{ + expect(function(){ + loadLevel(LEVEL2,0,2) + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_TAPE.length===1&&BOT_TAPE[0].color===BLUE) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot tape is reduced by 1 when passing through a different branch.",()=>{ + expect(function(){ + loadLevel(LEVEL2,0,2) + BOT_TAPE = [{color:BLUE}] + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_TAPE.length===0) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot obeys writer movement rules",()=>{ + expect(function(){ + loadLevel(LEVEL3,0,2) + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===3) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot obeys writer tape rules - Has correct tape when appending",()=>{ + expect(function(){ + loadLevel(LEVEL3,0,2) + for (var i=0;i<3;i++) {runBot(true)} + if (BOT_TAPE.length===3&&BOT_TAPE[2].color===RED) { + return true + } else { + return false + } + }(),true) + }) + + console.log("==============") + console.log("TEST RESULTS: "+TestSuite.passedtests+" passed, "+(TestSuite.totaltests-TestSuite.passedtests)+" failed, "+TestSuite.totaltests+" total") + console.log("==============") if (testsPass===undefined) { testsPass=true }