Skip to content

Commit

Permalink
Bots work!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Faraday committed Aug 31, 2018
1 parent bd765fc commit b3da74f
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 64 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'
};
4 changes: 2 additions & 2 deletions config/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GridGame.data.game = {
turn_time: 100,
spawn_interval: 3,
city_conversion_size: 10,
rock_count: 10,
wall_count: 40,
rock_count: 0,
wall_count: 0,
wall_health: 100
};
34 changes: 11 additions & 23 deletions config/players.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@ GridGame.data.players = [
name: 'green',
start: GridGame.positions.north_west_corner(2),
direction: 'east',
playable: true
playable: true,
towardsX: 'east',
towardsY: 'south',
awayX: 'west',
awayY: 'north'
},
{
name: 'red',
start: GridGame.positions.south_east_corner(2),
direction: 'west',
playable: true
playable: true,
towardsX: 'west',
towardsY: 'north',
awayX: 'east',
awayY: 'south'
}
/*
,
{
name: 'yellow',
start: {
x: 8,
y: 0
},
direction: 'south',
playable: false
},
{
name: 'blue',
start: {
x: 11,
y: 11
},
direction: 'north',
playable: false
}
*/
// Only 2 for API-driven game
];
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script type="text/javascript" src="./config/game.js"></script>
<script type="text/javascript" src="./lib/positions.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 @@ -40,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 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;
};

};
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
4 changes: 3 additions & 1 deletion lib/classes/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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
Loading

0 comments on commit b3da74f

Please sign in to comment.