From 93f56a3b27b8b66da3538dde0dc3b7cbaa89beac Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 29 Aug 2020 11:49:28 +0900 Subject: [PATCH] Implement double belt logic --- game.js | 42 +++++++++++++++++++++++++------ game.test.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/game.js b/game.js index b7997a3..05194fa 100644 --- a/game.js +++ b/game.js @@ -70,6 +70,12 @@ var LEVEL3 = [ [{},{...WRITERDOWN},{},{},{},], [{},{...BELTRIGHT},{},{},{},], [{},{},{},{},{},],] +var LEVEL4 = [ + [{},{},{},{},{},], + [{},{},{},{},{},], + [{},{...BELTRIGHT,direction2:DOWN},{},{},{},], + [{},{},{...BELTUP,direction2:LEFT},{},{},], + [{},{},{},{},{},],] var gameGrid= [] @@ -92,10 +98,26 @@ function runBot(testing) { //console.log("Update") var nextSquare = {} switch (BOT_DIR) { - case UP:{nextSquare = gameGrid[--BOT_Y][BOT_X];}break; - case LEFT:{nextSquare = gameGrid[BOT_Y][--BOT_X];}break; - case RIGHT:{nextSquare = gameGrid[BOT_Y][++BOT_X];}break; - case DOWN:{nextSquare = gameGrid[++BOT_Y][BOT_X];}break; + case UP:{ + nextSquare = gameGrid[--BOT_Y][BOT_X]; + BOT_DIR=(nextSquare.direction2 && + (nextSquare.direction2===UP||nextSquare.direction2===DOWN))?nextSquare.direction2:nextSquare.direction + }break; + case LEFT:{ + nextSquare = gameGrid[BOT_Y][--BOT_X]; + BOT_DIR=(nextSquare.direction2 && + (nextSquare.direction2===RIGHT||nextSquare.direction2===LEFT))?nextSquare.direction2:nextSquare.direction + }break; + case RIGHT:{ + nextSquare = gameGrid[BOT_Y][++BOT_X]; + BOT_DIR=(nextSquare.direction2 && + (nextSquare.direction2===RIGHT||nextSquare.direction2===LEFT))?nextSquare.direction2:nextSquare.direction + }break; + case DOWN:{ + nextSquare = gameGrid[++BOT_Y][BOT_X]; + BOT_DIR=(nextSquare.direction2 && + (nextSquare.direction2===UP||nextSquare.direction2===DOWN))?nextSquare.direction2:nextSquare.direction + }break; } if (nextSquare.direction!==undefined) { switch (nextSquare.type) { @@ -115,12 +137,13 @@ function runBot(testing) { } }break; case "WRITER":{ - AppendTape(nextSquare.color) + if (nextSquare.overwrite) { + OverwriteTape(nextSquare.color) + } else { + AppendTape(nextSquare.color) + } BOT_DIR = nextSquare.direction }break; - default:{ - BOT_DIR = nextSquare.direction - } } //console.log("Direction is now "+BOT_DIR) } else { @@ -191,6 +214,9 @@ function ConsumeTape() { function AppendTape(col) { BOT_TAPE.push({color:col}) } +function OverwriteTape(col) { + BOT_TAPE[0]={color:col} +} function LeftOf(dir) { return (dir+1)%4 diff --git a/game.test.js b/game.test.js index fd78cef..8f7b3c3 100644 --- a/game.test.js +++ b/game.test.js @@ -170,6 +170,77 @@ function runTests() { } }(),true) }) + .it("Bot obeys writer tape rules - Has correct tape when overwriting",()=>{ + expect(function(){ + loadLevel(LEVEL3,0,1) + BOT_TAPE=[{color:BLUE},{color:RED}] + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_TAPE.length===2&&BOT_TAPE[0].color===RED) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot goes forward if no color matched.",()=>{ + expect(function(){ + loadLevel(LEVEL2,0,2) + BOT_TAPE = [{color:YELLOW}] + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===2) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot goes right when approaching a double belt (Left->Right) from the left",()=>{ + expect(function(){ + loadLevel(LEVEL4,0,2) + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===2) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot goes down when approaching a double belt (Up->Down) from the top",()=>{ + expect(function(){ + loadLevel(LEVEL4,1,1) + BOT_DIR=DOWN + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_X===1&&BOT_Y===3) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot goes right when approaching a double belt (Left->Right) from the Right",()=>{ + expect(function(){ + loadLevel(LEVEL4,2,2) + BOT_DIR=LEFT + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===2) { + return true + } else { + return false + } + }(),true) + }) + .it("Bot goes up when approaching a double belt (Down->Up) from the Bottom",()=>{ + expect(function(){ + loadLevel(LEVEL4,2,4) + BOT_DIR=UP + for (var i=0;i<2;i++) {runBot(true)} + if (BOT_X===2&&BOT_Y===2) { + return true + } else { + return false + } + }(),true) + }) console.log("==============")