Implement double belt logic
This commit is contained in:
parent
904440e21b
commit
93f56a3b27
42
game.js
42
game.js
@ -70,6 +70,12 @@ var LEVEL3 = [
|
|||||||
[{},{...WRITERDOWN},{},{},{},],
|
[{},{...WRITERDOWN},{},{},{},],
|
||||||
[{},{...BELTRIGHT},{},{},{},],
|
[{},{...BELTRIGHT},{},{},{},],
|
||||||
[{},{},{},{},{},],]
|
[{},{},{},{},{},],]
|
||||||
|
var LEVEL4 = [
|
||||||
|
[{},{},{},{},{},],
|
||||||
|
[{},{},{},{},{},],
|
||||||
|
[{},{...BELTRIGHT,direction2:DOWN},{},{},{},],
|
||||||
|
[{},{},{...BELTUP,direction2:LEFT},{},{},],
|
||||||
|
[{},{},{},{},{},],]
|
||||||
|
|
||||||
var gameGrid= []
|
var gameGrid= []
|
||||||
|
|
||||||
@ -92,10 +98,26 @@ function runBot(testing) {
|
|||||||
//console.log("Update")
|
//console.log("Update")
|
||||||
var nextSquare = {}
|
var nextSquare = {}
|
||||||
switch (BOT_DIR) {
|
switch (BOT_DIR) {
|
||||||
case UP:{nextSquare = gameGrid[--BOT_Y][BOT_X];}break;
|
case UP:{
|
||||||
case LEFT:{nextSquare = gameGrid[BOT_Y][--BOT_X];}break;
|
nextSquare = gameGrid[--BOT_Y][BOT_X];
|
||||||
case RIGHT:{nextSquare = gameGrid[BOT_Y][++BOT_X];}break;
|
BOT_DIR=(nextSquare.direction2 &&
|
||||||
case DOWN:{nextSquare = gameGrid[++BOT_Y][BOT_X];}break;
|
(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) {
|
if (nextSquare.direction!==undefined) {
|
||||||
switch (nextSquare.type) {
|
switch (nextSquare.type) {
|
||||||
@ -115,12 +137,13 @@ function runBot(testing) {
|
|||||||
}
|
}
|
||||||
}break;
|
}break;
|
||||||
case "WRITER":{
|
case "WRITER":{
|
||||||
AppendTape(nextSquare.color)
|
if (nextSquare.overwrite) {
|
||||||
|
OverwriteTape(nextSquare.color)
|
||||||
|
} else {
|
||||||
|
AppendTape(nextSquare.color)
|
||||||
|
}
|
||||||
BOT_DIR = nextSquare.direction
|
BOT_DIR = nextSquare.direction
|
||||||
}break;
|
}break;
|
||||||
default:{
|
|
||||||
BOT_DIR = nextSquare.direction
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//console.log("Direction is now "+BOT_DIR)
|
//console.log("Direction is now "+BOT_DIR)
|
||||||
} else {
|
} else {
|
||||||
@ -191,6 +214,9 @@ function ConsumeTape() {
|
|||||||
function AppendTape(col) {
|
function AppendTape(col) {
|
||||||
BOT_TAPE.push({color:col})
|
BOT_TAPE.push({color:col})
|
||||||
}
|
}
|
||||||
|
function OverwriteTape(col) {
|
||||||
|
BOT_TAPE[0]={color:col}
|
||||||
|
}
|
||||||
|
|
||||||
function LeftOf(dir) {
|
function LeftOf(dir) {
|
||||||
return (dir+1)%4
|
return (dir+1)%4
|
||||||
|
71
game.test.js
71
game.test.js
@ -170,6 +170,77 @@ function runTests() {
|
|||||||
}
|
}
|
||||||
}(),true)
|
}(),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("==============")
|
console.log("==============")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user