Information systems
Python_04022021
HAOYI WANG
275911
Iniziato giovedì, 4 febbraio 2021, 17:52
Terminato giovedì, 4 febbraio 2021, 19:22
Tempo impiegato 1 ora 30 min.
Valutazione 26,50 su un massimo di 60,00 (44%)
Informazione
1. import random
2.
3. class Board:
4. def __init__(self):
5. self.board_data = [‘ ‘] * 9
6.
7. self.winning_combos = (
8. [6, 7, 8], [3, 4, 5], [0, 1, 2], [0, 3, 6], [1, 4, 7], [2, 5, 8],
9. [0, 4, 8], [2, 4, 6],
10. )
11. self.corners = [0, 2, 6, 8]
12. self.sides = [1, 3, 5, 7]
13. self.middle = 4
14.
15. def clear(self):
16. self.board_data=[‘ ‘]*9
17.
18. def print_board(self, print_current_board=True):
19. if print_current_board is True:
20. print ‘| {} | {} | {} |’.format(self.board_data[0], self.board_data[1], self.board_data[2])
21. print ‘____________________’
22. print ‘| {} | {} | {} |’.format(self.board_data[3], self.board_data[4], self.board_data[5])
23. print ‘____________________’
24. print ‘| {} | {} | {} |’.format(self.board_data[6], self.board_data[7], self.board_data[8])
25. print ‘____________________’
26. else:
27. print ‘| 1 | 2 | 3 |’
28. print ‘____________________’
2/15/21 4:07 PM Pagina 1 di 10
29. print ‘| 4 | 5 | 6 |’
30. print ‘____________________’
31. print ‘| 7 | 8| 9 |’
32. print ‘____________________’
33.
34.
35. def is_winner(self, board, marker):
36. for combo in self.winning_combos:
37. if (self.board_data [combo[0]] == self.board_data [combo[1]] == self.board_data
[combo[2]] == marker):
38. return True
39. return False
40.
41. def copy_board(self, board):
42. copy = [‘ ‘] * 9
43. for i in range(9):
44. copy[i] = board[i]
45. return copy
46.
47. def get_bot_move(self, bot_marker, player_marker):
48.
49. for i in range(0, len(self.board_data)):
50. board_copy = self.copy_board(self.board_data)
51. if self.is_space_free(board_copy, i):
52. self.make_move(board_copy, i, bot_marker)
53. if self.is_winner(board_copy, bot_marker):
54. return i
55.
56. for i in range(0, len(self.board_data)):
57. board_copy = self.copy_board(self.board_data)
58. if self.is_space_free(board_copy, i):
59. self.make_move(board_copy, i, player_marker)
60. if self.is_winner(board_copy, player_marker):
61. return i
62.
63. move = self.choose_random_move(self.corners)
64. if move != None:
65. return move
66.
67. if self.is_space_free(self.board_data, self.middle):
68. return self.middle
69.
70. return self.choose_random_move(self.sides)
71.
72.
73. def is_space_free(self, board, index):
74. if (board[index] == ‘ ‘):
2/15/21 4:07 PM Pagina 2 di 10
75. return True
76. else:
77. return False
78.
79.
80. def is_board_full(self):
81. for i in range(1, 9):
82. if self.is_space_free(self.board_data, i):
83. return False
84. return True
85.
86. def make_move(self, board, index, move):
87. board[index] = move
88.
89. def choose_random_move(self, move_list):
90. possible_winning_moves = []
91. for index in move_list:
92. if self.is_space_free(self.board_data, index):
93. possible_winning_moves.append(index)
94. if len(possible_winning_moves) != 0:
95. return random.choice(possible_winning_moves)
96. else:
97. return None
98.
99.
100.
101. class Game:
102.
103. def __init__(self):
104. self.board = Board()
105. self.player_name = ‘Human’
106. self.player_marker = ”
107. self.bot_name = ‘TBot’
108. self.bot_marker = ”
109.
110. def get_marker(self):
111. marker = raw_input(“Would you like your marker to be X or Y?: “).upper()
112. while marker not in [“X”, “Y”]:
113. marker = raw_input(“Would you like your marker to be X or Y? :”).upper()
114. if marker == “X”:
115. return (‘X’, ‘Y’)
116. else:
117. return (‘Y’, ‘X’)
118.
119.
120. def help(self):
121. print ”’
2/15/21 4:07 PM Pagina 3 di 10
122. \n\t The game board has 9 squares (3X3).
123. \n\t Two players take turns in marking the spots/grids on the board.
124. \n\t The first player to have 3 pieces in a horizontal, vertical or diagonal row wins the game.
125. \n\t To place your mark in the desired square, simply type the number corresponding with the
square on the grid
126.
127. \n\t Press Ctrl + C to quit ”’
128.
129. def start_game(self):
130. self.board.print_board(False)
131. self.help()
132.
133. self.player_marker, self.bot_marker = self.get_marker()
134. print “Your marker is ” + self.player_marker
135.
136.
137. play_again = True
138.
139. while play_again:
140.
141. if random.randint(0, 1) == 0:
142. print “I will go first”
143. self.enter_game_loop(‘b’)
144. else:
145. print “You will go first”
146. self.enter_game_loop(‘h’)
147.
148. play_again = raw_input(“Would you like to play again? (y/n): “).lower()
149. if play_again == ‘y’:
150.
151. self.board.clear()
152. else:
153. print “\n\t– GAME OVER!!!–\n\t”
154. return
155.
156. def get_player_move(self):
157. move = int(input(“Pick a spot to move: (1-9) “))
158. while move not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not
self.board.is_space_free(self.board.board_data,move – 1):
159. move = int(input(“Invalid move. Please try again: (1-9) “))
160. return move – 1
161.
162.
163. def enter_game_loop(self, turn):
164. is_running = True
165. player = turn
166. while is_running:
2/15/21 4:07 PM Pagina 4 di 10
167. if player == ‘h’:
168. user_input = self.get_player_move()
169. self.board.make_move(self.board.board_data, user_input, self.player_marker)
170. if (self.board.is_winner(self.board.board_data,self.player_marker)):
171. self.board.print_board()
172. print “\n\tCONGRATULATIONS {}, YOU HAVE WON THE GAME!!!
\n”.format(self.player_name)
173.
174. is_running = False
175. else:
176. if self.board.is_board_full():
177. self.board.print_board(True)
178. print “\n\t– Match Draw –\t\n”
179. is_running = False
180.
181. else:
182. self.board.print_board(True)
183. player = ‘b’
184. else:
185. bot_move = self.board.get_bot_move(self.bot_marker,self.player_marker)
186. self.board.make_move(self.board.board_data,bot_move, self.bot_marker)
187.
188. if (self.board.is_winner(self.board.board_data, self.bot_marker)):
189. self.board.print_board(True)
190. print “\n\t%s HAS WON!!!!\t\n” % self.bot_name
191. is_running = False
192. break
193. else:
194. if self.board.is_board_full():
195. self.board.print_board(True)
196. print “\n\t — Match Draw — \n\t”
197. is_running = False
198. else:
199. self.board.print_board(True)
200. player = ‘h’
201.
202.
203. if __name__ == “__main__”:
204. TicTacToe = Game()
205. TicTacToe.start_game()
2/15/21 4:07 PM Pagina 5 di 10
Domanda 1
Completo
Punteggio ottenuto 2,00 su 8,00
Comment all classes and relevant methods.
Example comments:
The class “Game” implements….
The method __init__ performs…
There are two classes, one is the board class, which defines the specifications of the game and
the definition of the winner
and the second is the game class, which defines the attributes of the game.
Commento:
Domanda 2
Completo
Punteggio ottenuto 1,00 su 2,00
What is the purpose of this program
This program is propose to play a guessing number game. There are defines two types of players,
a bot player and a maker player. After defining the win condition of the board, the bot and the
maker enter 3 numbers 0-9. If the number appears in the dictionary, the maker wins, otherwise the
bot win.
Commento:
2/15/21 4:07 PM Pagina 6 di 10
Domanda 3
Completo
Punteggio ottenuto 2,50 su 2,50
1. Why is is_running set to True in the method enter_game_loop (line 170)? The related code
snippet is reported below:
def enter_game_loop(self, turn):
is_running = True
player = turn
while is_running:
if player == ‘h’:
user_input = self.get_player_move()
self.board.make_move(self.board.board_data, user_input, self.player_marker)
because the runing means play game again.
Commento:
Domanda 4
Completo
Punteggio ottenuto 1,50 su 2,50
. What is the relationship between the parameter turn and the local variable player in function
enter_game_loop?
Turn is used as the return times and return value parameters to show whether the player needs to
play the game again
Commento:
2/15/21 4:07 PM Pagina 7 di 10
Domanda 5
Completo
Punteggio ottenuto 1,00 su 2,50
1. What is the role of the method __init__() of the class Board (line 4)?
Init defined the qwn basic attributes of the Board class . The initl here sets the basic specifications
of the chess game, and set the 9×9 yield
Commento:
Domanda 6
Completo
Punteggio ottenuto 0,50 su 2,50
1. How does the function choose_random_move in class Board work?
The random function is imported in first line. and the random function here provides random data
for the class.
Commento:
2/15/21 4:07 PM Pagina 8 di 10
Domanda 7
Completo
Punteggio ottenuto 3,00 su 10,00
Draw the use case diagram for the program and attach it to this question
use case.png
Commento:
hierarchy
include/extend
quit
Domanda 8
Completo
Punteggio ottenuto 8,00 su 10,00
Draw the class diagram for the program and attach it to this question
class.png
Commento:
random, randint
self calls
2/15/21 4:07 PM Pagina 9 di 10
Domanda 9
Completo
Punteggio ottenuto 7,00 su 20,00
Draw the sequence diagram for the program and attach it to this question
sequence.png
Commento:
da correggere
largely incomplete
R1/2
B5/10
G2/6
2/15/21 4:07 PM Pagina 10 di 10