def choose_g(self, board, buttons, statement): usercolumn = 6 if c4.checkifvalid(board, usercolumn) == True: c4.dousermove(board, 6) self.continuegame(board, buttons, statement)
def continuegame(self, board, buttons, statement): """ Main game function. Checks the gamestate to see if game is over If game is not over, makes the computer move and checks again If game is still not over, allows player to make another move If game is over, displays the appropriate labels Makes all buttons useless game is over """ gamestate = c4.checkgamestate(board) if gamestate == 0: computercolumn = c4.decidecomputermove(board) while c4.checkifvalid(board, computercolumn) != True: computercolumn = c4.decidecomputermove(board) c4.docomputermove(board, computercolumn) gamestate = c4.checkgamestate(board) graph = c4.plotgraphicalboard(board) canvas = FigureCanvasTkAgg(graph, self) canvas.draw() canvas.get_tk_widget().grid(row=2, column=1) if gamestate == 1: graph = c4.plotgraphicalboard(board) canvas = FigureCanvasTkAgg(graph, self) canvas.draw() canvas.get_tk_widget().grid(row=2, column=1) buttons[0].configure(command=donothing) buttons[1].configure(command=donothing) buttons[2].configure(command=donothing) buttons[3].configure(command=donothing) buttons[4].configure(command=donothing) buttons[5].configure(command=donothing) buttons[6].configure(command=donothing) buttons[0].update() buttons[1].update() buttons[2].update() buttons[3].update() buttons[4].update() buttons[5].update() buttons[6].update() statement.configure(text="You Won!") statement.update() print("You win") if gamestate == 2: buttons[0].configure(command=donothing) buttons[1].configure(command=donothing) buttons[2].configure(command=donothing) buttons[3].configure(command=donothing) buttons[4].configure(command=donothing) buttons[5].configure(command=donothing) buttons[6].configure(command=donothing) buttons[0].update() buttons[1].update() buttons[2].update() buttons[3].update() buttons[4].update() buttons[5].update() buttons[6].update() statement.configure(text="You Lost!") statement.update() print("You lose") if gamestate == 3: graph = c4.plotgraphicalboard(board) canvas = FigureCanvasTkAgg(graph, self) canvas.draw() canvas.get_tk_widget().grid(row=2, column=1) statement.configure(text="It's a draw!") statement.update() print("You draw")
def __init__(self, window, controller): ttk.Frame.__init__(self, window) title = ttk.Label(self, text="Connect 4 Game", font=large) title.grid(row=1, column=1) board = c4.makearrayboard() computercolumn = c4.decidecomputermove(board) while c4.checkifvalid(board, computercolumn) != True: computercolumn = c4.decidecomputermove(board) c4.docomputermove(board, computercolumn) graph = c4.plotgraphicalboard(board) canvas = FigureCanvasTkAgg(graph, self) canvas.draw() canvas.get_tk_widget().grid(row=2, column=1) separator = ttk.Label(self, text=" ", font=small) separator.grid(row=3, column=1) statement = ttk.Label(self, text="Choose a column", font=med) statement.grid(row=4, column=1) button_a = ttk.Button( self, text="a", command=lambda: self.choose_a(board, buttons, statement), width=1) button_b = ttk.Button( self, text="b", command=lambda: self.choose_b(board, buttons, statement), width=1) button_c = ttk.Button( self, text="c", command=lambda: self.choose_c(board, buttons, statement), width=1) button_d = ttk.Button( self, text="d", command=lambda: self.choose_d(board, buttons, statement), width=1) button_e = ttk.Button( self, text="e", command=lambda: self.choose_e(board, buttons, statement), width=1) button_f = ttk.Button( self, text="f", command=lambda: self.choose_f(board, buttons, statement), width=1) button_g = ttk.Button( self, text="g", command=lambda: self.choose_g(board, buttons, statement), width=1) if platform.system() == "Windows": button_a.place(x=68, y=410) button_b.place(x=111, y=410) button_c.place(x=154, y=410) button_d.place(x=197, y=410) button_e.place(x=240, y=410) button_f.place(x=283, y=410) button_g.place(x=327, y=410) else: button_a.place(x=25, y=410) button_b.place(x=68, y=410) button_c.place(x=111, y=410) button_d.place(x=154, y=410) button_e.place(x=197, y=410) button_f.place(x=240, y=410) button_g.place(x=283, y=410) buttons = [ button_a, button_b, button_c, button_d, button_e, button_f, button_g ]