Я изучаю javascript ООП и решил внедрить муравейник Langstons.У меня проблемы с холстом HTML5, хотя.Когда я запускаю это, мои сообщения об отладке в Ant.js всплывают, как и ожидалось, но только после того, как они все сделали, холст появляется в моем браузере (chrome).Затем это выглядит как состояние, которое должно быть в конце цикла.Почему это?Заранее спасибо ..
Вот мой код;
langstonsAnt.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Langstons Ant</title>
<script defer src="antGame.js"></script>
<script src="Ant.js"></script>
<script src="AntWorld.js"></script>
</head>
<body>
<canvas id="antboard" width="700" height="700" style="border: 5px solid black; background-size: 100%;">
Canvas not supported
</canvas>
</body>
</html>
antGame.js
let c = document.getElementById("antboard");
let ctx = c.getContext("2d");
startGame(ctx);
function startGame(ctx)
{
for (let x=0; x<100; x++)
{
for (let y=0; y<100; y++)
{
ctx.fillStyle = "red";
ctx.fillRect(7 * x, 7 * y, 7, 7);
}
}
}
// create antWorld board
let world = new AntWorld(100, ctx);
// create the ant
let ant = new Ant(world.board);
// place the ant on the board
world.setAntPos(ant.getAntX(), ant.getAntY());
// THIS IS THE LOOP I AM REFERRING THAT CALLS THE ALERTS.
for (let i=0; i<16; i++)
{
ant.moveForward();
world.setAntPos(ant.getAntX(),ant.getAntY());
//sleep(100);
}
function sleep(milliseconds) {
let start = new Date().getTime();
for (let i=0; i<1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
Ant.js
class Ant
{
constructor(board)
{
this.board = board;
this.antX = 50;
this.antY = 35;
this.NORTH = 0;
this.EAST = 1;
this.SOUTH = 2;
this.WEST = 3;
this.antDirection = this.NORTH;
}
getAntX()
{
return this.antX;
}
getAntY()
{
return this.antY;
}
moveForward()
{
switch (this.antDirection)
{
case this.NORTH:
// change direction and colour the square based on rules
if (this.board[this.antY][this.antX] === 0) // if sqr is white
{
this.board[this.antY][this.antX] = 1;
alert('About to fillRect');
ctx.fillStyle = "black";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.WEST;
} else { // if sqr is black
this.board[this.antY][this.antX] = 0;
alert('About to fillRect');
ctx.fillStyle = "white";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.EAST;
}
// move ant forward
this.antY--;
break;
case this.SOUTH:
// then colour the new square based on rules
if (this.board[this.antY][this.antX] === 0)
{
this.board[this.antY][this.antX] = 1;
alert('About to fillRect');
ctx.fillStyle = "black";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.EAST;
} else {
this.board[this.antY][this.antX] = 0;
alert('About to fillRect');
ctx.fillStyle = "white";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.WEST;
}
// move ant forward
this.antY++;
break;
case this.EAST:
// then colour the new square based on rules
if (this.board[this.antY][this.antX] === 0)
{
this.board[this.antY][this.antX] = 1;
alert('About to fillRect');
ctx.fillStyle = "black";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.NORTH;
} else {
this.board[this.antY][this.antX] = 0;
alert('About to fillRect');
ctx.fillStyle = "white";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.SOUTH;
}
// move ant forward
this.antX++;
break;
case this.WEST:
// then colour the new square based on rules
if (this.board[this.antY][this.antX] === 0)
{
this.board[this.antY][this.antX] = 1;
alert('About to fillRect');
ctx.fillStyle = "black";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.SOUTH;
} else {
this.board[this.antY][this.antX] = 0;
alert('About to fillRect');
ctx.fillStyle = "white";
ctx.fillRect(7 * this.antX, 7 * this.antY, 7, 7);
this.antDirection = this.NORTH;
}
// move ant forward
this.antX--;
break;
}
}
}
AntWorld.js
class AntWorld
{
constructor(size)
{
this.board = Array(size).fill(0).map(()=>Array(size).fill(0));
}
setAntPos(antX, antY)
{
this.board[antY][antX] = 1;
ctx.fillStyle = "black";
ctx.fillRect(7 * antX, 7 * antY, 7, 7);
}
}