forked from scribd/robot-fruit-hunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabot.js
131 lines (106 loc) · 3.01 KB
/
abot.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
//declare new variables
var NORTH_WEST = 111;
var NORTH_EAST = 222;
var SOUTH_WEST = 333;
var SOUTH_EAST = 444;
//check if grid is beyond boundaries of the board
function is_at_edge(pos) {
var board = get_board();
if (pos.x > (board.length-1) || pos.x < 0 ||
pos.y > (board[0].length-1) || pos.y < 0) {
return true;
}
return false;
};
//return the fruit type has the highest number
function count_of_each_type() {
var no_of_types = get_number_of_item_types();
var fruit_type_with_max_count = -1;
var curr_type_max_count = -1;
for(var i=1; i<=no_of_types; i++) {
var type_count = get_total_item_count(i);
if (type_count > curr_type_max_count) {
fruit_type_with_max_count = i;
curr_type_max_count = type_count;
}
}
return fruit_type_to_collect;
};
//return grids by radius
function get_grids_by_radius(x, y, rad) {
var top_left = {x: x-rad, y: y-rad}; //top left
var sq_len = 3 + (2*(rad-1));
var grids = [];
//top
for (var i=1; i<=sq_len; i++) {
grids.push({x: (x-rad)+(i-1), y: y-rad});
}
//bottom
for (var i=1; i<=sq_len; i++) {
grids.push({x: (x-rad)+(i-1), y: y+1});
}
//left
for (var i=2; i<=sq_len-1; i++) {
grids.push({x: x-rad, y: y-rad+(i-1)});
}
//right
for (var i=2; i<=sq_len-1; i++) {
grids.push({x: x+rad, y: y-rad+(i-1)});
}
return grids;
};
function direction_to_target(x, y, target_x, target_y) {
if (target_x == x && target_y > y) {
return SOUTH;
} else if (target_x == x && target_y < y) {
return NORTH;
} else if (target_x > x && target_y == y) {
return EAST;
} else if (target_x < x && target_y == y) {
return WEST;
} else if (target_x < x && target_y > y) {
if (get_random_boolean()) { return SOUTH } else { return WEST };
} else if (target_x > x && target_y > y) {
if (get_random_boolean()) { return SOUTH } else { return EAST };
} else if (target_x > x && target_y < y) {
if (get_random_boolean()) { return NORTH } else { return EAST };
} else if (target_x < x && target_y < y) {
if (get_random_boolean()) { return NORTH } else { return WEST };
}
return EAST; //default
}
function get_random_boolean() {
var r = Math.floor((Math.random()*2)+1);
if (r == 1) {
return false;
}
return true;
}
function new_game() {
}
function make_move() {
var board = get_board();
// we found an item! take it!
if (board[get_my_x()][get_my_y()] > 0) {
return TAKE;
}
var direction_to_move = PASS; //by default
//search the radius(es)
for (var rad=1; rad<=20; rad++) {
var has_found_fruit = false;
grids = get_grids_by_radius(get_my_x(), get_my_y(), rad);
for (var j=0; j<grids.length; j++) {
var g = grids[j];
if (!is_at_edge(g) && has_item(get_board()[g.x][g.y])) {
direction_to_move = direction_to_target(get_my_x(), get_my_y(), g.x, g.y);
has_found_fruit = true;
break;
}
}
if (has_found_fruit) {
break;
}
}
trace("direction_to_move: " + direction_to_move);
return direction_to_move;
}