Chapter 12
[ 315 ]
event: "select_players"
playerX = event.playerX
playerO = event.playerO
page = "roundX"
event: "play_round"
def player = getPlayer(event.player)
player.playRound(grid)
if (Grid.isSolved(grid)) {
winner = event.player
page = "gameover"
} else {
if (event.player == 'X') {
page = "roundO"
} else {
page = "roundX"
}
}
event: "game_end"
page = "welcome"
The preceding DSL models the basic states and events of the TicTacToe game.
We refer to the states as pages since it is easier to explain to the kids how a state
corresponds to a page in the game. The state of the game is represented by whatever
state variables are declared in the state: block of the DSL.
The basic game engine mechanism is simple enough. We declare the pages in the
game. A page could be a simple start page or a step in a game. We declare the state
variables that the game relies on. Then we declare the events that the engine can
respond to. The DSL writer codes the logic of the game in the event blocks. We can
optionally assign a new value to the built-in page state variable in the logic. This will
cause the game to transition to that page. If no assignment is made, then the game
continues on the same page.