Skip to content

Instantly share code, notes, and snippets.

@antonio-abrantes
Created July 12, 2018 12:30
Show Gist options
  • Save antonio-abrantes/ff28a7f98c9b59d30a019fa7d710e22b to your computer and use it in GitHub Desktop.
Save antonio-abrantes/ff28a7f98c9b59d30a019fa7d710e22b to your computer and use it in GitHub Desktop.
Exercício andando no tabuleiro em JavaScript
var Table = {
field: [
['', '', ''],
['', '', ''],
['', '', '']
],
position: [0,0],
up(){
if(this.position[0] > 0){
this.position[0]--;
}
this.print();
},
down(){
if(this.position[0] < this.field.length-1){
this.position[0]++;
}
this.print();
},
left(){
if(this.position[1] > 0){
this.position[1]--;
}
this.print();
},
right(){
if(this.position[1] < this.field[this.position[0]].length-1){
this.position[1]++;
}
this.print();
},
print(){
var lineStr = '';
for(var i = 0; i < this.field.length; i++){
var line = this.field[i];
for(var j = 0; j < line.length; j++){
if(this.position[0] === i && this.position[1] === j){
lineStr += '| X |';
}else{
lineStr += '| |';
}
}
lineStr += '\n';
}
console.log(lineStr);
}
}
Table.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment