I am having trouble figuring out the out of bounds part and I am not sure if my move is correct or not
I copied the code from someone else but I am having trouble understanding it why is there only a 0 or a 1 in the brackets? doesn’t that mean it won’t move off of the first row or column and why is there only one set of brackets? shouldn’t there be 2? it is a 2d matrix.

``````const prompt = require('prompt-sync')({sigint: true});

const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';

class Field {
constructor(arr){
this._arr = arr;
this.currentpos = [0,0];
// this.x = 0;
// this.y = 0;
}
get arr(){
return this._arr;
}
print(){
for (let row of this._arr){
console.log(row.join(' '));
}
}

let move = prompt('which way would you like to move? up, down, left, or right: ');
move = move.toLowerCase();
// possibly broken
switch(move) {
case "up":
this.currentpos[0]--;
// this.print();
break;
case 'down':
this.currentpos[0]++
// this.print();
break;
case 'left':
this.currentpos[1]--;
// this.print();
break;
case "right":
this.currentpos[1]++;
// this.print();
break;
default:
console.log('check spelling')
break;

}

}

generateField(per){
const width = this.arr.length;
const height = this.arr[0].length;
let fieldSize = height* width;
let numHoles =  Math.floor( per * fieldSize);
let holeCount = 0;
let locX = Math.floor(Math.random() * width);
let locY = Math.floor(Math.random() * height);
// console.log(numHoles);

// console.log(numHoles);
//creating the board placements
for (let i = 0 ; i< width; i++ ){
for (let j = 0; j< height ; j++){
let num = Math.floor(Math.random() * 5)
if(holeCount < numHoles ){
switch (num){
case 2:
this.arr[i][j]= hole;
holeCount++;
// console.log(holeCount);
break;
default:
this.arr[i][j] = fieldCharacter;
break;
}
// placing hat
this.arr[locX][locY] = hat;

// placing starter
this.arr[0][0] = pathCharacter;
} else {
this.arr[i][j] = fieldCharacter;
}
}
}
}
}

// game play
function game(){

//creating the board
let map =[
[1,1,1,1,1,1,1],
[1,0,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,2,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1]
];

const newField = new Field(map);

newField.generateField(0.3);
newField.print();

//playing game

if(Field.currentPosition == hole && Field.validMove == true){
console.log(' Game Over : you fell into a hole')
// break;
} else if (currentPosition == undefined){
//not working properly
console.log('Game over :you moved out of bounds');
// break;
} else if (Field.currentPosition == hat){
console.log('Congratulations you win');
// break;
} else {
newField.print();
}

}

game();
``````

So I have tried everything I can think of to fix this and nothing is working. it is just rolling over the holes and not going through the if else statements. It is also creating a bunch of stars in its path and I have no Idea how to fix it.

``````const prompt = require('prompt-sync')({sigint: true});

const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';

class Field {
constructor(arr){
this._arr = arr;
this.x = 0;
this.y = 0;
// this.currentpos = [this.x, this.y];

}
get arr(){
return this._arr;
}
print(){
for (let row of this._arr){
console.log(row.join(' '));
}
}

this.print();
let move = prompt('which way would you like to move? up, down, left, or right: ');
move = move.toLowerCase();
// possibly broken
switch(move) {
case "up":
this.y --;

//  this.print();
break;
case 'down':
this.y ++;
// this.print();
break;
case 'left':
this.x --;
//this.print();
break;
case "right":
this.x ++;
//this.print();
break;
default:
console.log('check spelling')
break;

}
this.arr[this.y][this.y] = pathCharacter;

}

generateField(per){
const width = this.arr.length;
const height = this.arr[0].length;
let fieldSize = height* width;
let numHoles =  Math.floor( per * fieldSize);
let holeCount = 0;
let locX = Math.floor(Math.random() * width);
let locY = Math.floor(Math.random() * height);
// console.log(numHoles);

// console.log(numHoles);
//creating the board placements
for (let i = 0 ; i< width; i++ ){
for (let j = 0; j< height ; j++){
let num = Math.floor(Math.random() * 5)
if(holeCount < numHoles ){
switch (num){
case 2:
this.arr[i][j]= hole;
holeCount++;
// console.log(holeCount);
break;
default:
this.arr[i][j] = fieldCharacter;
break;
}
// placing hat
this.arr[locX][locY] = hat;

// placing starter
this.arr[0][0] = pathCharacter;
} else {
this.arr[i][j] = fieldCharacter;
}
}
}
}
}

// game play
function game(){

//creating the board
let map =[
[1,1,1,1,1,1,1],
[1,0,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,2,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1]
];

const newField = new Field(map);

newField.generateField(0.3);
let keepPlaying = true;

//playing game

if( newField.arr[newField.y][newField.x] == hole ){

console.log(' Game Over : you fell into a hole')
keepPlaying = false;
} else if ( newField.arr[newField.y] < 0 || newField.arr[newField.x]<0 ){
//not working properly
console.log('Game over :you moved out of bounds');
keepPlaying = false;
} else if ( newField.arr[newField.y][newField.x] == hat){
console.log('Congratulations you win');
keepPlaying = false;
} else {
keepPlaying = true;
console.log('e');

}

while (keepPlaying == true){
console.log('im in')
newField.arr[newField.y][newField.x] = pathCharacter;
newField.print();
console.log(keepPlaying);
}

}

game();

``````

Hi Emma,

I haven’t checked your program in detail, just one thing I noticed:
Your general idea of tracking the current position by storing it in the Field’s x and y variables is good and should be working. But in your second approach you just check if their values are below 0. That would just be the case if you moved out of the field through the top or left boundary. You don’t check if you moved too far right or bottom.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.