Game Development Reference
In-Depth Information
10 - Tic Tac Toe
27. letter = input().upper()
28.
29. # the first element in the tuple is the player's
letter, the second is the computer's letter.
30. if letter == 'X':
31. return ['X', 'O']
32. else:
33. return ['O', 'X']
34.
35. def whoGoesFirst():
36. # Randomly choose the player who goes first.
37. if random.randint(0, 1) == 0:
38. return 'computer'
39. else:
40. return 'player'
41.
42. def playAgain():
43. # This function returns True if the player wants to
play again, otherwise it returns False.
44. print('Do you want to play again? (yes or no)')
45. return input().lower().startswith('y')
46.
47. def makeMove(board, letter, move):
48. board[move] = letter
49.
50. def isWinner(bo, le):
51. # Given a board and a player's letter, this function
returns True if that player has won.
52. # We use bo instead of board and le instead of letter
so we don't have to type as much.
53. return ((bo[7] == le and bo[8] == le and bo[9] == le)
or # across the top
54. (bo[4] == le and bo[5] == le and bo[6] == le) or #
across the middle
55. (bo[1] == le and bo[2] == le and bo[3] == le) or #
across the bottom
56. (bo[7] == le and bo[4] == le and bo[1] == le) or #
down the left side
57. (bo[8] == le and bo[5] == le and bo[2] == le) or #
down the middle
58. (bo[9] == le and bo[6] == le and bo[3] == le) or #
down the right side
59. (bo[7] == le and bo[5] == le and bo[3] == le) or #
diagonal
60. (bo[9] == le and bo[5] == le and bo[1] == le)) #
diagonal
61.
62. def getBoardCopy(board):
63. # Make a duplicate of the board list and return it the
duplicate.
64. dupeBoard = []
65.
66. for i in board:
67. dupeBoard.append(i)
Search WWH ::




Custom Search