英文:
JS Canvas, drawing random squares using 2D arrays
问题
以下是您的代码的翻译部分:
let board;
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [[],[]];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[[iHorizontal],[iVertical]] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize)))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize)))
cell[
[randomH],
[randomV]
] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[[iHorizontal],[iVertical]] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
setInterval(update,1000/60)
}
请注意,我已经删除了HTML中的"
,以便代码在JavaScript中正确运行。如果您需要任何进一步的帮助或解决问题,请告诉我。
英文:
I am trying to create a replication of John Conway's game of life, and I am working on making a functional 2D array that can retrieve if the cell is dead or alive, however, when attempting to draw the first 10 random cells on the screen, instead of drawing 10 random cells, my code draws 10 random lines. The code I am using is the following:
let board;
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [[],[]];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[[iHorizontal],[iVertical]] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize)))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize)))
cell[
[randomH],
[randomV]
] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[[iHorizontal],[iVertical]] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
setInterval(update,1000/60)
}
I have attempted a couple of things:
I attempted to invert line 61 to if (cell[[iVertical],[iVertical]] === 1){
, however, that just inverts the horizontal lines onto vertical lines
I have also attempted to make a nested if statement, so the code becomes:
if (cell[[iHorizontal],[iVertical]] === 1){
if (cell[[iVertical],[iHorizontal]] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
However, both this attempts have been unsuccessful.
答案1
得分: 0
您的主要问题是,您的多维数组不是100x100,而只有2x100。以下是已经修复了多维数组初始化和修正了数组索引的工作版本:
let board = {};
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
// 数组初始化
for (let iHorizontal = 0; iHorizontal * cellSize < board.width; iHorizontal++) {
cell[iHorizontal] = [];
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[iHorizontal][iVertical] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize))
cell[randomH][randomV] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
console.log(cellSize)
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[iHorizontal][iVertical] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
update();
// setInterval(update,1000/60)
}
我已经修复了数组的初始化和索引问题。
英文:
Your main issue is that instead of 100x100 multidimension array you had only 2x100 array. Here is working version, where I added multidimension array initialization and fixed array indexing (a[[i][j] => a[i][j])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let board = {};
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
// array init
for (let iHorizontal = 0; iHorizontal * cellSize < board.width; iHorizontal++) {
cell[iHorizontal] = [];
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[iHorizontal][iVertical] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize)))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize)))
cell[randomH][randomV] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
console.log(cellSize)
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[iHorizontal][iVertical] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
update();
// setInterval(update,1000/60)
}
<!-- language: lang-html -->
<canvas style="border: 1px solid red" id="board"></canvas>
<!-- end snippet -->
答案2
得分: -1
这是一个说明,说明了如何绘制矩形。
let board;
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [[],[]];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[[iHorizontal],[iVertical]] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize)))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize)))
cell[
[randomH],
[randomV]
] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
// only draw if even iVertical && even iHorizontal;
iVertical % 2 && iHorizontal % 2 && context.fillRect(
(iHorizontal * cellSize),
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[[iHorizontal],[iVertical]] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
update();
//setInterval(update,1000/60)
}
<canvas style="border: 1px solid red" id="board"></canvas>
英文:
Here's an illustration of how your rectangles are being drawn.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let board;
let context;
const cellAmount = 100;
let cellSize;
const numOfSpawn = 10;
var cell = [[],[]];
function createBoard(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
cell[[iHorizontal],[iVertical]] = 0;
}
}
let i = 0;
for (i; i < numOfSpawn; i++){
var randomH = Math.floor(Math.random()*Math.floor((board.clientWidth/cellSize)))
var randomV = Math.floor(Math.random()*Math.floor((board.clientHeight/cellSize)))
cell[
[randomH],
[randomV]
] = 1;
}
}
function update(){
board.width = document.documentElement.clientWidth;
board.height = document.documentElement.clientHeight;
if (board.height < board.width){
cellSize = Math.floor(board.height/cellAmount);
} else {
cellSize = Math.floor(board.width/cellAmount);
}
let iVertical = 0;
for (iVertical; iVertical * cellSize < board.height; iVertical++){
let iHorizontal = 0;
for(iHorizontal; iHorizontal * cellSize < board.width; iHorizontal++){
context.fillStyle = "black";
// only draw if even iVertical && even iHorizontal;
iVertical % 2 && iHorizontal % 2 && context.fillRect(
(iHorizontal * cellSize),
iVertical * cellSize,
cellSize,
cellSize
)
if (cell[[iHorizontal],[iVertical]] === 1){
context.fillStyle = "white";
context.fillRect(
iHorizontal * cellSize,
iVertical * cellSize,
cellSize,
cellSize
)
}
}
}
}
window.onload = () => {
board = document.getElementById("board");
context = board.getContext("2d");
createBoard();
update();
//setInterval(update,1000/60)
}
<!-- language: lang-html -->
<canvas style="border: 1px solid red" id="board"></canvas>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论