Implement a second level w/more logic

master
sigonasr2 4 years ago
parent c5b310cdd5
commit fa21e1af27
  1. 19
      game.js
  2. 98
      game.test.js

@ -35,7 +35,7 @@ var BELTDOWN = {type:"BELT",direction:DOWN/*,direction2 - defines a secondary di
var BELTRIGHT = {type:"BELT",direction:RIGHT} var BELTRIGHT = {type:"BELT",direction:RIGHT}
var BELTUP = {type:"BELT",direction:UP} var BELTUP = {type:"BELT",direction:UP}
var BELTLEFT = {type:"BELT",direction:LEFT} var BELTLEFT = {type:"BELT",direction:LEFT}
var BRANCHDOWN = {type:"BRANCH",direction:DOWN,color1:RED,color2:BLUE} //color 1 points left, color 2 points right var BRANCHDOWN = {type:"BRANCH",direction:DOWN,color1:RED,color2:BLUE} //color 1 points clockwise(right), color 2 points counter-clockwise(left)
var BRANCHLEFT = {type:"BRANCH",direction:LEFT,color1:RED,color2:BLUE} var BRANCHLEFT = {type:"BRANCH",direction:LEFT,color1:RED,color2:BLUE}
var BRANCHRIGHT = {type:"BRANCH",direction:RIGHT,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 BRANCHUP = {type:"BRANCH",direction:UP,color1:RED,color2:BLUE}
@ -87,6 +87,20 @@ var STAGE1 = {
level:createGrid(5,5,4,2), level:createGrid(5,5,4,2),
start:{x:0,y:2}, start:{x:0,y:2},
accept:(tape)=>true} accept:(tape)=>true}
var STAGE2 = {
name:"Blue Blue",
objective:"Accept only Blue Bots",
level:createGrid(5,5,4,2),
start:{x:0,y:2},
accept:(tape)=>{
for (var i=0;i<tape.length;i++) {
if (tape[i]!=="B") {
return false;
}
}
return true;
}
}
var gameGrid= [] var gameGrid= []
@ -249,7 +263,7 @@ function resetBot(x,y,state,tape) {
gameState=state gameState=state
BOT_STATE = ALIVE BOT_STATE = ALIVE
BOT_DIR = RIGHT BOT_DIR = RIGHT
BOT_TAPE=deepCopy(tape) BOT_TAPE=tape
placeBot(x,y) placeBot(x,y)
} }
@ -301,6 +315,7 @@ function renderGame() {
if (BOT_X!==-1&&BOT_Y!==-1) { if (BOT_X!==-1&&BOT_Y!==-1) {
displayGrid[BOT_Y][BOT_X]["BOT"]=true displayGrid[BOT_Y][BOT_X]["BOT"]=true
} }
console.log("Tape: "+BOT_TAPE)
console.log(displayGrid) console.log(displayGrid)
} }

@ -355,6 +355,104 @@ function runTests() {
}) })
.showResults() .showResults()
TestSuite = new describe("Stage 2")
TestSuite
.beforeEach(()=>{
resetGame()
})
.it("Stage 2 has a level",()=>{
expect(STAGE2.level===undefined,false,"Is defined")
expect(Array.isArray(STAGE2.level),true,"Is an array")
expect(STAGE2.level.length>0,true,"Cannot be empty")
})
.it("Stage 2 has a name",()=>{
expect(STAGE2.name===undefined,false,"Is defined")
expect(typeof(STAGE2.name),"string","Is a string")
expect(STAGE2.name.length>0,true,"Cannot be blank")
})
.it("Stage 2 has an objective",()=>{
expect(STAGE2.objective===undefined,false,"Is defined")
expect(typeof(STAGE2.objective),"string","Is a string")
expect(STAGE2.objective.length>0,true,"Cannot be blank")
})
.it("Stage 2 has a starting position",()=>{
expect(STAGE2.start===undefined,false,"Is defined")
expect(typeof(STAGE2.start),"object","Is an object")
expect(STAGE2.start.x===undefined,false,"Must have an X coordinate")
expect(STAGE2.start.y===undefined,false,"Must have a Y coordinate")
})
.it("Stage 2 has an acceptance condition",()=>{
expect(STAGE2.accept===undefined,false)
expect(typeof(STAGE2.accept),"function")
})
.it("loadStage sets up Stage 2",()=>{
loadStage(STAGE2)
expect(gameGrid.length===STAGE2.level.length,true,"Height of stage is equal")
expect(gameGrid[0].length===STAGE2.level[0].length,true,"Width of stage is equal")
})
.it("current stage set to Stage 2",()=>{
loadStage(STAGE2)
expect(gameStage===STAGE2,true)
})
.it("accept only blue bots for Stage 2.",()=>{
loadStage(STAGE2)
expect(gameStage.accept("BBB"),true,"Expect true for BBB")
expect(gameStage.accept("RB"),false,"Expect false for RB")
expect(gameStage.accept("BRBR"),false,"Expect false for BRBR")
expect(gameStage.accept("BBBBBBBBBBBB"),true,"Expect true for BBBBBBBBBBBB")
})
.it("bot fails at the start.",()=>{
loadStage(STAGE2)
runBot(true)
expect(gameState===REVIEWING,true)
})
.it("When TESTING state is on, the game should test the current level for cases expecting to pass, but fail and create 3 of them if possible.",()=>{
loadStage(STAGE2)
expect(BOT_QUEUE.length===0,true,"Bot queue should be empty.")
generateBotQueue()
expect(BOT_QUEUE.length===0&&gameState!==TESTING,true,"Bot queue should not be modified while state is not TESTING")
gameState=TESTING
generateBotQueue()
expect(BOT_QUEUE.length===3,true,"There should be 3 bots in queue for an unbuilt level, as there are cases that do not pass.")
})
.it("A stage should have an Exit.",()=>{
loadStage(STAGE2)
var hasAnExit=()=>{
for (var y=0;y<gameGrid.length;y++) {
for (var x=0;x<gameGrid[y].length;x++) {
if (gameGrid[y][x].type==="EXIT") {
return true;
}
}
}
return false;
}
expect(hasAnExit(),true)
})
.it("A bot reaching the exit should set the state to FINISH.",()=>{
loadStage(STAGE2)
placeBot(3,2)
runBot(true)
expect(gameState===FINISH,true)
})
.it("Run a TESTING state to see if an acceptable player-built level has no bots in queue.",()=>{
loadStage(STAGE2)
gameGrid=[
[{},{},{},{},{},],
[{},{...BELTDOWN},{},{},{},],
[{},{...BRANCHRIGHT},{...BELTRIGHT},{...BELTRIGHT},{type:"EXIT"},],
[{},{},{},{},{},],
[{},{},{},{},{},],
]
expect(BOT_QUEUE.length===0,true,"Bot queue should be empty.")
gameState=TESTING
generateBotQueue()
//console.log(BOT_QUEUE)
expect(BOT_QUEUE.length===0,true,"There should be 0 bots in queue for a good level, as all bots are supposed to pass.")
})
.showResults()
if (testsPass===undefined) { if (testsPass===undefined) {
testsPass=true testsPass=true
} }

Loading…
Cancel
Save