I am starting to write a recursion backtrack maze system printed in console but I already stumble in a trivial problem, I can't set the lines from my grid structure as you can see below:
console.log("Teste de Labirinto - Recursive Backtracking")
let grid = []
let cells = []
const width = 8
const height = 10
for (let i = 0; i < width; i++){
cells.push(".")
}
for (let j = 0; j < height; j++){
grid.push(cells)
}
function printMaze(){
let line = " "
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
line += " " + grid[j][i] + " "
}
console.log(line)
line = " "
}
}
function createMazeBorder() {
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
if(i == 0) {
grid[j][i] = "X"
}
}
}
}
createMazeBorder()
printMaze()
How can I create a wall of "X" in my grid? I tried all the "ifs" as possible and swap the "i" and "j", widht, height, still can't do it... Thank you for the time.
Well, solved it, it was not possible to create the walls because all my cells got the same reference, there is the code:
console.log("Teste de Labirinto - Recursive Backtracking")
let grid = []
const width = 8
const height = 10
for (let j = 0; j < height; j++){
let cells = [];
for (let i = 0; i < width; i++){
cells.push(".")
}
grid.push(cells)
}
function printMaze(){
let line = " "
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
line += " " + grid[j][i] + " "
}
console.log(line)
line = " "
}
}
function createMazeBorder() {
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
if(j == 0 || i == 0 || j == height-1 || i == width-1) {
grid[j][i] = "X"
}
}
}
}
createMazeBorder()
printMaze()
Related
function LCSubStr(X, Y) {
let m = X.length;
let n = Y.length;
let result = 0;
let end;
let len = new Array(4);
for (let i = 0; i < len.length; i++) {
len[i] = new Array(n);
for (let j = 0; j < n; j++) {
len[i][j] = 0;
}
}
let currRow = 0;
for (let i = 0; i <= m; i++) {
for (let j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
len[currRow][j] = 0;
}
else if (X[i - 1] == Y[j - 1]) {
len[currRow][j] = len[1 - currRow][j - 1] + 1;
if (len[currRow][j] > result) {
result = len[currRow][j];
end = i - 1;
}
}
else {
len[currRow][j] = 0;
}
}
currRow = 1 - currRow;
}
if (result == 0) {
return "-1";
}
return X.substr(end - result + 1, result);
}
// Driver Code
let X = "GeeksforGeeks";
let Y = "GeeksQuiz";
// function call
document.write(LCSubStr(X, Y));
How can I convert this code for multiple input?
I checked many lcs code but no one works with
ABCQEFDEFGHIJ BCXEFGYZBCDEWEFGHU > EFGH
This one just works good without any problem. I should convert this one for multiple input in Javascript.
Now we have X,Y but it shoulde be with multiple inputs.
I am currently trying to solve the xmas tree problem, with internal tree-like shape.
issue is with internal spacing, it supposed to be like: 1, 5, 7, 9. Instead it is 1, 3, 4, 5. I do not know, how to increment s loop by 2 in each loop turn.
/*
*********
**** ****
*** ***
** **
* *
*********
*/
function drawTree(h) {
let n = h + 3;
for (var i = 1; i <= 1; i++) {
var temp = "";
for (var j = 1; j <= n; j++) {
temp = temp + "*";
}
console.log(temp);
}
for (var i = 0; i < h - 2; i++) {
var tree = '';
console.log("\n");
for (var k = 3; k <= h - i; k++) {
tree += "*";
};
tree += "s";
for (var k = 1; k <= i; k++) {
for (var k = 1; k <= i; k++) {
tree += "s";
};
tree += "s";
};
for (var k = 3; k <= h - i; k++) {
tree += "*";
};
console.log(tree);
};
console.log("\n");
let g = h + 3;
for (var i = 1; i <= 1; i++) {
var temp = "";
for (var j = 1; j <= g; j++) {
temp = temp + "*";
}
console.log(temp);
}
};
drawTree(6);
function drawTree(stars, rowLength) {
for (let row = 0; row < rowLength; row++) {
if (row === 0) {
console.log("*".repeat(stars));
} else if(row === rowLength - 1) {
console.log("*".repeat(stars));
} else {
let spaces = 2 * row - 1;
if (spaces > stars) {
spaces = stars;
}
let numStarsInRow = "*".repeat((stars - spaces) / 2);
console.log(numStarsInRow + " ".repeat(spaces) + numStarsInRow);
}
}
}
drawTree(9, 5)
You can implement this by nesting loops over the height and the width of the tree, noting that the output is a * whenever:
it's the first or last row; or
the current x position is less than or equal to the halfway point minus the row number; or
the current x position is greater than or equal to the halfway point plus the row number
For all other cases the output is a space. For example:
function drawTree(height) {
// compute the width of the tree from the height
let width = height % 2 ? height + 2 : height + 3;
// find the halfway point
let half = (width - 1) / 2;
for (let i = 0; i < height; i++) {
let l = '';
for (let j = 0; j < width; j++) {
if (i == 0 || // first row
i == height - 1 || // last row
j <= (half - i) || // left side
j >= (half + i) // right side
) {
l += '*';
}
else {
l += ' ';
}
}
console.log(l);
}
}
drawTree(6);
drawTree(5);
I need to optymalise my chessboard code (JS) to avoid soo many lines of copied code for the same chess piece like pawn, and other pieces.
I need to create better data structure i think , but i dont have good idea how to iterate my data structure to locate all pieces on the board.This is codepen link for this project.
For example this is my function to place knight piece on the board.
function addKnight() {
//piece create function
var KnightFirst = oneKnight[8];
var KnightSecond = oneKnight[15];
var KnightThird = oneKnight[48];
var KnightFour = oneKnight[55];
pieceKnight1 = document.createElement("i");
pieceKnight1.classList.add("fas");
pieceKnight1.classList.add("fa-chess-knight");
pieceKnight2 = document.createElement("i");
pieceKnight2.classList.add("fas");
pieceKnight2.classList.add("fa-chess-knight");
pieceKnight3 = document.createElement("i");
pieceKnight3.classList.add("fas");
pieceKnight3.classList.add("fa-chess-knight");
pieceKnight4 = document.createElement("i");
pieceKnight4.classList.add("fas");
pieceKnight4.classList.add("fa-chess-knight");
KnightFirst.appendChild(pieceKnight1);
KnightSecond.appendChild(pieceKnight2);
pieceKnight2.classList.add("color");
KnightThird.appendChild(pieceKnight3);
KnightFour.appendChild(pieceKnight4);
pieceKnight4.classList.add("color");
}
I would do something like this. It isn't a complete code but just the structure.
var piece = {
x, y,
type,
color,
sprite: new Image(),
}
var pieces = [];
function addPieces () {
if (i < 16) {
for (let i = 0; i < 32; i++) {
for (let j = 0; j < 16; j++) {
pieces.push(piece);
pieces[i].type = "pawn";
if (j < 4) {
//set position for white pawns
} else {
//set position for black pawns
}
} }
if (i >= 16 && i < 20) {
for (let j = 0; j < 4; j++) {
pieces.push(piece);
pieces[i].type = "knight";
if (j < 2) {
//set position for white knights
} else {
//set position for black knights
}
}}
if (i >= 20 && i < 24) {
for (let j = 0; j < 4; j++) {
pieces.push(piece);
pieces[i].type = "rook";
if (j < 2) {
//set position for white rooks
} else {
//set position for black rooks
}
}}
if (i >= 24 && i < 28) {
for (let j = 0; j < 4; j++) {
pieces.push(piece);
pieces[i].type = "bishop";
if (j < 2) {
//set position for white bishops
} else {
//set position for black bishops
}
}}
if (i >= 28 && i < 30) {
for (let j = 0; j < 2; j++) {
pieces.push(piece);
pieces[i].type = "queen";
if (j < 1) {
//set position for white queen
} else {
//set position for black queen
}
}}
if (i >= 30 && i < 32) {
for (let j = 0; j < 2; j++) {
pieces.push(piece);
pieces[i].type = "king";
if (j < 1) {
//set position for white king
} else {
//set position for black king
}
} }
}
}
You could apply the same type of function for the update each frame, but that's your call. Hope this helps!
var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
i want result like :
array("s,red,cotton","s,red,linen","s,blue,cotton","s,blue,linen","s,black,cotton","s,black,linen");
array("m,red,cotton","m,red,linen","m,blue,cotton","m,blue,linen","m,black,cotton","m,black,linen");
would you like to help me please use javascript or jquery. Thank you :)
Three loops for each array. Just loop through every array, and append to new array.
var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
var arrayMaterials = []
for (var i = 0; i < size.length; i++) {
for (var j = 0; j < color.length; j++) {
for (var k = 0; k < material.length; k++) {
arrayMaterials.push(size[i] + "," + color[j] + "," + material[k]);
}
}
}
console.log(arrayMaterials);
As said in Comment you need 3 loops
var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
for (var a = 0; a < size.length; a++) {
for (var b = 0; b < color.length; b++) {
for (var c = 0; c < material.length; c++) {
console.log(size[a] + " , " + color[b] + " , " + material[c]);
}
}
}
Ok so i tried to make conway's game of life in p5.js and i am stuck in some wierd bug .
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
}
return arr;
}
function countNeighbors(grid, x, y) {
let sum = 0;
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
let col = x + i;
let row = y + i;
if (grid[col][row] === undefined){
sum += grid[col][row];
}
}
}
sum-=grid[x][y]
return sum;
}
let grid;
let next;
let cols;
let rows;
let resolution = 20;
let fr = 15;
function setup() {
createCanvas(600, 400);
frameRate(fr);
cols = width / resolution;
rows = height / resolution;
next = make2DArray(cols, rows);
grid = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j] = floor(random(2));
}
}
}
function draw() {
background(0);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let x = i * resolution;
let y = j * resolution;
if (grid[i][j] == 1) {
fill(0);
stroke(150);
rect(x, y, resolution - 1, resolution - 1);
} else {
fill(255);
stroke(150);
rect(x, y, resolution - 1, resolution - 1);
}
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let state = grid[i][j];
let neighbors = countNeighbors(grid, i, j);
if (state == 0 && neighbors == 3) {
next[i][j] = 1;
} else if (state == 1 && (neighbors < 2 || neighbors > 3)) {
next[i][j] = 0;
} else {
next[i][j] = state;
}
}
}
grid = next;
}
Soo basically i have function countNeighbors and i think that bug is in there , i tried checking if col and row are i boundaries of array
like this
if (col>-1 && col <grid.length && row>-1 && row < grid[0].length){
//increase sum
}
but its even worse. I am new to js so i figured out that if for example
let x=new Array(10);
//and then when i try this
console.log(c[-1])
//it should give undefined
but program still wont work :(
Thanks!
With a few changes you can make it work:
1) Replace let row = y + i; by let row = y + j; That typo was making most of the counts off.
2) Replace the condition
if (grid[col][row] === undefined){
by
if (0 <= col && col < cols && 0 <= row && row < rows){
The problem with your original condition is that if grid[col] is undefined then grid[col][row] is undefined[row], which doesn't make sense.
3) For consistency sake, add a semicolon to the end of sum-=grid[x][y]
4) Finally, so as to not create an unintended alias, you need to replace grid = next by something like:
for(let i = 0; i < grid.length; i++){
grid[i] = next[i].slice();
}
Alternatively, make next a variable which is local to draw() and place the line let next = make2DArray(cols, rows); to the beginning of draw().