示例#1
0
def print_board(game: othello_console.Othello) -> None:
    """ Prints a board in the form of 2D list """
    print("B: {}  W: {}".format(add_pieces(game, "B"), add_pieces(game, "W")))
    for row in range(game.get_rows()):
        for col in range(game.get_cols()):
            print(game.get_board()[row][col], end=" ")
        print()
示例#2
0
def add_pieces(game: othello_console.Othello, piece: str) -> int:
    """ Totals the number of pieces of B/W given the piece type as a str """
    sum = 0
    for row in range(game.get_rows()):
        for col in range(game.get_cols()):
            board_piece = game.get_board()[row][col]
            if board_piece == piece:
                sum += 1
    return sum
示例#3
0
def print_winner(game: othello_console.Othello) -> None:
    """ Prints out the winner of the game """
    black_pieces = add_pieces(game, "B")
    white_pieces = add_pieces(game, "W")
    if black_pieces == white_pieces:
        print("WINNER: NONE")
    elif black_pieces > white_pieces:
        if game.get_win_type() == ">":
            print("WINNER: BLACK")
        elif game.get_win_type() == "<":
            print("WINNER: WHITE")
    else:
        if game.get_win_type() == ">":
            print("WINNER: WHITE")
        elif game.get_win_type() == "<":
            print("WINNER: BLACK")
示例#4
0
def read_move(game: othello_console.Othello) -> (int, int):
    """ Reads input and returns if move is valid """
    if not game.any_possible_moves():
        game.flip_current_turn()
        if not game.any_possible_moves():
            raise othello_console.GameOverError
        else:
            game.flip_current_turn()
            raise othello_console.PlayerCantMoveError

    while True:
        try:
            move = str(input().strip()).split(" ")
            row = int(move[0])-1
            col = int(move[1])-1
            if row < game.get_rows() and col < game.get_cols() and game.check_valid_move(row, col):
                print("VALID")
                return row, col
            else:
                raise othello_console.InvalidMoveError
        except:
            raise othello_console.InvalidInputError