forked from hola/challenge_jsdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
35 lines (33 loc) · 899 Bytes
/
example.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
'use strict'; /*jslint node:true*/
function find_player(screen){
for (let y = 0; y<screen.length; y++)
{
let row = screen[y];
for (let x = 0; x<row.length; x++)
{
if (row[x]=='A')
return {x, y};
}
}
}
exports.play = function*(screen){
while (true){
let {x, y} = find_player(screen);
let moves = '';
if (' :*'.includes(screen[y-1][x]))
moves += 'u';
if (' :*'.includes(screen[y+1][x]))
moves += 'd';
if (' :*'.includes(screen[y][x+1])
|| screen[y][x+1]=='O' && screen[y][x+2]==' ')
{
moves += 'r';
}
if (' :*'.includes(screen[y][x-1])
|| screen[y][x-1]=='O' && screen[y][x-2]==' ')
{
moves += 'l';
}
yield moves[Math.floor(Math.random()*moves.length)];
}
};