-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit.js
89 lines (76 loc) · 2.25 KB
/
rabbit.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
const garden = [[5, 7, 8, 6, 3],
[0, 0, 7, 0, 4],
[4, 6, 3, 4, 9],
[3, 1, 0, 5, 8]]
const AWAKE = 0;
const SLEEPING = 2;
rabbit = {
state:AWAKE,
carrots_eaten:0,
garden: garden,
col:0,
row:0,
moveTo:function(coords) {
this.col = coords.col;
this.row = coords.row;
},
eat:function() {
//don't attempt to do anything while rabbit is sleeping game over
if(this.state==SLEEPING) {
console.log("Game over the rabbit is sleeping");
return;
}
this.carrots_eaten += this.garden[this.row][this.col];
//the rabbit eat this carrot so there will be no left in this spot
this.garden[this.row][this.col] = 0;
this.move();
},
move: function(){
const maxInRow = get_max_carrots(this.garden,[this.row - 1, this.row + 1],[this.col]);
const maxInCols = get_max_carrots(this.garden,[this.row],[this.col-1, this.col+1]);
if(maxInRow.carrots > maxInCols.carrots) {
this.col = maxInRow.col;
this.row = maxInRow.row;
} else {
this.col = maxInCols.col;
this.row = maxInCols.row;
}
if(maxInRow.carrots==0 && maxInCols.carrots==0) {
this.state = SLEEPING;
}
}
};
function get_max_carrots(garden,centerRows,centerCols) {
maxCarrots = 0;
colInit = 0;
rowInit = 0;
for(i = 0 ; i< centerRows.length; i++) {
if(centerRows[i]<0) continue;
for(j = 0 ; j< centerCols.length; j++) {
if(centerCols[j]<0) continue;
carrotsAtPosition = garden[centerRows[i]][centerCols[j]];
if(carrotsAtPosition>maxCarrots){
maxCarrots=carrotsAtPosition;
rowInit = centerRows[i];
colInit = centerCols[j];
}
}
}
return {col:colInit, row:rowInit, carrots:maxCarrots};
}
function init(garden) {
const rowsNum = garden.length
const colsNum = garden[0].length
const centerCols = [Math.ceil(colsNum/2)-1];
const centerRows = [Math.ceil(rowsNum/2)-1];
if(rowsNum % 2 == 0)
centerRows.push(centerRows[0]+1);
if(colsNum % 2 == 0)
centerCols.push(centerRows[0]+1);
return get_max_carrots(garden, centerRows,centerCols);
}
initial_coords = init(garden);
rabbit.moveTo(initial_coords);
while(rabbit.state == AWAKE) {
rabbit.eat();
}