Implement double belt logic
This commit is contained in:
parent
904440e21b
commit
93f56a3b27
40
game.js
40
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":{
|
||||
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
|
||||
|
71
game.test.js
71
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("==============")
|
||||
|
Loading…
x
Reference in New Issue
Block a user