1. Using the Python class below, write the recursive function CreateAllBoards(layout) :
''' Layout positions:
0 1 2
3 4 5
6 7 8
'''
# layouts look like "_x_ox__o_"
Wins =
[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
AllBoards = {} # this is a dictionary with key = a
layout, and value = its corresponding BoardNode
class BoardNode:
def __init__(self,layout):
self.layout
= layout
self.endState = None # if this is a terminal board, endState == 'x' or 'o' for
wins, of 'd' for draw, else None
self.children = [] # all layouts that can be reached with a single move
def print_me(self):
print
('layout:',self.layout, 'endState:',self.endState)
print
('children:',self.children)
def CreateAllBoards(layout,parent):
# recursive function to manufacture all BoardNode nodes and
place them into the AllBoards dictionary
2. Now manufacture all the boards using CreateAllBoards('_________')
3. Make sure that the number of elements in AllBoards is what you calculated in the first homework, namely 5,478. Check this using print(len(AllBoards))
4. As a further check, add up the number of children for all of the BoardNodes inside AllBoards (yes, that's a huge number of duplications, but it's a check that you've done it right). Place that number into the Comments-to-Teacher, and also submit your code file.
5. Additionally, calculate and report to the Comments-to-Teacher: the number of boards in AllBoards that end in x-winning, o-winning, draws, and the number of boards that are not ends-of-games. This, also, should match the calculations you got from the previous homework. Also submit any comments on the homework itself, as usual.
6. Remember to submit your code file.