Skip to content

Instantly share code, notes, and snippets.

@jlmferreira
Created December 9, 2015 13:21
Show Gist options
  • Save jlmferreira/cd3b68b70c993391bc2f to your computer and use it in GitHub Desktop.
Save jlmferreira/cd3b68b70c993391bc2f to your computer and use it in GitHub Desktop.
Programa para gerar a logica de um jogo de caca palavras
var lines = 10;
var columns = 10;
var abc = ["a", "b", "c", "d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","w","y","z"];
var index = abc.length;
var words = ["apple","bitch","fast","datass"];
var mapWords = [];
var gameBoard = [[]];
for(i = 0 ;i < words.length;){
var initialMapColumn = (Math.floor(Math.random() * columns));
var initialMapLine = (Math.floor(Math.random() * lines));
var horizontal = (Math.random() * 1) > 0.5;
//horizontal
if (horizontal){
if((words[i].length + initialMapColumn) < columns){
mapWords.push({initialCol: initialMapColumn, initialLine: initialMapLine, word: words[i], horizontal: true});
i += 1;
}else{
//vertical
if(words[i].length + initialMapLine < lines){
mapWords.push({initialCol: initialMapColumn, initialLine: initialMapLine, word: words[i], horizontal: false});
i += 1;
}else{
//vertical
if(words[i].length + initialMapLine < lines){
mapWords.push({initialCol: initialMapColumn, initialLine: initialMapLine, word: words[i], horizontal: false});
i += 1;
}
}
}
}
}
for(y = 0;y < lines;y++){
var line = [];
for (x = 0;x<columns;x++){
line.push(abc[Math.floor(Math.random() * index)]);
}
gameBoard.push(line);
}
for(x in mapWords){
if(mapWords[x].horizontal){
for(i = 0 ; i < mapWords[x].word.length;i++){
gameBoard[mapWords[x].initialLine][mapWords[x].initialCol+i] = (mapWords[x].word.charAt(i));
}
}else{
for(i = 0 ; i < mapWords[x].word.length;i++){
gameBoard[mapWords[x].initialLine+i][mapWords[x].initialCol] = (mapWords[x].word.charAt(i));
}
}
}
for (i =1;i<gameBoard.length;i++){
console.log(gameBoard[i])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment