[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
def checkDraw(self, board=None):
board = board or self.board
for row in board:
if Empty in row:
return 0
return 1 # none empty: draw or win

def checkWin(self, mark, board=None):
board = board or self.board
for row in board:
if row.count(mark) == self.degree: # check across
return 1
for col in range(self.degree):
for row in board: # check down
if row[col] != mark:
break
else:
return 1
for row in range(self.degree): # check diag1
col = row # row == col
if board[row][col] != mark: break
else:
return 1
for row in range(self.degree): # check diag2
col = (self.degree-1) - row # row+col = degree-1
if board[row][col] != mark: break
else:
return 1

def checkFinish(self):
if self.checkWin(self.userMark):
outcome = "You've won!"
elif self.checkWin(self.machineMark):
outcome = 'I win again :-)'
elif self.checkDraw():
outcome = 'Looks like a draw'

Other move-selection code mostly just performs other kinds of analysis on the board
data structure or generates new board states to search a tree of moves and
countermoves.


You’ll also find relatives of these files in the same directory that implements alternative
search and move-scoring schemes, different board representations, and so on. For ad-
ditional background on game scoring and searches in general, consult an AI text. It’s
fun stuff, but it’s too specialized to cover well in this book.


Where to Go from Here


This concludes the GUI section of this book, but this is not an end to the book’s GUI
coverage. If you want to learn more about GUIs, be sure to see the tkinter examples
that appear later in this book and are described at the start of this chapter. PyMailGUI,
PyCalc, and the mostly external PyForm and PyTree provide additional GUI case


766 | Chapter 11: Complete GUI Programs

Free download pdf