class Console: def _get_move(self): while True: try: row = int(input("row: ")) col = int(input("col: ")) return Square(row, col) except Exception as e: print("Invalid move! -> " + str(e)) def start(self): player_move = True while True: try: rows = int(input("Number of rows for the board: ")) columns = int(input("Number of columns for the board: ")) if rows < 2 or rows > 20 or columns < 2 or columns > 20: raise GameException( "The number of rows and columns must be between 2 and 20!" ) self.__game = Game([rows, columns]) break except Exception as e: print(str(e)) b = self.__game.board while b.won() == False: try: if player_move: print(b) move = self._get_move() self.__game.human_move(move) else: self.__game.computer_move() player_move = not player_move except Exception as e: print(str(e)) print("Game over!") print(b) if player_move == True: print("Computer wins!") else: print("Player wins!")
class GUI: def start(self): self.main_window = Tk() self.main_window.title("Obstruction - ") self.main_window.configure(background="thistle2") #Creating entries for the board size self.frame_1 = Frame(self.main_window) self.frame_1.pack(side=TOP) self.l_row = Label(self.frame_1, text="Nr. Rows: ", bg="thistle2") self.l_row.pack(side=LEFT) self.row_entr = Entry(self.frame_1, width=10) self.row_entr.pack(side=LEFT) self.l_col = Label(self.frame_1, text="Nr. Columns: ", bg="thistle2") self.l_col.pack(side=LEFT) self.col_entr = Entry(self.frame_1, width=10) self.col_entr.pack(side=LEFT) #Creating the board self.frame_play = Frame(self.main_window) self.frame_play.pack() self.play_btn = Button(self.frame_play, compound = CENTER,\ text = "START GAME",\ bg = "thistle1", \ command = self._play) self.play_btn.pack() self.main_window.mainloop() def _play(self): self.frame_2 = Frame(self.main_window) self.frame_2.pack() try: self._rows = int(self.row_entr.get()) self._columns = int(self.col_entr.get()) if self._rows < 2 or self._rows > 20 or self._columns < 2 or self._columns > 20: raise GameException("This size of the board is not allowed!") self.frame_1.pack_forget() self.frame_play.pack_forget() self._game = Game([self._rows, self._columns]) self.buttons = [] count = 0 for row in range(self._rows): for col in range(self._columns): self.buttons.append(Button(self.frame_2,text = " ",\ width = 3, height = 1, bg = "thistle2",\ command = lambda col = col,row = row : self._move(row,col) )) self.buttons[count].grid(column=col, row=row) count += 1 except Exception as e: messagebox.showerror("Error!", str(e)) def _move(self, row, col): try: self._game.human_move(Square(row, col)) self._show_change() if self._game.board.won(): messagebox.showinfo("Game won!", "Congratulation YOU WON this game!") self.main_window.destroy() else: self._game.computer_move() self._show_change() if self._game.board.won(): messagebox.showinfo("Game won!", "The Computer WON this game!") self.main_window.destroy() except Exception as e: messagebox.showerror("Error!", str(e)) def _show_change(self): count = 0 for r in range(self._rows): for c in range(self._columns): if self._game.board.get_square(r, c) == ".": self.buttons[count].config(bg="thistle4") else: self.buttons[count].config( text=self._game.board.get_square(r, c)) count += 1
class Test_Game(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.__rows = 4 self.__columns = 5 self.__10board = Board(10, 10) self.__board = Board(self.__rows, self.__columns) self.__game = Game([self.__rows, self.__columns]) self.__sq1 = Square(1, 1) self.__sq2 = Square(2, 2) self.__sq34 = Square(3, 4) self.__sq45 = Square(4, 5) def tearDown(self): unittest.TestCase.tearDown(self) def test_board(self): self.assertEquals(len(self.__10board.empty_squares()), 10 * 10) self.assertEquals(len(self.__board.empty_squares()), self.__rows * self.__columns) self.assertEquals(self.__board.get_square(2, 3), " ") self.assertEquals(self.__board.won(), False) self.__board.move(Square(2, 3), "X") self.assertEquals(self.__board.get_square(2, 3), "X") self.assertEquals(len(self.__board.empty_squares()), 11) self.assertEquals(self.__board.get_square(1, 2), ".") try: self.__board.move(Square(2, 3), "X") assert False except GameException as ge: self.assertEquals(str(ge), "Square already taken!") self.__board.move(Square(2, 3), "E") self.assertEquals(self.__board.get_square(2, 3), " ") self.assertEquals(len(self.__board.empty_squares()), 4 * 5) self.assertEquals(self.__board.get_square(1, 2), " ") def test_game(self): self.assertEquals(len(self.__game.board.empty_squares()), self.__rows * self.__columns) self.__game.board.move(self.__sq1, "X") self.assertEquals(len(self.__game.board.empty_squares()), 11) try: self.__game.board.move(self.__sq45, "X") assert False except GameException as ge: self.assertEquals(str(ge), "Move outside board!") self.__game.board.move(self.__sq34, "O") self.assertEquals(len(self.__game.board.empty_squares()), 7) self.__game.board.move(Square(0, 4), "X") board = self.__game.board.copy() best_move = self.__game.computer_AI(board, "computer", 0, -100, 100) self.assertEquals(str(best_move[0]), str(Square(3, 0))) self.__game.computer_move() self.assertEquals(self.__game.board.get_square(3, 0), "O") self.__game.board.move(Square(3, 2), "X") self.assertEquals(self.__game.board.won(), True)