# X x x x x x x x x x x x x x x x x o o o o o o o o x o

• 316

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 187 - 189 out of 316 pages.

xxxxxxxxxxxxxxxxxooooooooxoxoxoxoxoxoxoxxxxxxx. . .. . .. . .TTTI CCAOEGAME TREExoxxxo o. . .These aregamestructures.These aremovestructures.KEYSNow we use this tree to play a game of tic-tac-toe. Player X starts out with nine pos-sible moves. She chooses to place her X in the upper-left corner. As a result, we focus onlyon the leftmost branch of the tree, leaving player O with eight possibilities. Say player Othen marks the spot directly next to the X. This in turn means we again choose the left-most branch. We keep pruning the tree like this until the game is over, which happenswhen one player wins or all ields are marked.Generating the tic-tac-toe tree is reasonably straightforward. Suppose we have a datarepresentation for the two players, X and O, and we represent the tree itself withtttnodes:(struct ttt (board moves))
170Chapter 10Each such node contains the current state of the board and the list of possible moves. Eachmove is a two-element list that combines an action with the resulting game tree, where anaction is just another structure:(struct action (player position))It records the player that takes the action and her chosen token placement.The tree-generating function consumes the two players and produces a completegame tree starting from an empty board:(define (generate-ttt-tree player1 player2)(define (generate-tree board player opponent)(ttt board (generate-moves board player opponent)))(define (generate-moves board0 player opponent)(define free-fields (board-find-free-fields board0))(for/list ((f free-fields))(define actnow (action player f))(define board1 (board-take-field board0 player f))(list actnow (generate-tree board1 opponent player))));; -- start here --(generate-tree the-empty-board player1 player2))As you can see, the function introduces two auxiliary functions:generate-treeandgenerate-moves. The irst one generates a tree node from the current state of the boardand two players, assuming the irst player takes an action. The second function gener-ates the list of possible moves from the current board and the two players, still assumingthat the irst player is the active one.From the deinition, you can see thatgenerate-treeis straightforward. It cre-ates the node from the current board and generates the possible moves with the secondfunction. In contrast, thegenerate-movesfunction must iterate over all free ields,which it inds withboard-find-free-fields, a function not shown here. For each ofthe free ields, the loop creates an action for the current player and the free ield. Thenit computes withboard-take-fieldthe efect of the action on the current board. Theresult of an iteration is a two-element list that pairs the action with the resulting tree,which is determined via a recursive call togenerate-tree. When the loop has inished,generate-movesreturns an entire list of such two-element lists.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 316 pages?

Course Hero member to access this document

Term
Spring
Professor
professor_unknown
Tags