Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# *****Need help creating a function that detects wins. I have some code, but it is not working and this is the output I have

#*****Need help creating a function that detects wins. I have some code, but it is not working and this is the output I have so far. I'm looking to help clean up the code if possible so when the user gets 4 in a row whether it's diagonal, horizontal or vertical, 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 box wall image_height = background_image.height() # y coord of box bottom

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') #multiply width 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 green ball 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 red ball 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= ncols:return None if 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 in loop

window.mainloop()

image text in transcribed

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:\Users\Lenworth\Downloads\connect4-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]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago