Example #1
0
def initialize(board):
    """
    Initailizes the board. If the board is of even length, two black pieces are 
    placed diagonally right and two white pieces placed diagonally left in the 
    middle of the board. If the board is of odd length, sets up the board based 
    on that length minus one. 
    """
    size = board.length
    if size%2 == 0: # even length
        mid = int(size/2)
        w1 = (mid, mid)
        b1 = (mid-1, mid)
        b2 = (mid, mid-1)
        w2 = (mid-1, mid-1)
        place_lst = [w1,w2,b1,b2]
    else: # odd length
        size = size - 1
        mid = int(size/2)
        w1 = (mid, mid)
        b1 = (mid-1, mid)
        b2 = (mid, mid-1)
        w2 = (mid-1, mid-1)
        place_lst = [w1,w2,b1,b2]
        
    for i in range(4):
        place_lst[i] = deindexify(place_lst[i][0], place_lst[i][1])
    
    # places pieces
    for i in range(4):
        pos = indexify(place_lst[i])
        if i < 2:
            board.place(pos[0], pos[1], reversi.Piece("white"))
        else:
            board.place(pos[0], pos[1], reversi.Piece())
Example #2
0
def is_game_finished(board):
    '''Checks to see if board is full or if there are no remaining moves left for both colors.'''
    if board.is_full == True:
        return (True)
    white = get_all_capturing_cells(board, reversi.Piece('white'))
    black = get_all_capturing_cells(board, reversi.Piece('black'))
    if len(white) == 0 and len(black) == 0:
        return (True)
    else:
        return (False)
Example #3
0
def initialize(board):
    """
    Initializes the board by giving it four pieces. Two black and two white that are placed in the center, diagonal from each other.
    """
    length = board.length
    even = int(length/2) - 1
    odd = int(length/2)
    board.place(even,even,reversi.Piece('white'))
    board.place(even,odd,reversi.Piece('black'))
    board.place(odd,even,reversi.Piece('black'))
    board.place(odd,odd,reversi.Piece('white'))
Example #4
0
def initialize(board):
    '''Places two black and two white pieces in the middle of the board diagnoally 
    with respect to eachother. Handles any size board.'''
    white, black = reversi.Piece('white'), reversi.Piece('black')
    start = board.length // 2
    move = start - 1

    board.place(move, move, white)
    board.place(start, start, white)
    board.place(move, start, black)
    board.place(start, move, black)
Example #5
0
def is_game_finished(board):
    """
    Checks to see if the board is full or if there are no possible moves left to make. Returns True if game is done. False if not.
    """
    bp = reversi.Piece("black")
    wp = reversi.Piece("white")
    if board.is_full():
        return True
    elif len(get_all_capturing_cells(board,bp)) == 0 and len(get_all_capturing_cells(board,wp)) == 0:
        return True
    else:
        return False
Example #6
0
def is_game_finished(board):
    """
    If board is full, or no moves are possible for either black or white, 
    returns True. Otherwise, returns False. 
    """
    if board.is_full():
        return True
    elif len(get_hint(board, reversi.Piece("white"))) == 0 and \
    len(get_hint(board, reversi.Piece())) == 0:
        return True
    else:
        return False
Example #7
0
def initialize(board):
    """Initializes a board using the start-game rules of Reversi

    Arguments:
        board {Board} -- board to initialize
    """

    board.place(board.length // 2 - 1, board.length // 2 - 1,
                reversi.Piece('white'))
    board.place(board.length // 2, board.length // 2, reversi.Piece('white'))
    board.place(board.length // 2, board.length // 2 - 1,
                reversi.Piece('black'))
    board.place(board.length // 2 - 1, board.length // 2,
                reversi.Piece('black'))
Example #8
0
def is_game_finished(board):
    """Checks whether the game is finished

    Arguments:
        board {Board} -- board to check in current state

    Returns:
        bool -- True if game finished, False if not
    """

    b_poss_moves = len(get_hint(board, reversi.Piece('black')))
    w_poss_moves = len(get_hint(board, reversi.Piece('white')))

    if b_poss_moves == 0 and w_poss_moves == 0 or board.is_full():
        return True

    return False
Example #9
0
def initialize(board):
    """
    Initializes two black and two white pieces
    in the middle of the board. Black pieces placed
    diagonally right and white pieces placed diagonally left.
    Takes the length of the boards and uses floor division
    to get a whole number (instead of a floating point number).
    (This accounts for odd sized boards and even sized boards).
    
    """
    length = board.length // 2 - 1  #uses floor division to get length as a
    #whole number, accounts for odd sized board
    #lengths
    #This makes the white pieces placed diagonally left and the blackpieces
    #placed diagonally right:
    board.place(length, length, reversi.Piece('white'))
    board.place(length + 1, length + 1, reversi.Piece('white'))
    board.place(length + 1, length, reversi.Piece('black'))
    board.place(length, length + 1, reversi.Piece('black'))
Example #10
0
def is_game_finished(board):
    """
    This function calls the get_all_capturing_cells
    function, takes the length of the dictionary returned
    in that function. If the length is empty, the function
    returns true- meaning the game is finished. If the 
    length is not empty, the function returns false- meaning
    the game is not finished.
    """
    #calling the get_all_capturing_cells function, accounting for the
    #length of the dictionary that is returned in the function
    x = len(get_all_capturing_cells(board, reversi.Piece('white')))
    y = len(get_all_capturing_cells(board, reversi.Piece('black')))

    if x == 0 and y == 0:  #if the length of the dictionary contains nothing,
        #the game is finished, returns true that it is
        return True
    else:  #else if the length of the dictionary contains something
        #the game is not finished, returns false that it is not
        return False
Example #11
0
def figure_1(board):
    """
    You can use this function to test your program
    """
    board.place(0,0,reversi.Piece('black'))
    board.place(0,3,reversi.Piece('black'))
    board.place(0,4,reversi.Piece('white'))
    board.place(0,5,reversi.Piece('white'))
    board.place(0,6,reversi.Piece('white'))
    board.place(1,1,reversi.Piece('white'))
    board.place(1,3,reversi.Piece('white'))
    board.place(1,5,reversi.Piece('white'))
    board.place(1,6,reversi.Piece('white'))
    board.place(1,7,reversi.Piece('white'))
    board.place(2,2,reversi.Piece('white'))
    board.place(2,3,reversi.Piece('black'))
    board.place(2,4,reversi.Piece('white'))
    board.place(2,5,reversi.Piece('white'))
    board.place(2,7,reversi.Piece('white'))
    board.place(3,0,reversi.Piece('black'))
    board.place(3,1,reversi.Piece('white'))
    board.place(3,2,reversi.Piece('white'))
    board.place(3,4,reversi.Piece('white'))
    board.place(3,5,reversi.Piece('white'))
    board.place(3,6,reversi.Piece('black'))
    board.place(3,7,reversi.Piece('black'))
    board.place(4,0,reversi.Piece('white'))
    board.place(4,2,reversi.Piece('white'))
    board.place(4,4,reversi.Piece('white'))
    board.place(5,0,reversi.Piece('black'))
    board.place(5,2,reversi.Piece('black'))
    board.place(5,3,reversi.Piece('white'))
    board.place(5,5,reversi.Piece('black'))
    board.place(6,0,reversi.Piece('black'))
    board.place(6,1,reversi.Piece('black'))
    board.place(6,3,reversi.Piece('white'))
    board.place(6,6,reversi.Piece('white'))
    board.place(7,1,reversi.Piece('black'))
    board.place(7,2,reversi.Piece('white'))
    board.place(7,3,reversi.Piece('black'))
    board.place(7,7,reversi.Piece('black'))
Example #12
0
def game_play_human():
    """
    This is the main mechanism of the human vs. human game play.
    """
    
    banner = """
     _____                         _ 
    |  __ \                       (_)
    | |__) |_____   _____ _ __ ___ _ 
    |  _  // _ \ \ / / _ \ '__/ __| |
    | | \ \  __/\ V /  __/ |  \__ \ |
    |_|  \_\___| \_/ \___|_|  |___/_|
    
    Developed by The Students Inc.
    CSE231 Spring Semester 2018
    Michigan State University
    East Lansing, MI 48824, USA.
    """

    prompt = "[{:s}'s turn] :> "
    print(banner)
   
    # Choose the color here
    (my_color, opponent_color) = choose_color()
    
    # Take a board of size 8x8
    # Prompt for board size
    size = input("Input a board size: ")
    board = reversi.Board(int(size))
    initialize(board)
    
    # Decide on whose turn, use a variable called 'turn'.
    turn = my_color if my_color == 'white' else opponent_color
    
    # loop until the game is finished
    while not is_game_finished(board):
        try:
            # Count the pieces and assign into piece_count
            piece_count = count_pieces(board)
            
            print("Current board:")
            board.display(piece_count)    
            
            # Get a piece according to turn
            piece = reversi.Piece(turn)

            # Get the command from user using input
            command = input(prompt.format(turn)).lower()
            
            # Now decide on different commands
            if command == 'exit':
                break
            elif command == 'hint':
                print("\tHint: " + ", ".join(get_hint(board, piece)))
            elif command == 'pass':
                hint = get_hint(board, piece)
                if len(hint) == 0:
                    turn = my_color if turn == opponent_color \
                                        else opponent_color
                    print("\tHanded over to \'{:s}\'.".format(turn))
                else:
                    print("\tCan't hand over to opponent, you have moves," + \
                          " type \'hint\'.")
            else:
                    (row, col) = indexify(command)
                    t = place_and_flip(board, row, col, piece)
                    if "Error:" in t:
                        print(t)
                        continue
                    else:
                        print("\t{:s} played {:s}.".format(turn, command))
                        turn = my_color if turn == opponent_color \
                                            else opponent_color
        except Exception as err:
            print("Error:", err)
    
    # The loop is over.
    piece_count = count_pieces(board)
    print("Current board:")
    board.display(piece_count)    
    winner = get_winner(board)
    if winner != 'draw':
        diff = abs(piece_count[0] - piece_count[1])
        print("\'{:s}\' wins by {:d}! yay!!".format(winner, diff))
    else:
        print("This game ends in a draw.")