Need Help Creating Function Detects Wins Code Working Output Far M Looking Help Clean Code Q43830147
#*****Need help creating a function that detects wins. Ihave some code, but it is not working and this is the output I haveso far. I’m looking to help clean up the code if possible so whenthe user gets 4 in a row whether it’s diagonal, horizontal orvertical, it detects the user has won*****(PYTHON)
# -*- coding: utf-8 -*-
from tkinter import *
window = Tk()
#————————————————————————————–
# Get background image.
port_image = PhotoImage(file=’port_hole_14-80px.png’)
background_image = PhotoImage(file=’square-01.png’)
green_ball_image = PhotoImage(file=’glass_ball-77px-04.png’)
red_ball_image=PhotoImage(file=’glass_ball-77px-01.png’)
on_move_image=green_ball_image
off_move_image=red_ball_image
image_width = background_image.width() # x coord of right boxwall
image_height = background_image.height() # y coord of boxbottom
nrows = 6
ncols = 7
turn = 0
num_column=[]
for column in range(ncols):
num_column.append(0)
print(num_column)
ball_list=[]
for c in range(ncols):
ball_list.append([])
#————————————————————————————–
# Create canvas, background and sprite objects.
canvas = Canvas(window, width = 7*image_width, height =6*image_height, bg = ‘black’)
canvas.create_image(2, 2, image = background_image, anchor =’nw’)
for rows in range(nrows+1): #increments +1 to fill the rows
for col in range(ncols+1): #increments +1 to fill the columns
last_square=canvas.create_image(2+image_width*rows,2+image_height*col, image=background_image, anchor=’nw’) #multiplywidth by rows and height by columns
for rows in range(nrows+1):
for col in range(ncols+1):
canvas.create_image(2+image_width*rows, 2+image_height*col,image=port_image,anchor=’nw’)
canvas.pack()
#divide x coordinate by image width
def on_mouse_press(event):
global turn
location= event.x
column=(location//image_width)
print(‘Mouse Pressed’, location//image_width)
if(turn==0):
greenball=canvas.create_image(2+image_width * column, 0,image=on_move_image , anchor = ‘nw’, tag=’GREEN’)#Creates greenball
ball_list[column].append(greenball)
canvas.lift(greenball,last_square)
num_column[column] +=1
print(num_column)
pixels= 480 – (num_column[column]*image_height)
for i in range(pixels):
canvas.move(greenball,0,1)
canvas.update()
canvas.after(1)
turn =1
elif turn == 1:
redball=canvas.create_image(2+image_width * column, 0,image=off_move_image , anchor = ‘nw’, tag=’RED’)#Creates redball
ball_list[column].append(redball)
canvas.lift(redball,last_square)
num_column[column] +=1
print(num_column)
pixels= 480 – (num_column[column]*image_height)
for i in range(pixels):
canvas.move(redball,0,1)
canvas.update()
canvas.after(1)
turn = 0
return
def color_at(row,col):
ball=ball_list[ncols][nrows]
ball_color = canvas.gettags(ball)[0]
row = nrows-row-1
if col<0 or col >= ncols:return None
if row < 0 or row >=len(ball_list[col]): return None
return canvas.gettags(ball_list[col][row])[0]
def color_check(color):
if color_at(row,col)==color
and color_at(row,col+1)== color
and color_at(row, col+2)==color
and color_at(row, col+3)==color:
return row,col
if color_at(row, col)==color
and color_at(row+1, col)==color
and color_at(row+2, col)==color
and color_at(row+3, col)==color:
return row, col
def win_check(color):
dr,dc =1,0
dr,dc=0,1
dr,dc=1,1
dr,dc=1,-1
for dr,dc in ((1,0), (1,1) (0,1), (1,-1)):
if color_at(row,col)==color
and color_at(row+dr, col+dc)==color
and color_at(row+2*dr, col+2*dc)==color
and color_at(row+3*dr, col+3*dc)==color:
return row,col,dr,dc
window.bind(”, on_mouse_press)
window.title(‘CONNECT 4’) # do this before event thread is busy inloop
window.mainloop()

File Edit Shell Debug Options Window Help 8 2019, 19:29:22) [MSC v.1916 32 Python 3.7.4 (tags/v3.7.4:e09359112e, Jul bit (Intel)] on win32 Type “help”, “copyright”, “credits” or “license ()” for more information. |>>> RESTART: C:UsersLenworthDownloadsconnect4-00.py ==== ==== ==== ==== ==== === [0, 0, 0, 0, 0, 0, 0] Mouse Pressed 0 [1, 0, 0, 0, 0, 0, 0] Mouse Pressed 1 [1, 1, 0, 0, 0, 0, 0] Mouse Pressed 0 [2, 1, 0, 0, 0, 0, 0] Mouse Pressed 1 [2, 2, 0, 0, 0, 0, 0] Mouse Pressed 0 [3, 2, 0, 0, 0, 0, 0] Mouse Pressed 1 [3, 3, 0, 0, 0, 0, 0] Mouse Pressed 0 [4, 3, 0, 0, 0, 0, 0] Mouse Pressed 1 [4, 4, 0, 0, 0, 0, 0] Show transcribed image text File Edit Shell Debug Options Window Help 8 2019, 19:29:22) [MSC v.1916 32 Python 3.7.4 (tags/v3.7.4:e09359112e, Jul bit (Intel)] on win32 Type “help”, “copyright”, “credits” or “license ()” for more information. |>>> RESTART: C:UsersLenworthDownloadsconnect4-00.py ==== ==== ==== ==== ==== === [0, 0, 0, 0, 0, 0, 0] Mouse Pressed 0 [1, 0, 0, 0, 0, 0, 0] Mouse Pressed 1 [1, 1, 0, 0, 0, 0, 0] Mouse Pressed 0 [2, 1, 0, 0, 0, 0, 0] Mouse Pressed 1 [2, 2, 0, 0, 0, 0, 0] Mouse Pressed 0 [3, 2, 0, 0, 0, 0, 0] Mouse Pressed 1 [3, 3, 0, 0, 0, 0, 0] Mouse Pressed 0 [4, 3, 0, 0, 0, 0, 0] Mouse Pressed 1 [4, 4, 0, 0, 0, 0, 0]
Expert Answer
Answer to #*****Need help creating a function that detects wins. I have some code, but it is not working and this is the output I …
OR