Форум сайта python.su
0
Нужно библиотеку вставить в код программы “Крестики нолики”
Код программы:
import random
2.
3. def displayBoard(board):
4. print('\n\t', board, ‘|’, board, ‘|’, board)
5. print('\t', “———”)
6. print('\t', board, ‘|’, board, ‘|’, board)
7. print('\t', "———')
8. print('\t', board, ‘|’, board, ‘|’, board, ‘\n’)
9.
10. def inputPlayerLetter():
11. letter = ‘'
12. while not (letter == ’X' or letter == ‘O’):
13. print('Введите знак, которым будете играть: X или O (латинский символ)')
14. letter = input().upper()
15. if letter == ‘X’:
16. return
17. else:
18. return
19.
20. def defineFirstMove(letter):
21. if letter == ‘O’:
22. return ‘компьютер’
23. else:
24. return ‘человек’
25.
26. def playAgain():
27. print('Хотите сыграть еще раз? (yes/no)')
28. return input().lower().startswith('y')
29.
30. def makeMove(board, letter, move):
31. board = letter
32.
33. def isWinner(bo, le):
34. return ((bo == le and bo == le and bo == le) or
35. (bo == le and bo == le and bo == le) or
36. (bo == le and bo == le and bo == le) or
37. (bo == le and bo == le and bo == le) or
38. (bo == le and bo == le and bo == le) or
39. (bo == le and bo == le and bo == le) or
40. (bo == le and bo == le and bo == le) or
41. (bo == le and bo == le and bo == le))
42.
43. def getBoardCopy(board):
44. dupeBoard =
45.
46. for i in board:
47. dupeBoard.append(i)
48.
49. return dupeBoard
50.
51. def isSpaceFree(board, move):
52. return board == ‘ ’
53.
54. def getPlayerMove(board):
55. move = ‘ ’
56. while move not in ‘1 2 3 4 5 6 7 8 9’.split() or not isSpaceFree(board, int(move)):
57. print('Каким будет ваш следующий ход? (1-9)')
58. move = input()
59. return int(move)
60.
61. def chooseRandomMoveFromList(board, movesList):
15
62. possibleMoves =
63. for i in movesList:
64. if isSpaceFree(board, i):
65. possibleMoves.append(i)
66.
67. if len(possibleMoves) != 0:
68. return random.choice(possibleMoves)
69. else:
70. return None
71.
72. def getComputerMove(board, computerLetter):
73. if computerLetter == ‘X’:
74. playerLetter = ‘O’
75. else:
76. playerLetter = ‘X’
77.
78. for i in range(1, 10):
79. copy = getBoardCopy(board)
80. if isSpaceFree(copy, i):
81. makeMove(copy, computerLetter, i)
82. if isWinner(copy, computerLetter):
83. return i
84.
85. for i in range(1, 10):
86. copy = getBoardCopy(board)
87. if isSpaceFree(copy, i):
88. makeMove(copy, playerLetter, i)
89. if isWinner(copy, playerLetter):
90. return i
91.
92. move = chooseRandomMoveFromList(board, )
93. if move != None:
94. return move
95.
96. if isSpaceFree(board, 5):
97. return 5
98.
99. return chooseRandomMoveFromList(board, )
100.
101. def isBoardFull(board):
102. for i in range(1, 10):
103. if isSpaceFree(board, i):
104. return False
105. return True
106.
107. def display_instruct():
108. print(
109. ‘'’
110. Чтобы сделать ход, введите число от 1 до 9. Числа однозначно соответствуют полям
111. доски, как показано ниже:\n
112. \t7 | 8 | 9
113. \t———
114. \t4 | 5 | 6
115. \t———
116. \t1 | 2 | 3
117. ‘'’
118. )
119.
120.
121. print('Добро пожаловать в игру “крестики-нолики”!')
122.
123. while True:
124. theBoard = * 10
125. playerLetter, computerLetter = inputPlayerLetter()
126. turn = defineFirstMove(playerLetter)
127. gameIsPlaying = True
128.
16
129. while gameIsPlaying:
130. if turn == ‘человек’:
131. display_instruct()
132. displayBoard(theBoard)
133. move = getPlayerMove(theBoard)
134. makeMove(theBoard, playerLetter, move)
135.
136. if isWinner(theBoard, playerLetter):
137. displayBoard(theBoard)
138. print('Вы выиграли!')
139. gameIsPlaying = False
140. else:
141. if isBoardFull(theBoard):
142. displayBoard(theBoard)
143. print('Ничья!')
144. break
145. else:
146. turn = ‘компьютер’
147.
148. else:
149. move = getComputerMove(theBoard, computerLetter)
150. makeMove(theBoard, computerLetter, move)
151.
152. if isWinner(theBoard, computerLetter):
153. displayBoard(theBoard)
154. print('Вы проиграли!')
155. gameIsPlaying = False
156. else:
157. if isBoardFull(theBoard):
158. displayBoard(theBoard)
159. print('Ничья!')
160. break
161. else:
162. turn = ‘человек’
163.
164. if not playAgain():
165. break
Офлайн
73
Офлайн