forked from JSJitsu/hero-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-battle.js
186 lines (152 loc) · 4.92 KB
/
test-battle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
If you'd like to test your hero code locally, run this script using node.
While you do not need to run this script to enter the battle, it is highly
recommended that you run a few test battles to ensure your hero is working as
you intended.
See README.md for more information.
*/
// possible cli options, and their default values
let cliOptions = {
wait: false,
turns: 15
};
// accepting cli parameters
var args = require('commander');
args.description('CLI to test the hero.js code locally')
.option('-w, --wait', 'Turn by turn step through of the battle')
.option('-t, --turns [int]', 'Specifies how many turns to run', parseInt)
.parse(process.argv);
// args validation
if (args.turns && isNaN(args.turns)) {
console.log();
console.log("**Invalid turns input, input has to be an integer");
args.outputHelp();
return false;
}
// overwrite cli parameters
for (var key in args) {
cliOptions[key] = args[key];
}
// Get the helper file and the Game logic
var ai_battle_engine = require('ai-battle-engine');
var GameEngine = new ai_battle_engine();
var helpers = require('./helpers.js');
var Game = GameEngine.getGame();
// Get my hero's move function ("brain")
var heroMoveFunction = require('./hero.js');
// The move function ("brain") the practice enemy will use
var enemyMoveFunction = function (gameData, helpers) {
// Move in a random direction
// var choices = ['North', 'South', 'East', 'West'];
// return choices[Math.floor(Math.random()*4)];
return helpers.findNearestHealthWell (gameData);
};
var currentTurn = 0;
// Makes a new game with a 5x5 board
var game = new Game(5);
/**
* Sets up the game's enviroment and adds heroes, displays startup summary
*/
function gameSetup () {
// Add a health well in the middle of the board
game.addHealthWell(2,2);
// Add diamond mines on either side of the health well
game.addDiamondMine(2,1);
game.addDiamondMine(2,3);
// Add your hero in the top left corner of the map (team 0)
game.addHero(0, 0, 'MyHero', 0);
// Add an enemy hero in the bottom right corner of the map (team 1)
game.addHero(4, 4, 'Enemy', 1);
if (cliOptions.wait){ // wait mode
clearScreen();
}
console.log('About to start the game! Here is what the board looks like:');
// You can run game.board.inspect() in this test code at any time
// to log out the current state of the board (keep in mind that in the actual
// game, the game object will not have any functions on it)
game.board.inspect();
if (cliOptions.wait){ // wait mode
console.log();
console.log("Press ENTER to continue");
}
}
/**
* Runs the current turn
* param {integer} turn - the current turn of the game
*/
function runTurn (turn) {
var hero = game.activeHero;
var direction;
if (hero.name === 'MyHero') {
// Ask your hero brain which way it wants to move
direction = heroMoveFunction(game, helpers);
} else {
direction = enemyMoveFunction(game, helpers);
}
console.log('-----');
console.log('Turn ' + turn + ':');
console.log('-----');
console.log(hero.name + ' tried to move ' + direction);
console.log(hero.name + ' owns ' + hero.mineCount + ' diamond mines');
console.log(hero.name + ' has ' + hero.health + ' health');
game.handleHeroTurn(direction);
game.board.inspect();
if (cliOptions.wait && turn<cliOptions.turns){ // wait mode
console.log();
console.log("Press ENTER to continue");
}
}
/**
* Display summary of the game result
*/
function gameSummary () {
if (game.winningTeam === 0) {
console.log('You have won!');
} else if (game.winningTeam === 1) {
console.log('You have lost.');
} else {
console.log('The game has ended with no winner.');
}
}
/**
* End of game, what to do?
*/
function gameEnd () {
gameSummary();
process.exit();
}
// Utils helper functions
// Clears the console's screen
function clearScreen () {
process.stdout.write('\033c');
}
// Game Start
gameSetup();
// Run Turns
if (cliOptions.wait){ // wait mode
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.setEncoding('utf8');
process.stdin.on('keypress', (str, key) => {
if (key.name === 'return') {
if (currentTurn<cliOptions.turns){
clearScreen();
currentTurn++;
runTurn(currentTurn);
} else {
// Game ends
gameEnd();
}
} else if (key.ctrl && key.name==='c'){ // ctrl + c, exit immediately
console.log("Pressed Ctrl + C, exiting test-battle.js");
process.exit();
}
});
} else { // normal mode, runs in 1 go
for (currentTurn=1; currentTurn<=cliOptions.turns; currentTurn++) {
runTurn(currentTurn);
}
// Game ends
gameEnd();
}