Example #1
0
 def _on_click(self) -> None:
     '''
     Is called when the play button is clicked. It asks for the user input and checks if they are valid.
     Then it redraws the board based on the input and destroys the play button. If invalid input is put in
     then the user is notified.
     '''
     canvas_width = self._canvas.winfo_width()#getting width of canvas
     canvas_height = self._canvas.winfo_height()#getting height of canvas
     info = gameInfo()#Initializing info
     info.show()#makes new window to get info
     if info.was_ok_clicked():#Checks if the ok button was clicked
         self._spaces = []#Takes out the inital pieces on the board before resizing
         row_number = info.get_row_number()#Gets the input for row
         col_number = info.get_col_number()#Gets input for column
         start_turn = info.get_start_turn()#Gets input for start_turn
         top_left = info.get_top_left()#Gets input for topleft piece
         win_method = info.get_win_method()#Gets input for win method
         self._greeting_text.set('Game Started')#Says game has begun
         self._row.set(row_number)#Sets row
         self._col.set(col_number)#Sets column
         self._startturn.set(start_turn)#Sets start player
         self._topleft.set(top_left)#Sets top left piece
         self._winmethod.set(win_method)#Sets win method
         if row_number not in ['4','6','8','10','12','14','16'] or col_number not in ['4','6','8','10','12','14','16']:#validates row and column valid
             self._greeting_text.set('Invalid entry. Please enter even number of rows and columns between 4 and 16')
             info._dialog_window.destroy()
         if start_turn not in ['B','W','b','w']:#Validates start turn
             self._greeting_text.set("Invalid entry. Please enter 'B' or 'W' for start turn")
         if top_left not in ['B','b','W','w']:#Validates top left
             self._greeting_text.set("Invalid entry. Please enter 'B' or 'W' for top left piece")
         if win_method not in ['M','m','L','l']:#Validates win method
             self._greeting_text.set("Invalid entry. Please enter 'M' or 'L' for win method")
         if self._greeting_text.get() == 'Game Started':#If all input was valid set the initial pieces
             self._state = gameLogic.gameState(int(row_number),int(col_number),str(start_turn),str(top_left),str(win_method))
             if self._state._topLeft == gameLogic.BLACK:
                 self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'black'))
                 self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'black'))
                 self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()), 'black', 'white'))
                 self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'white'))
             else:
                 self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'white'))
                 self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'white'))
                 self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'black'))
                 self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'black'))
             self._play_button.destroy()#Destroys the play button when the game starts
         self._redraw_board()
        except:
            pass
    while True:
        try:
            col = input("Enter an even number of columns you want on the board: ").strip()  # Gets columns
            col = assistCol(col)  # Checks validations
            break
        except ValueError:
            if any(c.isalpha() for c in col):  # In case letters were entered
                print("Please do not enter letters.")
            elif any(c in specialChar for c in col):  # In case characters in array above were entered
                print("Please do not enter in any special characters")
            elif any(c in [".", "/"] for c in col):  # In case of decimals or fractions
                print("Please do not use decimals.")
        except:
            pass
    return [rows, col]


if __name__ == "__main__":
    """
    Main method executes program
    """
    count = 0
    info = getInfo()  # Gets info
    game = gameLogic.gameState(info[0], info[1], info[2], info[3], info[4])  # Initializes board
    while True:
        printBoard(game, count)  # Prints the board
        count = 1
        makeMove(game, game.getTurn())  # Makes moves
Example #3
0
    def __init__(self):
        '''
        The initializing method that sets all the variables. It also contains
        all the labels and places them on the grid. It also has a button to begin playing.
        It basically starts the game.
        '''
        self._root_window = tkinter.Tk()#the window with everything

        #The button that will pop another window to enter game info
        self._play_button = tkinter.Button(
            master = self._root_window, text = 'Play', font = DEFAULT_FONT,
            command = self._on_click)
        #Putting the button on the grid
        self._play_button.grid(
            row = 3, column = 0, padx = 10, pady = 10,
            sticky = tkinter.N)

        self._greeting_text = tkinter.StringVar()#Variable to tell whether game has begun
        self._greeting_text.set('Reset the game with different options by pressing play!')#default
        self._row = tkinter.IntVar()#Variable to get input for rows
        self._row.set('4')
        self._col = tkinter.IntVar()#Variable to get input for columns
        self._col.set('4')
        self._startturn = tkinter.StringVar()#Variable to get input for starting player turn
        self._startturn.set('B')
        self._topleft = tkinter.StringVar()#Variable to get input for top Left piece on board
        self._topleft.set('B')
        self._winmethod = tkinter.StringVar()#Variable to get input for how to win
        self._winmethod.set('M')
        #Initializing the game
        self._state = gameLogic.gameState(int(str(self._row.get())),int(str(self._col.get())),str(self._startturn.get()),str(self._topleft.get()),str(self._winmethod.get()))

        #Label for whether game has started yet.
        greeting_label = tkinter.Label(
            master = self._root_window, textvariable = self._greeting_text,
            font = DEFAULT_FONT)

        #Puts above label on grid
        greeting_label.grid(
            row = 1, column = 0, padx = 10, pady = 10,
            sticky = tkinter.S)

        self._spaces = []#array to hold coordinates for pieces
        #Makes a canvas for the board
        self._canvas = tkinter.Canvas(
            master = self._root_window, width = 500, height = 450,
            background = '#008000')
        #Puts canvas on grid
        self._canvas.grid(
            row = 0, column = 0, padx = 10, pady = 10,
            sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)
        #Label for first player and score
        self._player1 = tkinter.Label(self._root_window, text = 'WHITE: \n'+self._state.getWhiteCount())
        #Puts above label on grid
        self._player1.grid(row = 0, column = 1, padx = 20, pady = 10,
                sticky = tkinter.W)
        #Label for second player and score
        self._player2 = tkinter.Label(self._root_window, text = 'BLACK: \n'+self._state.getBlackCount())
        #Puts above label on grid
        self._player2.grid(row = 0, column = 2, padx = 20, pady = 10,
                sticky = tkinter.W)
        #Initializes the board with default options which can later be changed.
        if self._state._topLeft == gameLogic.BLACK:
            self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'black'))
            self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'black'))
            self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()), 'black', 'white'))
            self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'white'))
        else:
            self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'white'))
            self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'white'))
            self._spaces.append((((int(self._col.get())/2)-1)/int(self._col.get()),((int(self._row.get())/2))/int(self._row.get()), (int(self._col.get())/2)/int(self._col.get()),((int(self._row.get())/2)+1)/int(self._row.get()),'black', 'black'))
            self._spaces.append((((int(self._col.get())/2))/int(self._col.get()),((int(self._row.get())/2)-1)/int(self._row.get()), ((int(self._col.get())/2)+1)/int(self._col.get()),(int(self._row.get())/2)/int(self._row.get()),'black', 'black'))


        self._canvas.bind('<Configure>', self._on_canvas_resized)#Resizes board on initializing
        self._canvas.bind('<Button-1>', self._on_canvas_clicked)#Calls -on_canvas_clicked when play button is pressed

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