def start_game(cols, rows, turn, position, condition): game = othello.GameState(cols, rows, turn, position) while True: print('It is ' + game._turn + "'s turn") print() if game.check_possible_moves(): try: row = int( input( 'ROW: Please enter the row where you want to put a piece on' )) col = int( input( 'COLUMN: Please enter the column where you want to put a piece on' )) game.flip_piece(row, col) except: print('Invalid row/column, try again') finally: counter = 0 else: print('No possible moves for ' + game._turn) counter += 1 if counter >= 2: break game.turn_switch() print() print() print() print("THE WINNER IS: " + othello.determine_winner(game, condition))
def _get_input(self) -> 'GameState': ''' Asks users to enter input and returns a GameState ''' self._input_entered = False settings = user_input.BasicSettings() settings.show() if settings.was_ok_clicked(): '''If basic settings are entered, displays an empty board''' self._rows = settings.get_rows() self._columns = settings.get_columns() self._turn = settings.get_first_player() self._winning_mode = settings.get_winning_mode() set_board = user_input.InitialBoard(self._rows, self._columns) set_board.show() if set_board.was_finished(): '''If initial board is set, returns a GameState''' self._board = set_board.get_initial_board() self._input_entered = True return othello.GameState(self._rows, self._columns, self._board, self._turn, self._winning_mode) elif set_board.was_quit(): '''If initial board is dismissed, asks for input again''' return self._get_input() elif settings.was_cancel_clicked(): '''If basic settings are canceled, starts game again''' self._display_start_page()
def _gamestate_setup(self) -> None: '''Initalizes a game logic state''' self._gamestate = othello.GameState(self._colVar.get(), self._rowVar.get(), self._startVar.get(), self._topleftVar.get(), self._winVar.get())
def interface_console(): print('FULL') col = othello.get_col() row = othello.get_row() game = othello.GameState(col, row) game._print() while game._check_board_full() == False: try: while True: user_input = user_move_input() col_num = int(user_input[0]) - 1 row = int(user_input[1]) - 1 if game._check_a_move(col_num, row) == True: game._change_turn() break else: print('INVALID') break print('\n') game._print() print('\n') except IndexError and ValueError: print('INVALID') print("Game Over") print('The winner is {}'.format(game._get_winner()))
def test2(): state = othello.GameState(4, 4, str(1), str(1), str(1)) state.putDiscTest(' B ', 1, 4) state.putDiscTest(' W ', 2, 4) state.putDiscTest(' B ', 3, 4) state.putDiscTest(' W ', 1, 3) state.putDiscTest(' B ', 2, 3) state.putDiscTest(' W ', 3, 3) state.putDiscTest(' B ', 1, 2) state.putDiscTest(' B ', 2, 2) state.putDiscTest(' W ', 3, 2) state.putDiscTest(' B ', 3, 1) state.putDiscTest(' W ', 1, 1) state.putDiscTest(' B ', 2, 1) state.putDiscTest(' W ', 4, 1) state.putDiscTest(' B ', 4, 2) state.putDiscTest(' W ', 4, 3) state.putDiscTest(' B ', 4, 4) state._white_discs = 5 state._black_discs = 5 while True: print_board(state) if state.move_check() == 0: state.switch_turn() if state.move_check() == 0: state.GameOver() print('Game Over') print('The winner is: ' + state.getWinner()) break print('Current player:' + state.getTurn()) while True: try: column = int(input('Column: ')) if column <= state.getColumns() and column > 0: break else: print('A column must be between 1 and', state.getColumns()) except: print('The value must be an integer') while True: try: row = int(input('Row: ')) if row <= state.getRows() and row > 0: break else: print('A row must be between 1 and', state.getRows()) except: print('The value must be an integer') try: state.putDisc(column, row) except othello.DiscPresent: print('\n\n***7A disc is already in that cell***\n\n') except othello.InvalidMove: print('\n\n****That\'s an invalid move***\n\n')
def user_input() -> othello.GameState: '''Handles are user input to initialize game''' while True: try: rows = int(input('How many rows? ')) except: print('Input must be an integer') else: if rows >= 4 and rows <= 16 and rows%2 == 0: break else: print('Number of rows must be an even number between 4 and 16') while True: try: columns = int(input('How many colums? ')) except: print('Input must be an integer') else: if columns >= 4 and columns <= 16 and columns%2 == 0: break else: print('Number of rows must be an even number between 4 and 16') while True: move = input('Which of the players will move first?\n' + '1) White\n' + '2) Black\n') if move == '1' or move == '2': break else: print('Choice must be 1 or 2\n') while True: topleft = input('Which color disc will be in the top-left position?\n' + '1) White (default)\n' + '2) Black\n') if topleft == '1' or topleft == '2': break else: print('Choice must be 1 or 2\n') while True: winning_condition = input('How is a winner determined?\n' + '1) The player with the most discs on the board at the end of the game\n' + '2) The player with the fewest discs on the board at the end of the game\n') if winning_condition == '1' or winning_condition == '2': break else: print('Choice must be 1 or 2\n') return othello.GameState(rows, columns, move, topleft, winning_condition)
def _ingame_ok_command(self): self._init_text.set('In game...') self._ok_button.grid(row=1, column=2, padx=1, pady=1) self._gamestate = othello.GameState(self._row, self._col, self._board, self._turn) self._board = self._gamestate.board self._canvas.bind('<Button-1>', self.move) self._canvas.bind('<Configure>', self.canvas_resized) self._ok_button = tkinter.Button(master=self._root_window, text='change turn', font=FONT, command=self._change_turn) self._ok_button.grid(row=1, column=2, padx=1, pady=1)
def read_info(self): """Get the information from the option window """ self._information = GameInfo() self._information.start() self._col = int(self._information._col) self._row = int(self._information._row) self._top_left = self._information._top_left_color self._start_color = self._information._begin_color self._win_method = self._information._users_win_method self._new_game = othello.GameState(self._col, self._row, self._start_color, self._top_left, self._win_method)
def thegame(self): x = [ self._e1.get(), self._e2.get(), self._e3.get(), self._e4.get(), self._e5.get() ] try: if (int(x[0]) <= 16 and int(x[0]) >= 4 and int(x[1]) <= 16 and int(x[1]) >= 4 and (x[2].upper() == 'W' or x[2].upper() == 'B') and (x[3].lower() == 'white' or x[3].lower() == 'black') and (x[4].lower() == 'most' or x[4].lower() == 'fewest')): OthelloApplication( othello.GameState(int(x[0]), int(x[1]), str(x[2]).upper(), str(x[3]).lower()), x[4]).start() except: pass
#Mohamed Kharaev 43121144. Lab Section 13 #run.py import settings import othello import othelloGUI if __name__ == '__main__': settings = settings.SettingsAPP() settings.run() game = othello.GameState(settings.selected_rows, settings.selected_cols, settings.selected_bw, settings.selected_mode) init_gameGUI = othelloGUI.Initialize_Board_GUI(game) init_gameGUI.run() game = init_gameGUI.game othelloGUI.GameGUI(game).run()
row, col = [(int(i) - 1) for i in move.split()] return row, col def _winner_str(piece: int) -> str: '''returns a str that represents the winner''' if piece == othello.NONE: return 'NONE' else: return _piece_to_text(piece) if __name__ == '__main__': print('FULL') settings = input_game_settings() game = othello.GameState(settings.rows, settings.cols, settings.turn, settings.mode) game = initialize_board(game) _print_board(game) while game.check_any_move_possible(): row, col = _input_move(game) previous_board = othello.copy_game_board(game.get_board()) try: game.make_move(row, col) except (othello.InvalidMove): pass if previous_board == game.get_board(): print('INVALID') else: print('VALID') if game.check_any_move_possible(): _print_board(game)
def _on_play_now_clicked(self): """ This function handles the event in which the "PLAY NOW" button is pressed. If the OK button was clicked in the options dialog window, this function will delete everything on the current root window in order to draw the Othello game board and game information. """ options_dialog = OptionsDialog() options_dialog.show() if options_dialog.get_ok_button_clicked() == True: # Othello options and game state self._user_input_dict = options_dialog.get_final_input_dict() self._othello_game_state = othello.GameState( self._user_input_dict["row"], self._user_input_dict["column"], self._user_input_dict["first player"], self._user_input_dict["win style"]) # Delete current widgets on window self._play_now_button.grid_forget() self._othello_label.grid_forget() # Othello logo othello_app_label = tkinter.Label(master=self._root_window, text="Othello", font=("Helvetica", 50), fg="green") othello_app_label.grid(row=0, column=0, columnspan=2, padx=10, pady=5, sticky=tkinter.N + tkinter.E + tkinter.S + tkinter.W) # Game board canvas self._game_board_canvas = tkinter.Canvas(master=self._root_window, width=500, height=500, background="#1D7F3C") self._game_board_canvas.grid(row=1, column=0, padx=10, pady=10, sticky=tkinter.N + tkinter.E + tkinter.S + tkinter.W) self._game_board_canvas.update() # Binding widgets stage self._game_board_canvas.bind("<Configure>", self._on_canvas_resized) self._game_board_canvas.bind("<Button-1>", self._on_canvas_clicked) # Game information area self._game_info_frame = tkinter.Frame(master=self._root_window) self._game_info_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky=tkinter.N + tkinter.E + tkinter.S + tkinter.W) # StringVar variables. # White score self._white_score_text = tkinter.StringVar() white_score_label = tkinter.Label( master=self._game_info_frame, textvariable=self._white_score_text, font=HEADER_FONT) white_score_label.grid(row=0, column=0, padx=10, pady=5, sticky=tkinter.W) # Black score self._black_score_text = tkinter.StringVar() black_score_label = tkinter.Label( master=self._game_info_frame, textvariable=self._black_score_text, font=HEADER_FONT) black_score_label.grid(row=1, column=0, padx=10, pady=5, sticky=tkinter.W) # Current player self._current_player_text = tkinter.StringVar() current_player_label = tkinter.Label( master=self._game_info_frame, textvariable=self._current_player_text, font=HEADER_FONT) current_player_label.grid(row=0, column=2, padx=10, pady=5, sticky=tkinter.W) # Winner self._winner_text = tkinter.StringVar() winner_label = tkinter.Label(master=self._game_info_frame, textvariable=self._winner_text, font=HEADER_FONT) winner_label.grid(row=1, column=2, padx=10, pady=5, sticky=tkinter.W) # Display self._redraw_draw_board() self._get_game_info() # Manage resizing weights self._root_window.rowconfigure(0, weight=0)