Skip to content

Commit

Permalink
Merge branch 'experiment_bots' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Faraday committed Aug 31, 2018
2 parents 90aad1e + b3da74f commit ba9e094
Show file tree
Hide file tree
Showing 19 changed files with 457 additions and 115 deletions.
10 changes: 10 additions & 0 deletions config/bots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GridGame.data.bots = {
'All Towards': '808c86eb544d9efaf94a30efc91bdfaf',
'Code on Page': 'code',
'Faraday Cage': 'd9eb9a70ad0dc0fb1885be0fce032adc',
'Gist Id': 'bot_id',
'Long Random': 'b9a0fe99060d9be1fc617bb7262f57be',
'Manual': 'manual',
'Rotate': '6634a7f381bb679ee7c2d90d93588d66',
'Short Random': 'b16df33acb0f0c7830d2ca2d656f80be'
};
7 changes: 5 additions & 2 deletions lib/data/game.js → config/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ GridGame.data.game = {
height: 12,
turn_time: 100,
spawn_interval: 3,
city_conversion_size: 10
};
city_conversion_size: 10,
rock_count: 0,
wall_count: 0,
wall_health: 100
};
23 changes: 23 additions & 0 deletions config/players.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
GridGame.data.players = [
{
name: 'green',
start: GridGame.positions.north_west_corner(2),
direction: 'east',
playable: true,
towardsX: 'east',
towardsY: 'south',
awayX: 'west',
awayY: 'north'
},
{
name: 'red',
start: GridGame.positions.south_east_corner(2),
direction: 'west',
playable: true,
towardsX: 'west',
towardsY: 'north',
awayX: 'east',
awayY: 'south'
}
// Only 2 for API-driven game
];
33 changes: 30 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
<!DOCTYPE HTML>
<html>
<head>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'XXX-google-analytics-id-XXX', 'auto');
ga('send', 'pageview');

</script>

<title>Grid Game</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">

<script type="text/javascript" src="./lib/external/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="./lib/grid_game.js"></script>

<script type="text/javascript" src="./lib/data/game.js"></script>
<script type="text/javascript" src="./lib/resize_grid.js"></script>

<script type="text/javascript" src="./config/game.js"></script>
<script type="text/javascript" src="./lib/positions.js"></script>
<script type="text/javascript" src="./lib/data/players.js"></script>
<script type="text/javascript" src="./config/players.js"></script>
<script type="text/javascript" src="./config/bots.js"></script>
<script type="text/javascript" src="./lib/data/key_maps.js"></script>
<script type="text/javascript" src="./lib/data/key_display.js"></script>

Expand All @@ -25,12 +41,20 @@
<script type="text/javascript" src="./lib/classes/board.js"></script>
<script type="text/javascript" src="./lib/classes/player.js"></script>
<script type="text/javascript" src="./lib/classes/keyboard.js"></script>
<script type="text/javascript" src="./lib/classes/api.js"></script>
<script type="text/javascript" src="./lib/api_ui.js"></script>

<link rel="stylesheet" href="./style/grid_game.css"/>
</head>
<body>
<h1>My Grid Game</h1>
<div id="grid_game"></div>
<div id="api_space">
<div id="forms"></div>
<div class="button" data-action="start">Start</div>
<div class="button" data-action="stop">Stop</div>
<div class="button" data-action="reset">Reset</div>
</div>
<div class="footer">
<p>
<a href="https://www.github.com/AJFaraday/grid_game" target="_blank">Grid Game</a>
Expand All @@ -43,7 +67,10 @@ <h1>My Grid Game</h1>
<a href="https://github.com/AJFaraday/grid_game/issues" target="_blank">issue tracker</a>.
</p>
<p>
left-twix.com, right-twix.com, lefttwix.com and righttwix.com
<a href="http://www.left-twix.com">left-twix.com</a>,
<a href="http://www.right-twix.com">right-twix.com</a>,
<a href="http://www.lefttwix.com">lefttwix.com</a> and
<a href="http://www.righttwix.com">righttwix.com</a>
can be bought from
<a href="https://www.domainmonster.com">Domain Monster</a> at
spectacular expense, but I'd listen to reasonable offers.
Expand Down
68 changes: 68 additions & 0 deletions lib/api_ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
GridGame.api_ui = {
init: function() {
$.each(
GridGame.apis,
function(i, api) {
var space = $('<div>')
.addClass('api_space')
.addClass('api_' + i);
api.sel = $('<select>').data('api-index', i).data('api', api);
api.sel.on('change', function() {GridGame.api_ui.set_source(api)});
$.each(
GridGame.data.bots,
function(name, gist) {
api.sel.append(
$('<option>')
.attr('value', gist)
.html(name)
);
}
);
space.append(api.sel);
api.id_field = $('<input>').css('display', 'none');
space.append(api.id_field);
api.id_field.on('change', function() {GridGame.api_ui.set_source(api)});

api.code_field = $('<textarea>').css('display', 'none').addClass('code_field');
api.code_field.attr('rows', 20);
space.append(api.code_field);
api.code_field.on('change', function() {GridGame.api_ui.set_source(api)});
$('#forms').append(space);
GridGame.api_ui.set_source(api);
}
);

this.init_button_actions();
},

set_source: function(api) {
switch(api.sel.val()) {
case 'code':
api.code_field.show();
api.id_field.hide();
api.func = function() {eval(api.code_field.val())};
api.player.playable = false;
break;
case 'bot_id':
api.id_field.show();
api.get_gist(api.id_field.val());
api.player.playable = false;
break;
case 'manual':
api.func = function() {};
api.player.playable = true;
break;
default:
api.get_gist(api.sel.val());
api.id_field.hide();
api.code_field.hide();
api.player.playable = false;
}
},

init_button_actions: function() {
$('.button[data-action="start"]').on('click', function() {GridGame.start()});
$('.button[data-action="stop"]').on('click', function() {GridGame.stop()});
$('.button[data-action="reset"]').on('click', function() {GridGame.reset()});
}
};
54 changes: 54 additions & 0 deletions lib/classes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
GridGame.apis = [];
GridGame.classes.api = function(player, config) {
GridGame.apis.push(this);
this.player = player;

this.get_named_bot = function(name) {
var bot = GridGame.data.bots[name];
if (bot === undefined) {
console.log("No known bot named " + name);
} else {
this.get_gist(bot);
}
};


this.func = function() {};

this.get_gist = function(id) {
var api = this;
$.get(
'https://api.github.com/gists/'+id,
function(response) {
var code = response.files['bot.js'].content;
console.log(code);
api.func = function() {eval(code)};
}
)
};

this.towardsX = function() {
this.player.change_direction(config.towardsX);
};

this.towardsY = function() {
this.player.change_direction(config.towardsY);
};

this.awayX = function() {
this.player.change_direction(config.awayX);
};

this.awayY = function() {
this.player.change_direction(config.awayY);
};

this.random_direction = function() {
this.player.go_random_direction();
}

this.turn = function() {
return GridGame.turn_number;
};

};
22 changes: 21 additions & 1 deletion lib/classes/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GridGame.classes.board = function () {
for (var y = 0; y < GridGame.height; y++) {
this.build_row(y);
}
GridGame.resize.make_grid_fit();
};

this.build_table = function () {
Expand All @@ -24,7 +25,7 @@ GridGame.classes.board = function () {
var row = [];
for (var x = 0; x < GridGame.width; x++) {
var tile = this.build_tile(x, y);
row.push(tile)
row.push(tile);
current_row.append(tile.build());
}
this.rows.push(row);
Expand Down Expand Up @@ -83,5 +84,24 @@ GridGame.classes.board = function () {

};

// TODO maybe move these two to an obstacle placing object
this.place_n_obstacles = function(flag_name, number) {
for(var i = number; i > 0; i--){
this.place_random_obstacle(flag_name);
}
};

this.place_random_obstacle = function(flag_name) {
var x = Math.floor(Math.random() * GridGame.width);
var y = Math.floor(Math.random() * GridGame.height);
var tile = this.tile(x, y);
if (tile.city || tile.rock || tile.wall) {
this.place_random_obstacle(flag_name);
} else {
tile[flag_name] = true;
tile.value = GridGame[flag_name + '_health'];
}
};

};

2 changes: 1 addition & 1 deletion lib/classes/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ GridGame.classes.keyboard = function() {
'keydown',
function(e) {
var command = keyboard.key_map[e.keyCode];
if (command) {
if (command && command.player.playable) {
e.preventDefault();
command.player.change_direction(command.direction);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/classes/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ GridGame.classes.player = function (config) {
GridGame.player_components.control_panel.apply(this);
GridGame.player_components.direction_change.apply(this);

this.api = new GridGame.classes.api(this, config);

this.config = config;
this.name = config.name;
this.playable = config.playable;
Expand Down Expand Up @@ -41,11 +43,15 @@ GridGame.classes.player = function (config) {
var target_belongs_to_me = (target.player == this);
var target_is_city = target.city;
var target_already_stepped_to = target.changed_player;
var target_is_rock = target.rock;
var target_is_wall = target.wall; // TODO and wall has health
return (
target_present &&
(target_unowned || target_belongs_to_me) &&
!target_is_city &&
!target_already_stepped_to
!target_already_stepped_to &&
!target_is_rock &&
!target_is_wall
)
};

Expand Down
Loading

0 comments on commit ba9e094

Please sign in to comment.