Exemplo n.º 1
0
def ui():
    '''Executes the game'''
    []
    BOARD_ROWS = board("rows")
    BOARD_COLUMNS = board("columns")
    FIRST = first_and_default('who should go first')
    DEFAULT = first_and_default(
        'which color piece is in the top left-position in the default')
    WIN = win_method()
    a = Othello_logic.OthelloGameState(BOARD_ROWS, BOARD_COLUMNS, FIRST,
                                       DEFAULT, WIN)
    b = a.new_board()
    d = a.default_board()
    while True:
        display_board(d, BOARD_ROWS, BOARD_COLUMNS)
        print_turn(a.current_move())
        r = move('row', BOARD_ROWS) - 1
        c = move('column', BOARD_COLUMNS) - 1
        try:
            a.place_piece(r, c)
            a.count_pieces()
            print('SCORE:\nBlack:', a._numB, '\nWhite:', a._numW)
            a.change_turn()
            if a.current_move() == a.turn_pass():
                pass
            else:
                if a.current_move() == a.turn_pass():
                    print(a.opposite_turn(), 'has no more moves.',
                          a.opposite_turn(), 'passes.')
                else:
                    if a.victor() == a._BLACK:
                        print('No more moves can be made.\nBLACK WINS!')
                        break
                    elif a.victor() == a._WHITE:
                        print('No more moves can be made.\nWHITE WINS!')
                        break
                    elif a.victor() == '':
                        print("No more moves can be made.\nIt is a TIE.")
                        break
                    else:
                        pass
        except Othello_logic.InvalidMove:
            print('Sorry. That was an invalid move.')
        except Othello_logic.InvalidRowColumn:
            print('Sorry. That row and/or column does not exist')
    display_board(d, BOARD_ROWS, BOARD_COLUMNS)
Exemplo n.º 2
0
    def __init__(self, row: int, col: int, first: str, default: str, win: str):
        self._root_window = tkinter.Tk()

        self._logic = Othello_logic.OthelloGameState(row, col, first, default, win)
        self._new_game = self._logic.new_board()
        self._game_time = self._logic.default_board()

        self._columns = col
        self._rows = row
        self._first = first
        self._default = default
        self._win = win

        self._root_window.title('Othello')

        self._frame = tkinter.Frame(master = self._root_window, background = '#F50C0C')

        self._frame.grid(
            row = 0, columnspan = 3, padx = 5, pady = (5, 0),
            sticky = tkinter.NSEW)
        
        self._canvas = tkinter.Canvas(
            master = self._root_window,
            width = 500, height = 500,
            background = '#187330')

        self._black_count = tkinter.Label(master = self._frame, text = ('Black: ' + str(self._logic._numB)), font = ('Helvetica', 16), background = '#F50C0C')
        self._black_count.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = tkinter.NSEW)
        self._white_count = tkinter.Label(master = self._frame, text = ('White: ' + str(self._logic._numW)), font = ('Helvetica', 16), background = '#F50C0C')
        self._white_count.grid(row = 0, column = 2, padx = 5, pady = 5, sticky =  tkinter.NSEW)

        self._color = ''

        self._black_turn = "Black's turn"
        self._white_turn = "White's turn"

        if self._first == 'B':
            self._color = 'black'
            self._turn = tkinter.Label(master = self._frame, text = self._black_turn, font = ('Helvetica', 16), background = '#F50C0C')
            self._turn.grid(row = 0, column = 1, padx = 5, pady = 5, sticky = tkinter.N + tkinter.S)
        else:
            self._color = 'white'
            self._turn = tkinter.Label(master = self._frame, text = self._white_turn, font = ('Helvetica', 16), background = '#F50C0C')
            self._turn.grid(row = 0, column = 1, padx = 5, pady = 5, sticky = tkinter.N + tkinter.S)

        self._canvas.grid(
            row = 1, column = 0, padx = 3, pady = 5,
            sticky = tkinter.NSEW)

        self._frame.rowconfigure(0, weight = 1)
        self._frame.columnconfigure(0, weight = 1)
        self._frame.columnconfigure(1, weight = 1)
        self._frame.columnconfigure(2, weight = 1)
        
        self._canvas.bind('<Configure>', self._on_canvas_resized)
        self._canvas.bind('<Button-1>', self._on_canvas_clicked)


        self._root_window.rowconfigure(0, weight = 1)
        self._root_window.rowconfigure(1, weight = 8)
        self._root_window.columnconfigure(0, weight = 1)

        self._col_width = 0
        self._row_height = 0
Exemplo n.º 3
0
#Python 3.3
import Othello_ui
import Othello_logic

a = Othello_logic.OthelloGameState(Othello_ui.BOARD_ROWS,
                                   Othello_ui.BOARD_COLUMNS, Othello_ui.FIRST,
                                   Othello_ui.DEFAULT)
b = a.new_board()
c = a.default_board()
a._board


def display_board(board: [[str]], rows: int, columns: int) -> None:
    '''Creates and displays the game board'''
    s = '   '
    for i in range(1, columns + 1):
        if i > 9:
            s += str(i) + ' '
        else:
            s += str(i) + '  '
    s += '\n'

    for row in range(rows):
        if row > 9:
            s += str(row + 1) + ' '
        else:
            s += str(row + 1) + '  '
        for col in range(columns):
            if board[row][col] == ' ':
                s += '.  '
            else: