Game Development Reference
In-Depth Information
68.
69. return dupeBoard
70.
71. def isSpaceFree(board, move):
72. # Return true if the passed move is free on the passed
board.
73. return board[move] == ' '
74.
75. def getPlayerMove(board):
76. # Let the player type in his move.
77. move = ' '
78. while move not in '1 2 3 4 5 6 7 8 9'.split() or not
isSpaceFree(board, int(move)):
79. print('What is your next move? (1-9)')
80. move = input()
81. return int(move)
82.
83. def chooseRandomMoveFromList(board, movesList):
84. # Returns a valid move from the passed list on the
passed board.
85. # Returns None if there is no valid move.
86. possibleMoves = []
87. for i in movesList:
88. if isSpaceFree(board, i):
89. possibleMoves.append(i)
90.
91. if len(possibleMoves) != 0:
92. return random.choice(possibleMoves)
93. else:
94. return None
95.
96. def getComputerMove(board, computerLetter):
97. # Given a board and the computer's letter, determine
where to move and return that move.
98. if computerLetter == 'X':
99. playerLetter = 'O'
100. else:
101. playerLetter = 'X'
102.
103. # Here is our algorithm for our Tic Tac Toe AI:
104. # First, check if we can win in the next move
105. for i in range(1, 10):
106. copy = getBoardCopy(board)
107. if isSpaceFree(copy, i):
108. makeMove(copy, computerLetter, i)
109. if isWinner(copy, computerLetter):
110. return i
111.
112. # Check if the player could win on his next move, and
block them.
113. for i in range(1, 10):
114. copy = getBoardCopy(board)
115. if isSpaceFree(copy, i):
116. makeMove(copy, playerLetter, i)
Search WWH ::




Custom Search