Skip to content
Open
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
31 changes: 31 additions & 0 deletions Arena/src/Arena/Resources/ArenaInstruction.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<li><a href="#dotNet">.Net Bots</a></li>
<li><a href="#php">PHP Bots</a></li>
<li><a href="#java">JAVA Bots</a></li>
<li><a href="#nodejs">Node.js Bots</a></li>
<li><a href="#firewall">Firewall Configuration</a></li>
</ul>
</nav>
Expand Down Expand Up @@ -340,6 +341,36 @@
</article>
</section>

<section id="nodejs">
<header>Bot written in Node.js</header>
<article>
<ul>
<li>
Setup sample bot in Node.js
<ol>
<li>Download Node.js from <a href="https://nodejs.org/en/" target="blank">https://nodejs.org/en/</a> and install it</li>
<li>Copy folder "TankBlasterBot" (hello_wars/nodeJs/TankBlasterBot/) to your local directory </li>
<li>Open command prompt and go to project directory</li>
<li>Type "npm install" to install all necessary modules</li>
<li>Type "node app.js" to run application</li>
<li>Your bot should be accessible at "http://localhost:8081/". Check "http://localhost:8081/info" in your browser to see if it works</li>
</ol>
</li>

<li>
Modify ArenaConfiguration file:
<ol>
<li>change game type you wish to play</li>
<li>change elimination type name to your preferred elimination type</li>
<li>modify bot urls. If you need more players, just type a few copies of the same url. They will be interpreted as a separate bots</li>
</ol>
</li>
<li>When you run the arena, it should automatically connect with bots. Press "Play" to start game</li>
</ul>

</article>
</section>

<section id="firewall">
<header>Firewall configuration</header>
<article>
Expand Down
Binary file modified Documentation/HelloWars - TankBlaster.pptx
Binary file not shown.
144 changes: 144 additions & 0 deletions nodeJs/TankBlasterBot/PerformNextMove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
var express = require('express');
var bodyParser = require('body-parser');

var arena = new Object();

module.exports = (function() {
var api = express.Router();
api.use(bodyParser.json());
api.post('/PerformNextMove', function(req, res) {
arena = req.body;
var result = CalculateNextMove()
res.writeHead(200, {"Content-Type": "application/json"});
var json = JSON.stringify(result);
res.end(json);
});
return api;
})();

function CalculateNextMove()
{
var result = new Object();

if(arena.MissileAvailableIn == 0 && Math.floor(Math.random() * 10) == 0){
result.Action = 2;
result.FireDirection = Math.floor(Math.random() * 4);
}
else if(Math.floor(Math.random() * 10 ) == 0) {
result.Action = 1;
result.FireDirection = 0;
}
else{
result.Action = 0;
result.FireDirection = 0;
}

result.Direction = GetDirection();

return result;
}

function GetDirection(){
var directions = [0, 1, 2, 3];
var availableDirections = new Array();

for(var i = 0; i < directions.length; i++){
if(!IsInDangerZone(AddDirectionMove(ParsePoint(arena.BotLocation) , directions[i]))){
availableDirections.push(directions[i]);
}
}

return availableDirections[Math.floor(Math.random() * availableDirections.length)];
}

function GetSurroundingPoints(centerLocation, radius){
var locations = new Array();
for (var i = 1; i <= radius; i++){
locations.push({ X:centerLocation.X, Y:centerLocation.Y + i});
locations.push({ X:centerLocation.X, Y:centerLocation.Y - i});
locations.push({ X:centerLocation.X + i, Y:centerLocation.Y});
locations.push({ X:centerLocation.X - i, Y:centerLocation.Y});
}
return ValidateMultipleLocations(locations);
}

function ValidateMultipleLocations(locations){
var result = new Array();
for (var i = 0; i < locations.length; i++){
if(IsLocationValid(locations[i])){
result.push(locations[i]);
}
}
return result;
}

function IsLocationValid(location){
xlen = arena.Board.length;
ylen = arena.Board[0].length;
var result = location.X >= 0 && location.X < xlen && location.Y >= 0 && location.Y < ylen;
return result;
}

function IsInDangerZone(location){
if (!IsLocationValid(location)){
return true;
}

for(var i = 0; i < arena.Bombs.length; i++){
var dangerZone = GetDangerZone(ParsePoint(arena.Bombs[i].Location) , arena.Bombs[i].ExplosionRadius);
for(var j = 0; j < dangerZone.length; j++){
if (dangerZone[j].X == location.X && dangerZone[j].Y == location.Y){
return true;
}
}
}

for(var i = 0; i < arena.Missiles.length; i++){
var dangerZone = GetDangerZone(ParsePoint(arena.Missiles[i].Location) , arena.Missiles[i].ExplosionRadius);
for(var j = 0; j < dangerZone.length; j++){
if (dangerZone[j].X == location.X && dangerZone[j].Y == location.Y){
return true;
}
}
}

if (arena.Board[location.X][location.Y] != 0){
return true;
}
return false;
}

function GetDangerZone(centerLocation, explosionRadius){
var result = GetSurroundingPoints(centerLocation, explosionRadius);
result.push(centerLocation);
return result;
}

function AddDirectionMove(location, direction){
var result = { X: location.X, Y: location.Y};
switch (direction){
case 0:
result.Y--;
break;

case 1:
result.Y++;
break;

case 2:
result.X++;
break;

case 3:
result.X--;
break;
}

return result;
}

function ParsePoint(str){
var arr = str.split(",");
return {X: parseInt(arr[0]) , Y: parseInt(arr[1])};
}

10 changes: 10 additions & 0 deletions nodeJs/TankBlasterBot/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var express = require('express');
var info = require('./info');
var PerformNextMove = require('./PerformNextMove');

var app = express();

app.get('/info', info);
app.post('/PerformNextMove', PerformNextMove);

var server = app.listen(8081);
22 changes: 22 additions & 0 deletions nodeJs/TankBlasterBot/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var express = require('express');

var info = {
Name:"node.js_Bot",
Description:"I am NODE.JS Bot",
GameType:"TankBlaster"
};
module.exports = (function() {
var api = express.Router();

api.get('/info', function(req, res) {
res.writeHead(200, {"Content-Type": "application/json"});
var json = JSON.stringify(info);
res.end(json);
});
return api;
})();





16 changes: 16 additions & 0 deletions nodeJs/TankBlasterBot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "NodeJsBot",
"version": "1.0.0",
"description": "Sample bot for TankBlaster game",
"main": "app.js",
"author": "SMT Software Services",
"license": "GPL-2.0"
"repository" : {
"type" : "git",
"url" : "https://github.com/michalczerwinski/hello_wars"
},
"dependencies": {
"express": "*",
"body-parser": "*"
}
}