-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFooController.groovy
37 lines (32 loc) · 1.21 KB
/
FooController.groovy
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
package fregepluginapp
class FooController {
static scope = "session"
def computerBegins = true // computer begins
int[] board = [0] * 9
def ttt() {
// println params
def forecast = [0] * 9
def gameover = false
int lookahead = params.lookahead != null ? params.lookahead.toInteger() : 3
if (params.newGame){
board = [0] * 9
if (computerBegins) {
int randomStartPos = Math.random() * 8
board[randomStartPos] = 1 // first mark in the game randomly by computer (X)
}
computerBegins = !computerBegins // toggle who begins next
} else {
if (params.index != null) {
board[params.index.toInteger()] = -1 // human has placed mark (O)
def calculated = Minimax.nextBoard(lookahead, board)
if (calculated) {
forecast = Minimax.forecast(lookahead, board)
board = calculated
} else {
gameover = true
}
}
}
render view: 'edit', model: [board: board, forecast:forecast, gameover: gameover, lookahead: lookahead]
}
}