Skip to content

Instantly share code, notes, and snippets.

@dnkm
Last active June 15, 2016 13:02
Show Gist options
  • Select an option

  • Save dnkm/298f18f91c86cb3672bd55072c9b074f to your computer and use it in GitHub Desktop.

Select an option

Save dnkm/298f18f91c86cb3672bd55072c9b074f to your computer and use it in GitHub Desktop.
Javascript Tutorial Sample Codes
var board;
var heroCoord = new Array(2); // x,y
//print for our perspective
function showBoard() {
console.log(heroCoord);
var print = '';
for(var y=board[0].length-1; y>=0; y--) {
for(var x=0; x<board.length; x++) {
if (heroCoord[0] == x && heroCoord[1] == y) {
print += '[c] ';
} else {
print += '[' + board[x][y] + '] ';
}
}
print += '\n';
}
console.log(print);
}
function createBoard(rows, cols) {
board = new Array(cols);
for(var x=0; x<cols; x++) {
board[x] = new Array(rows);
for(var y=0; y<rows; y++) {
board[x][y] = ' ';
}
}
}
function getRandomCoord() {
var x = parseInt(Math.random() * board.length);
var y = parseInt(Math.random() * board[0].length);
return [x,y];
}
function moveHero(x,y) {
heroCoord = [
Math.min(Math.max(heroCoord[0]+=x, 0), board.length-1),
Math.min(Math.max(heroCoord[0]+=y, 0), board[0].length-1)
];
}
createBoard(5,10);
heroCoord = getRandomCoord();
showBoard(board);
moveHero(1,0);
showBoard(board);
moveHero(-1,2);
showBoard(board);
var a = [1, 2, 34, 203, 3, 746, 200, 984, 198, 764, 9];
function bubbleSort(ar)
{
console.log(ar);
var swapped;
for(var j=0; j<ar.length-1; j++) {
swapped = false;
for(var i=0; i<ar.length-1-j; i++) {
if (ar[i] > ar[i+1]) {
var temp = ar[i];
ar[i] = ar[i+1];
ar[i+1] = temp;
swapped = true;
}
}
console.log(j + ": " + ar);
if (!swapped) {
break;
}
}
}
bubbleSort(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment