Skip to content

Instantly share code, notes, and snippets.

@lytedev
Last active August 29, 2015 14:08
Show Gist options
  • Save lytedev/7ba19a8c24adb95621ca to your computer and use it in GitHub Desktop.
Save lytedev/7ba19a8c24adb95621ca to your computer and use it in GitHub Desktop.
A simple JavaScript bot that plays the multiplayer Game of Life found at the site http://gameoflifetotalwar.com/
// Create our huge, short challenge, which our current strategy happens to excel at
function create_huge_short_challenge() {
// Shortest available challende length
jQuery("#challenge_length").val("50");
// Largest map (our giant army has a huge advantage here)
jQuery("#challenge_size").val("200");
// Submit
jQuery(".btn[value='Create the Challenge']").click();
}
// Submit our life placements
// (avg 1600 lives by generation 50 with all columns and enough pieces)
function submit_placements() {
// Get board info
var height = board.length;
var width = board[0].length;
var pieces = parseInt(jQuery("#strength").html());
// Initialize our board object
var customBoard = {
width: width,
height: height,
pieces: pieces,
};
// Set all tiles blank
for (y = 0; y < height; ++y) {
customBoard[y] = {};
for (x = 0; x < width; ++x) {
customBoard[y][x] = 0;
}
}
// A helper function returning a boolean indicating whether or not an
// array contains a value. Used to check if a column exists in our
// placement of columns.
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
// Fills in our predefined columns as our play strategy dictates
function place_columns(customBoard) {
// Predefined columns
var max = Math.floor(customBoard.width * 0.45);
var min = 0;
var total = max + min;
var columns = [
Math.floor(total / 2),
Math.floor(max),
Math.floor(min),
Math.floor(total * 3/4),
Math.floor(total * 1/4),
Math.floor(total * 7/8),
Math.floor(total * 5/8),
Math.floor(total * 3/8),
Math.floor(total * 1/8),
];
// Force our loop to at least accept one column (or at least as much
// as we can fit with our total pieces).
if (customBoard.pieces < customBoard.height) {
customBoard.pieces = customBoard.height + 1;
}
// Iterate our predefined columns and fill them while we have enough
// pieces
for (var colID = 0; colID < columns.length; ++colID) {
for (y = 0; y < customBoard.height; ++y) {
customBoard[y][columns[colID]] = live;
}
customBoard.pieces -= customBoard.height;
if (customBoard.pieces < customBoard.height) {
break;
}
}
}
// Setup our placements
place_columns(customBoard);
// Converts our custom board object into a "standard" board array
// for deflation.
function convert_custom_board(customBoard) {
var board = [];
for (var y = 0; y < customBoard.height; ++y) {
board[y] = [];
for (var x = 0; x < customBoard.width; ++x) {
board[y][x] = customBoard[y][x];
}
}
return board;
}
// Get our placements for submission
var convertedBoard = convert_custom_board(customBoard);
var b = deflate_board(convertedBoard, live);
var pl = placements(b);
// Post out movements
var posting = $.post('/api/v1/challenge/'+battle+'/place', {
placements: JSON.stringify(pl),
_token: $('#_token').val()
}, 'json');
// Reload once the call completes
posting.done(function (data) {
window.location.reload();
});
// If we fail, let's bail - something weird is up
posting.fail(function (data) {
alert(data.responseJSON.error);
$(this).removeAttr('disabled');
});
}
// Waiting for challenger to accept OR
// Waiting for challenger to place pieces:
// Do nothing - wait until victor or lose state
function game_over_checker() {
setInterval(function() {
console.log("Checking win/lose state...");
var winners = jQuery("#winner, #loser");
if (winners.size() > 0) {
if (winners.is(":visible")) {
// Go home upon win/loss
window.location = "/";
}
}
}, 1000);
}
// Homepage of site
if (window.location.pathname === "/")
{
// Create our challenge
create_huge_short_challenge();
}
else if (window.location.pathname.indexOf("/challenge/") > -1)
{
// If we're supposed to be placing lives
if (jQuery("#board_ready").size() > 0)
{
submit_placements();
}
// Wait until challenge is over
else
{
game_over_checker();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment