Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Integrating It All


[ 312 ]

Grid.playCorner grid, this.player
else
Grid.playRandomCell grid, this.player
}
}
}
class EoghanPlayer extends Bot {
def playRound(grid) {
if (!Grid.isSolved()) {
if (Grid.canWin(grid, this.player))
Grid.playWin grid, this.player
else if (Grid.canBlock(grid, this.player))
Grid.playBlock grid, this.player
else if (Grid.canTakeCorner(grid, this.player))
Grid.playCorner grid, this.player
else if (Grid.canTakeCenter(grid, this.player))
Grid.playCenter grid, this.player
else
Grid.playRandomCell grid, this.player
}
}
}

Not bad for 10-12 year olds! The preceding code represents two TicTacToe player
bots that implement Nathan and Eoghan's strategies for playing the game. The class
relies heavily on the Grid class, which was a boilerplate we provided. Nathan and
Eoghan would also have helped us flesh out this class. We discovered early on that
coding the methods to detect whether the grid was solved or to play a blocking mode
were beyond the skill level of most students. However most were able to work out
how to implement Grid.canTakeCenter, Grid.playCenter, Grid.canTakeCorner,
and Grid.playCorner.


A game DSL for kids


By the closing weeks of term 2 we had quite a nice console-based TicTacToe game
built. The first of the kids were starting to complete functioning strategy classes and
we could play two strategies against each other in an automated game. It was time
to up the ante. The challenge now was to find a way to allow the kids to get more
return for their efforts. If you asked them what they would like to build, games and
something running on a mobile ranked high on their lists.


http://www.ebook3000.com
Free download pdf