def __init__(self, name, shape, board, start): if functions.is_string(name) and functions.is_string(shape): self.shape = variables.Iam[shape].value self.name = name self.board = board self.turn_to_play = start self.opponent = None
def _add_label(self, text, color="black", font=0): if functions.is_number(font) and functions.is_string( text) and functions.is_string(color): return tkinter.Label(self, text=text, font=(variables.TEXT_FONT, variables.LABEL_TEXT_SIZE + font), fg=color)
def test_incorrect_string(self): invalid_input_1 = 1 invalid_input_2 = ["this is so wrong."] invalid_input_3 = {0: "And also this."} invalid_input_4 = None invalid_input_5 = 1.0 expected_output = False self.assertEqual(expected_output, functions.is_string(invalid_input_1)) self.assertEqual(expected_output, functions.is_string(invalid_input_2)) self.assertEqual(expected_output, functions.is_string(invalid_input_3)) self.assertEqual(expected_output, functions.is_string(invalid_input_4)) self.assertEqual(expected_output, functions.is_string(invalid_input_5))
def __init__(self, socket, msg, header_type): if functions.is_string(msg): self.msg = msg else: self.msg = str(msg) self.socket = socket self.header_type = header_type
def _process_change_field(self, num): if functions.is_string(num) and functions.is_number(int(num)): opponent_name = self.player.opponent.name if int(num) == ResponseCode.Ok.value: # opponent accept print("changed field size") self.player.board.gui.set_field_size(self.new_field_size) num = self.player.board.gui.playing_field.restart() self.player.board.playing_field.initialize_field(num) elif int( num ) == ResponseCode.Not_Ok.value: # opponent is not cool with a field change print("didn't change size") true_size = int( math.sqrt( self.player.board.playing_field.get_number_of_cells())) self.player.board.gui.set_field_size(true_size) messenger.SendToUser.show_info( "Message", opponent_name + " declined your request for changing field size to " + str(self.new_field_size)) elif 3 <= int(num) <= 5: # field size can only be from 3 to 5 agree = messenger.SendToUser.ask_for_field_change( opponent_name + " want to change field size to " + str(num) + ".\nDo you agree?") if agree: print("I agree to change size") mailman.ChangeFieldSizeRequest( self.sock, ResponseCode.Ok.value).send() self.new_field_size = int(num) else: print("I disagree to change size") mailman.ChangeFieldSizeRequest( self.sock, ResponseCode.Not_Ok.value).send()
def _add_button(self, text, funct): if functions.is_string(text): return tkinter.Button(self, text=text, padx=20, bg="white", bd=1, command=funct)
def _process_change_size(self, client_sock, msg_rec): if functions.is_string(msg_rec) and functions.is_number(int(msg_rec)): print("Trying to change field size") other_sock = self._get_other_sock(client_sock) if int(msg_rec) == ResponseCode.Ok.value: mailman.ChangeFieldSizeResponse(other_sock, str(ResponseCode.Ok.value)).send() mailman.ChangeFieldSizeResponse(client_sock, str(ResponseCode.Ok.value)).send() elif int(msg_rec) == ResponseCode.Not_Ok.value: mailman.ChangeFieldSizeResponse(other_sock, str(ResponseCode.Not_Ok.value)).send() else: mailman.ChangeFieldSizeResponse(other_sock, msg_rec).send()
def _process_message(self, client_sock, msg_rec): if functions.is_string(msg_rec[1]): header_type = int(msg_rec[0]) if header_type == Headers.Request_join.value: self._process_join(client_sock, str(msg_rec[1])) elif header_type == Headers.Request_click.value: self._process_click(client_sock, msg_rec[1]) elif header_type == Headers.Request_leave.value: self._process_leave(client_sock, msg_rec[1]) elif header_type == Headers.Request_change_size.value: self._process_change_size(client_sock, msg_rec[1]) else: print("I don't know this header: " + msg_rec)
def _process_join(self, msg_txt): if functions.is_string(msg_txt): if msg_txt == str(ResponseCode.Ok.value): # can play print("I,", self.player.name, ", have joined the game.") elif msg_txt == str(ResponseCode.Not_Ok.value): # game is full messenger.SendToUser.error( "There is already two players in the game!") exit(0) else: # initialize who will start data = msg_txt.split("\\") start = False if data[0] == "True": start = True self._configure_game(start, data[1])
def _process_join(self, sock, msg): if functions.is_string(msg): print("Processing join...") if len(self.clients) < 2: # only for two players print("Accepted player " + msg) self.clients[sock] = msg # add player mailman.JoinResponse(sock, ResponseCode.Ok.value).send() if len(self.clients) == 2: # initialize players print("All players are ready to play") players = list(self.clients.keys()) print(self.clients[players[1]], "start to play") mailman.JoinResponse(players[0], str("True\\" + self.clients[players[1]])).send() # first one start mailman.JoinResponse(players[1], str("False\\" + self.clients[players[0]])).send() # second wait else: print("Declined player " + msg) mailman.JoinResponse(sock, ResponseCode.Not_Ok.value).send()
def ask_for_new_game(text): if functions.is_string(text): return mb.askyesno("Game is over!", text + "\nDo you want to play another game?") raise ValueError(str(text) + " is not a string!")
def show_info(title, text): if functions.is_string(title) and functions.is_string(text): mb.showinfo(title, text) return True raise ValueError(str(text) + " is not a string!")
def ask_for_field_change(text): if functions.is_string(text): return mb.askyesno("Request from opponent", text) raise ValueError(str(text) + " is not a string!")
def error(text): if functions.is_string(text): mb.showerror("Error!", text) return True raise ValueError(str(text) + " is not a string!")
def test_correct_string(self): valid_input = "This is the correct input." expected_output = True self.assertEqual(expected_output, functions.is_string(valid_input))
def change_response_text(self, text): if functions.is_string(text): self.elements[3].config(text=text) return text raise ValueError(str(text) + " is not a string!")
def _process_leave(self, client_sock, msg_rec): if functions.is_string(msg_rec): print(self.clients[client_sock], "is leaving") other_sock = self._get_other_sock(client_sock) mailman.LeaveResponse(other_sock, msg_rec).send()
def _add_editbox(self, text, width=5): if functions.is_number(width) and functions.is_string(text.get()): return tkinter.Entry(self, text=text, justify="center", width=width)
def play(name): if functions.is_string(name): GuessingGame(name).gui.mainloop() else: raise ValueError(str(name) + " is not a string!")