113 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */ |
113 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */ |
114 ai.nextMaxScore.prototype.cleanup = function() { } |
114 ai.nextMaxScore.prototype.cleanup = function() { } |
115 |
115 |
116 |
116 |
117 //////////////////////////////////////////////////////////////// |
117 //////////////////////////////////////////////////////////////// |
118 // N level deep on score value without random. |
118 // N level deep on score value without random simulation. |
119 //////////////////////////////////////////////////////////////// |
119 //////////////////////////////////////////////////////////////// |
120 |
120 |
121 ai.deepMaxScore = function(brdEngine) { |
121 ai.deepMaxScore = function(brdEngine) { |
122 this.brdEngine = brdEngine; |
122 this.brdEngine = brdEngine; |
123 } |
123 } |
|
124 ai.deepMaxScore.dirs = ["up", "down", "left", "right"]; |
124 ai.deepMaxScore.prototype.analyse = function(brd) { |
125 ai.deepMaxScore.prototype.analyse = function(brd) { |
125 var origBrd = new this.brdEngine(brd); |
126 var origBrd = new this.brdEngine(brd); |
126 var nextBrd = new this.brdEngine(); |
127 var nextBrd = new this.brdEngine(); |
|
128 var prevScore = -1, nextScore = -1; |
127 var maxScore = -1; |
129 var maxScore = -1; |
128 var action; |
130 var bestDir; |
129 if (origBrd.up(nextBrd)) { |
131 for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) { |
130 maxScore = this.bestScore(nextBrd); |
132 var dir = ai.deepMaxScore.dirs[i]; |
131 action = "up"; |
133 if (origBrd[dir](nextBrd)) { |
132 } |
134 nextScore = nextBrd.score(); |
133 if (origBrd.left(nextBrd)) { |
135 var score = this.bestScore(nextBrd); |
134 var score = this.bestScore(nextBrd); |
136 // console.log("dir: %o, prevScore: %o, nextScore: %o, maxScore: %o, score: %o", dir, prevScore, nextScore, maxScore, score); |
135 if (maxScore < score) { |
137 if (maxScore < score || (maxScore === score && prevScore < nextScore)) { |
136 action = "left"; |
138 prevScore = nextScore; |
137 maxScore = score; |
139 maxScore = score; |
|
140 bestDir = dir; |
|
141 } |
138 } |
142 } |
139 } |
143 } |
140 if (origBrd.down(nextBrd)) { |
144 return bestDir; |
141 var score = this.bestScore(nextBrd); |
|
142 if (maxScore < score) { |
|
143 action = "down"; |
|
144 maxScore = score; |
|
145 } |
|
146 } |
|
147 if (origBrd.right(nextBrd)) { |
|
148 var score = this.bestScore(nextBrd); |
|
149 if (maxScore < score) { |
|
150 action = "right"; |
|
151 maxScore = score; |
|
152 } |
|
153 } |
|
154 return action; |
|
155 } |
145 } |
156 ai.deepMaxScore.prototype.bestScore = function(brd, seenBrds) { |
146 ai.deepMaxScore.prototype.bestScore = function(brd, seenBrds) { |
157 if (seenBrds) { |
147 if (seenBrds) { |
158 for (var i = 0; i < seenBrds.length; i++) |
148 for (var i = 0; i < seenBrds.length; i++) |
159 if (brd.equals(seenBrds[i])) |
149 if (brd.equals(seenBrds[i])) |
160 return 0; |
150 return 0; |
161 } else { |
151 } else { |
162 seenBrds = []; |
152 seenBrds = []; |
163 } |
153 } |
164 seenBrds.push(brd); |
154 seenBrds.push(brd); |
165 var maxScore = brd.score(); |
155 var currScore = brd.score(); |
|
156 var maxScore = currScore; |
166 var nextBrd = new this.brdEngine(); |
157 var nextBrd = new this.brdEngine(); |
167 if (brd.up(nextBrd)) |
158 for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) { |
168 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1); |
159 if (brd[ai.deepMaxScore.dirs[i]](nextBrd)) { |
169 if (brd.down(nextBrd)) |
160 var score = nextBrd.score(); |
170 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1); |
161 if (score > currScore) |
171 if (brd.left(nextBrd)) |
162 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds)); |
172 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1); |
163 } |
173 if (brd.right(nextBrd)) |
164 } |
174 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1); |
|
175 return maxScore; |
165 return maxScore; |
176 } |
166 } |
177 |
167 |