Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for repeatable, numbered board layouts #10

Merged
merged 1 commit into from
Jun 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions assets/js/board.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var Board = {
init: function() {
init: function(boardNumber) {
var fullBoard;

Board.initRandom(boardNumber);

// initialize board
HEIGHT = Math.min(Math.floor(Math.random() * 11) + 5, 15);
WIDTH = Math.min(Math.floor(Math.random() * 11) + 5, 15);
HEIGHT = Math.min(Math.floor(Board.random() * 11) + 5, 15);
WIDTH = Math.min(Math.floor(Board.random() * 11) + 5, 15);

Board.board = new Array(WIDTH);

for (var i=0; i<WIDTH; i++) {
Expand All @@ -25,7 +28,7 @@ var Board = {

// initialize items on board
do {
Board.numberOfItemTypes = Math.floor(Math.random() * 3 + 3);
Board.numberOfItemTypes = Math.floor(Board.random() * 3 + 3);
} while(Board.numberOfItemTypes * Board.numberOfItemTypes >= HEIGHT * WIDTH)
Board.totalItems = new Array();
Board.simpleBotCollected = new Array(Board.numberOfItemTypes);
Expand All @@ -38,17 +41,17 @@ var Board = {
Board.totalItems[i] = i * 2 + 1;
for (var j=0; j<Board.totalItems[i]; j++) {
do {
x = Math.min(Math.floor(Math.random() * WIDTH), WIDTH);
y = Math.min(Math.floor(Math.random() * HEIGHT), HEIGHT);
x = Math.min(Math.floor(Board.random() * WIDTH), WIDTH);
y = Math.min(Math.floor(Board.random() * HEIGHT), HEIGHT);
} while (Board.board[x][y] != 0);
Board.board[x][y] = i + 1;
}
}

// get them the same starting position
do {
x = Math.min(Math.floor(Math.random() * WIDTH), WIDTH);
y = Math.min(Math.floor(Math.random() * HEIGHT), HEIGHT);
x = Math.min(Math.floor(Board.random() * WIDTH), WIDTH);
y = Math.min(Math.floor(Board.random() * HEIGHT), HEIGHT);
} while (Board.board[x][y] != 0);
Board.myX = x;
Board.myY = y;
Expand Down Expand Up @@ -151,6 +154,29 @@ var Board = {
}
}
return true;
},
initRandom: function(boardNumber) {
// Create a random number generator (PRNG) for board
// setup use and one for any other use. Doing this
// allows us to better control the sequence of numbers
// we receive. Only those functions generating random
// numbers for board setup should call Board.random().

Math.seedrandom(boardNumber);
Board.boardSetupPRNG = Math.random;
Math.seedrandom();
Board.normalPRNG = Math.random;
},
random: function() {
// Generate a random number from the board setup
// PRNG and then switch Math.random back to the normal PRNG.
var number;

Math.random = Board.boardSetupPRNG;
number = Math.random();
Math.random = Board.normalPRNG;

return number;
}
}

Expand Down
38 changes: 36 additions & 2 deletions assets/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var GamePlay = {
$('.forward').bind('click', function() { Board.processMove(); GamePlay.draw();});
$('.newgame').bind('click', function() { GamePlay.setupNewGame();});
$('.reset').bind('click', function() { Board.reset();});
$('#set_board').bind('click', function() { GamePlay.setBoardNumber();});
$('#board_number').bind('keyup', function(e) { if(e.keyCode == 13) {GamePlay.setBoardNumber();}});

$('#check_breadcrumbs').click(function(evt) {
if (evt.srcElement.checked) {
GamePlay.show_breadcrumbs = true;
Expand Down Expand Up @@ -37,8 +40,29 @@ var GamePlay = {
};

},
setupNewGame: function() {
Board.init();
setupNewGame: function(boardNumber) {
// Create a new board setup according to the following priority:
//
// 1. If a board number is passed in, use that.
// 2. If the bot has default_board_number() defined, use that.
// 3. Generate a random board number.
var nextBoardNum;

if(boardNumber === undefined) {
if ( typeof default_board_number == 'function' && !isNaN(parseInt(default_board_number()))) {
nextBoardNum = default_board_number()
} else {
Math.seedrandom();
nextBoardNum = Math.min(Math.floor(Math.random() * 999999), 999999);
}
} else {
nextBoardNum = boardNumber;
}

$('#board_number').val(nextBoardNum);

Board.init(nextBoardNum);

Board.newGame();
GamePlay.itemTypeCount = get_number_of_item_types();
document.getElementById('grid').width = GamePlay.itemTypeCount * 50 + WIDTH * 50;
Expand Down Expand Up @@ -145,5 +169,15 @@ var GamePlay = {
}
}
}
},
setBoardNumber: function() {
var boardNumber;

boardNumber = parseInt($('#board_number').val());
if (!isNaN(boardNumber)) {
GamePlay.setupNewGame(boardNumber);
} else {
GamePlay.setupNewGame();
}
}
}
Loading