Add in testing suite
This commit is contained in:
parent
b3396a365e
commit
476dbfd6df
23
game.js
23
game.js
@ -79,12 +79,11 @@ function createGrid(width,height) {
|
|||||||
return grid
|
return grid
|
||||||
}
|
}
|
||||||
|
|
||||||
function runBot() {
|
function runBot(testing) {
|
||||||
//console.log(new Date().getTime())
|
//console.log(new Date().getTime())
|
||||||
if (lastGameUpdate<new Date().getTime()) {
|
if (lastGameUpdate<new Date().getTime()||testing) {
|
||||||
//console.log("Update")
|
|
||||||
lastGameUpdate=new Date().getTime()+gameSpeed
|
lastGameUpdate=new Date().getTime()+gameSpeed
|
||||||
|
//console.log("Update")
|
||||||
var nextSquare = {}
|
var nextSquare = {}
|
||||||
switch (BOT_DIR) {
|
switch (BOT_DIR) {
|
||||||
case UP:{nextSquare = gameGrid[--BOT_Y][BOT_X];}break;
|
case UP:{nextSquare = gameGrid[--BOT_Y][BOT_X];}break;
|
||||||
@ -97,13 +96,13 @@ function runBot() {
|
|||||||
if (nextSquare.type==="BRANCH") {
|
if (nextSquare.type==="BRANCH") {
|
||||||
//console.log("Branch found")
|
//console.log("Branch found")
|
||||||
if (BOT_TAPE[0].color===nextSquare.color1) {
|
if (BOT_TAPE[0].color===nextSquare.color1) {
|
||||||
console.log("Matches color1")
|
//console.log("Matches color1")
|
||||||
//Move towards left side of the branch.
|
//Move towards left side of the branch.
|
||||||
BOT_DIR = LeftOf(nextSquare.direction)
|
BOT_DIR = LeftOf(nextSquare.direction)
|
||||||
ConsumeTape()
|
ConsumeTape()
|
||||||
} else
|
} else
|
||||||
if (BOT_TAPE[0].color===nextSquare.color2) {
|
if (BOT_TAPE[0].color===nextSquare.color2) {
|
||||||
console.log("Matches color2")
|
//console.log("Matches color2")
|
||||||
//Move towards left side of the branch.
|
//Move towards left side of the branch.
|
||||||
BOT_DIR = RightOf(nextSquare.direction)
|
BOT_DIR = RightOf(nextSquare.direction)
|
||||||
ConsumeTape()
|
ConsumeTape()
|
||||||
@ -111,12 +110,12 @@ function runBot() {
|
|||||||
} else {
|
} else {
|
||||||
BOT_DIR = nextSquare.direction
|
BOT_DIR = nextSquare.direction
|
||||||
}
|
}
|
||||||
console.log("Direction is now "+BOT_DIR)
|
//console.log("Direction is now "+BOT_DIR)
|
||||||
} else {
|
} else {
|
||||||
gameState = REVIEWING
|
gameState = REVIEWING
|
||||||
BOT_STATE = DEAD
|
BOT_STATE = DEAD
|
||||||
}
|
}
|
||||||
renderGame()
|
if (!testing){renderGame()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,11 +184,3 @@ function RightOf(dir) {
|
|||||||
if (dir===0) {dir=4}
|
if (dir===0) {dir=4}
|
||||||
return (dir-1)%4
|
return (dir-1)%4
|
||||||
}
|
}
|
||||||
|
|
||||||
setupGame();
|
|
||||||
loadLevel(LEVEL2,0,2)
|
|
||||||
setInterval(()=>{
|
|
||||||
step()
|
|
||||||
draw()
|
|
||||||
},1000/60)
|
|
||||||
console.log("Running")
|
|
158
game.test.js
Normal file
158
game.test.js
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
function loadScript(url, callback)
|
||||||
|
{
|
||||||
|
var head = document.head;
|
||||||
|
var script = document.createElement('script');
|
||||||
|
script.type = 'text/javascript';
|
||||||
|
script.src = url;
|
||||||
|
script.onreadystatechange = callback;
|
||||||
|
script.onload = callback;
|
||||||
|
head.appendChild(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
var testsPass=undefined;
|
||||||
|
|
||||||
|
class describe {
|
||||||
|
constructor(testname) {
|
||||||
|
this.testname=testname
|
||||||
|
this.beforecb = undefined;
|
||||||
|
this.cb = undefined;
|
||||||
|
console.log(this.testname)
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach = (cb)=>{
|
||||||
|
this.beforecb = cb
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
it = (checkname,cb)=>{
|
||||||
|
console.log("->"+checkname)
|
||||||
|
this.beforecb()
|
||||||
|
this.cb=cb;
|
||||||
|
this.cb()
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function expect(testval1,testval2,test) {
|
||||||
|
if (testval1!==testval2) {
|
||||||
|
console.log(" Test Failed!")
|
||||||
|
testsPass=false
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
console.log(" Test Passed!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function AllBlankSpaces(level) {
|
||||||
|
for (var x=0;x<level.length;x++) {
|
||||||
|
for (var y=0;y<level.length;y++) {
|
||||||
|
if (Object.keys(level[y][x]).length!==0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function runTests() {
|
||||||
|
console.log("Running test suite...")
|
||||||
|
new describe("Bot moving")
|
||||||
|
.beforeEach(()=>{
|
||||||
|
gameGrid=[]
|
||||||
|
gameState=WAITING
|
||||||
|
BOT_X=-1
|
||||||
|
BOT_Y=-1
|
||||||
|
BOT_DIR=RIGHT
|
||||||
|
BOT_STATE=ALIVE
|
||||||
|
BOT_TAPE=[{color:RED},{color:BLUE}]
|
||||||
|
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)
|
||||||
|
runBot(true)
|
||||||
|
if (BOT_X===1&&BOT_Y===2) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
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)}
|
||||||
|
if (BOT_X===2&&BOT_Y===1) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
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)}
|
||||||
|
if (BOT_X===2&&BOT_Y===3) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
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}]
|
||||||
|
for (var i=0;i<3;i++) {runBot(true)}
|
||||||
|
if (BOT_X===2&&BOT_Y===1) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}(),true)
|
||||||
|
console.log(" ("+(new Date().getTime()-starttime)+"ms)")
|
||||||
|
})
|
||||||
|
|
||||||
|
if (testsPass===undefined) {
|
||||||
|
testsPass=true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function runGame() {
|
||||||
|
setupGame();
|
||||||
|
loadLevel(LEVEL2,0,2)
|
||||||
|
setInterval(()=>{
|
||||||
|
step()
|
||||||
|
draw()
|
||||||
|
},1000/60)
|
||||||
|
console.log("Running")
|
||||||
|
}
|
||||||
|
|
||||||
|
loadScript("game.js",runTests)
|
||||||
|
|
||||||
|
initializeGame()
|
||||||
|
|
||||||
|
function initializeGame() {
|
||||||
|
if (testsPass) {
|
||||||
|
runGame()
|
||||||
|
} else {
|
||||||
|
setTimeout(()=>{
|
||||||
|
initializeGame()
|
||||||
|
},1000)
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="game"></div>
|
<div id="game"></div>
|
||||||
<script type="text/javascript" src="game.js"></script>
|
<script type="text/javascript" src="game.test.js"></script>
|
||||||
</body>
|
</body>
|
Loading…
x
Reference in New Issue
Block a user