#πŸ”’ Connect four python program

1532 messages Β· Page 2 of 2 (latest)

flint ferry
#

that works

gusty shard
#

rows is simplest

#

collumns is the next on complexity list

#

then last but not least diagonal

#

thats one is hell

flint ferry
#

oh damn

gusty shard
#

anyways

flint ferry
#

i mean

#

my

#

thing for colums works

#

but not in my other code...

gusty shard
#

p sure in ur other code ur board entries are | X |

#

not X

#

so might be why

flint ferry
#

well the string is

#

"| X |"

#

wait

gusty shard
#

anyways

flint ferry
#

ya

#

idk

#

.

gusty shard
#

lets leave this scuffe winc on check for later

#

lets start on a rows win con check

#

cause thats simplest

flint ferry
#

it worked

#

what

#

.

#

nvm

#

lets just

#

not do this

gusty shard
#

bro is on drugs

flint ferry
#

prolyl

#

lets do wtv ur saying

#

amazing!

#

ok i ready

gusty shard
#

do we continue on win con check or the other function from ealier

#

ur choice

flint ferry
#

row

gusty shard
#

now

#

im do what u did

#

and make a minimal example

flint ferry
#

mm ok

gusty shard
#
row = ["X", "X", "_", "_", "O", "O", "O"]
#

so

#

lets say we have this row

#

we want to start at the far left right

flint ferry
#

yes

gusty shard
#

so

#
row = ["X", "X", "_", "_", "O", "O", "O"]
#       0    1    2    3    4    5    6
flint ferry
#

yuh

gusty shard
#

from 0 to what number is the first set of 4 entries

flint ferry
#

?

#

question or

#

no

gusty shard
#

yes

#

im jsut typing while u think/answer

flint ferry
#

i mean there are no connects of 4

#

i think im answering another question

gusty shard
#

but even then we checking if connect 4 exists

#

so starting at far left aka 0

#

if we stop at 0

#

tahts checking 1 entry

#

if we stop at 1

#

thats checking 2 entries

#

etc.

#

what number we stop at for 4 entries

#

aka connect 4

flint ferry
#

i mean like

#

5 ig

#

i think?

#

OH WAIT

#

no 3

#

stop at 3

gusty shard
#

correct

#
row = ["X", "X", "_", "_", "O", "O", "O"]
#       0    1    2    3    4    5    6

first_entry_start = 0
first_entry_end   = 3
first_entry       = ["X", "X", "_", "_"] 
#

now whats the last set 4 here

#

which number does it stop at

#

and which number does it start at ?

flint ferry
#

uhhhhhhhhh

#

huh

#

i aint get it

gusty shard
#

imagine this 0 to 3 range thing

#

is a window

flint ferry
#

yes

gusty shard
#

lets slide that window over till it hits edge of list

#

what are the 4 numbers we looking at

flint ferry
#

ohhhhh

#

tahts what ur asking

#

its gon be X X _ _

#

and

#

2 _

gusty shard
#

for first entry yes

#

now slide that window over to the right till it hits end of list

#

and what we looking at now

flint ferry
#

yuh

gusty shard
#

what are the 4

flint ferry
#

are we adding it or

gusty shard
#

nah overriding

flint ferry
#

oh ok we're not

#

only 3

#

unless

#

we starting at

#

the second

#

_

#

fi so

#

if*

#

_ o o o

gusty shard
#

that is correct

#

that is the last 4 in the list

#

i can "animate" a sliding window for u if that doesnt make sense

flint ferry
#

na i can do that

#

with time

#

i know

#

i understand that type of stuff

#

i love slicing

gusty shard
#

X X _ _ ||O O O||
||X|| X _ _ O ||O O||
||X X|| _ _ O O ||O||
||X X _|| _ O O O

flint ferry
#

that works too

#

nice

gusty shard
#

there

#

see how the window of size 4 is just sliding along

#

now translating that into python code

#
row = ["X", "X", "_", "_", "O", "O", "O"]
#       0    1    2    3    4    5    6

first_entry_start = 0
first_entry_end   = 3
first_entry       = ["X", "X", "_", "_"]

last_entry_start  = 3
last_entry_end    = 6
last_entry        = ["_", "O", "O", "O"]
#

we would have something like this

#

right

flint ferry
#

mmmmmm ok

gusty shard
#

does it make sense

flint ferry
#

yes

#

i ahve done

gusty shard
#

im just denoting the indexes of where stuff appears

flint ferry
#

this type of stuff

gusty shard
#

now

flint ferry
#

yuh

gusty shard
#

lets transform this window into a loop

#

shall we

flint ferry
#

yes we shall

#

i should be able to do this

gusty shard
#

that shit was genius

flint ferry
#

ya its pretty sick

#

do u want me to like

#

"print out"

#

the things

gusty shard
#

like in that spoiler message

flint ferry
#

ok

#

oh wait a sec

#

i think i had confused

#

like

#

thinking about this

#
row = ["X", "X", "_", "_", "O", "O", "O"]

for i in row:
    print(row[i:i + 4])
#

cus i was doing this

#

then i realized like

#

na

gusty shard
#

if i was an int that is

flint ferry
#

oh wait

#

i did the

#

for loop wrong

gusty shard
#

ye

flint ferry
#

should be this

#
row = ["X", "X", "_", "_", "O", "O", "O"]

for i in range(4):
    print(row[i:i + 4])
gusty shard
#

thats closer

flint ferry
#

hmmmmmm

#

oh

#

just 4 times

gusty shard
#

but clearly at one point it falls off and doesnt print 4

#

why u think it doesnt print 4

flint ferry
#

huh?

#

oh

#

i just fixed it

gusty shard
#

it doesnt print 4 entries

#

why is that

flint ferry
#

cus there is no more

#

after it

#

also i fixedd itt

gusty shard
#

technical answer is more than that but im give it to ya

flint ferry
#

lol

gusty shard
#

now

#

usually this would be bad

#

because we dont that there would only 4 sets

#

but in here because board size is fixed

#

we can get away with range(4)

flint ferry
#

ahhhhh ok

#

so basicly now i just need to liek

#

maybe add these to a list and check if they are all 1 letter

#

or sum more simple

gusty shard
#

i got u one better

#

set()

#

its a very neat lil trick

flint ferry
#

what does set do

gusty shard
#

it transforms what u give it into a set

#

think of it as in a math set

#

sets are cool for numerous reasons

#

but reason they pop up here

#

is because they can have no duplicate

#

so if u set(['_', 'O', 'O', 'O'])

#

u get {'_', 'O'}

flint ferry
#

ooooooooooooh

#

s o its like

#

making so there are only special characters

gusty shard
#

go ahead

#

write some code

#

cook up

flint ferry
#

ok

gusty shard
#

play around with it

flint ferry
#

this is fun

gusty shard
#

python is very fun

flint ferry
#

OH

#

AND THEN

#

IF

#

THE SET

#

ONLY HAS

#

1

#

THEN ITS

#

A CONNECT

gusty shard
#

there we go

flint ferry
#

OF 4

gusty shard
#

he got it

#

fyi u can pass multiple things to print

#

print(row[i:i+4], set(row[i:i+4]))

#

try this for example

flint ferry
#

ok

#

oooh

gusty shard
#

it helps a lot when doing these kinds of things

flint ferry
#

so it prints both the set and unset

gusty shard
#

it prints the slice then the set of slice

#

yes

#

there is so much to print that u wouldnt believe u only used it as print(string)

#

lmfao

flint ferry
#

crazy

gusty shard
#

welp

#

u got all the puzzle pieces

#

go build the damn row checker now

flint ferry
#

ill go do dat

gusty shard
#

isnt it cool

flint ferry
#

super

#

now i gotta go through

#

the row of rows

gusty shard
#

ye

#

u gotta loop throught board

#

to make sure u apply this to every row

flint ferry
#

i dont gotta go

gusty shard
#

my brain

#

is braindead

flint ferry
#

lol

gusty shard
#

oh its 4am thats why

#

huh

flint ferry
#

its 2 for m

gusty shard
#

anyways

flint ferry
#

me

#

yuh ig

#

that works

gusty shard
#

u just never stopped once u found a connect 4

flint ferry
#

ahhh ok

gusty shard
#

which u can fix if u want but

#

usually in a game u dont have to

#

since the first time u find one

#

its rare u find another

flint ferry
#

yaaa

gusty shard
#

and even if u do find another

#

its prob the same player that somehow got 2 connect 4

#

at once

#

or 3

flint ferry
#

so uh

#

ya

#

id kabout that

#

hmmm unless

#

ya

#

uh

gusty shard
#

issue is that u never checked if whats in the len 1 set is either an X or an O

flint ferry
gusty shard
#

u just checking if length 1

#

in this case its triggering on a connect 4 of empty

#

which is hella funny

#

but prob not what u want

flint ferry
#

it snot checking it on empyt

#

it only has 1 in

#

and thats triggering it

#

OHH

#

I KNOW

#

ill do

#

if the unset = 4

#

and the set

#

= 1

#

def check(board):
    for row in board:
        for i in range(4):
            set_row_check_var = set(row[i:i+4])
            unset_row_check_var = row[i:i+4]
        if len(set_row_check_var) == 1 and len(unset_row_check_var) == 4:
            print("CONNECT 4")
            time.sleep(2000)
#

dw about the sleep

gusty shard
#

== not = inside the if

flint ferry
#

yep lol just got that error

gusty shard
#

anyways i got a funny idea for u

flint ferry
#

?

gusty shard
#

if u went throught the board in a specific way

#

u could get collumn 1 as a list

flint ferry
#

bru

gusty shard
#

then just shove said list into the row check

#

since its same logic

flint ferry
#

true

#

i can

#

do that

gusty shard
#

very funny

#

anyways

flint ferry
#

wait i know whyyyyyyyy

#

what i will did

#

do

#

iss

#

pop out the empty ones

gusty shard
#

dont

flint ferry
#

why

gusty shard
#

u dont want to mess up wth size of board

flint ferry
#

na not on the board

#

for the checking

#

what i will do

#

is

gusty shard
#

the easiest way here

flint ferry
#

oh ok nvm

gusty shard
#

is to check whats inside the set of lenght 1

flint ferry
#

mhm

#

wdym length 1

gusty shard
#

len(set_row_check_var) == 1

#

inside this set

#

what is in there

#

to trigger CONNECT 4 when nothing is on board

#

u can either think about it or just print the damn set alongside the message

#

up to u

flint ferry
#

this might work

#

nvm

#

it dont

gusty shard
#

u checking if its in the non-set version

flint ferry
#

ya

gusty shard
#

u most likely want to use the set version

flint ferry
#

OHH

#

nope

#

stil not working

gusty shard
#

print the set to see whats inside it

#

then

flint ferry
#

pl

#

ok

#

oh

gusty shard
#

so ye

#

u found issue

#

now to make sure it ignores when that string is only one in set

flint ferry
#

ok lemme think

#

ya idk

gusty shard
#

!e

a = {'|_|'}

if '|_|' not in a:
    print('test')
nova nebulaBOT
gusty shard
#

u prob just got wrong string

flint ferry
#

really?

#

oh

#

well look what i just did

gusty shard
#

u can see how code above works

flint ferry
#

i think that might work

gusty shard
# flint ferry

u dont want to check it inside unset in case its something like _ _ _ X X X X

flint ferry
#

oh ok

gusty shard
#

hopefully u see why

flint ferry
#

nope it dont work

#

and ya

#

i do

gusty shard
# flint ferry

copy the string it printed that it found inside set here

#

and paste it into ur if in code

flint ferry
#

WAIT

#

do i have to put

#

()

#

around it

#

nvm

#

uh

#

ok

#

oh wait

#

those are

#

{}

#

gang is this a dictionary

gusty shard
#

its a set

flint ferry
#

why is there curley brackets

#

ohhhh ok

#

makes sents

gusty shard
#

they use same brackets as dictionaries

flint ferry
#

sense

gusty shard
#

u not crazy

kindred grail
#

Wtf 1394 message
Anyone have tldr context

flint ferry
gusty shard
#

fucking god knows how long it has been

kindred grail
#

GreatπŸ’€

flint ferry
gusty shard
flint ferry
#

hes helpfull

#

im down to liek

#

400 lines

gusty shard
#

would be less if his adhd didnt pivot him into win con check

gusty shard
flint ferry
#
def check(board):
    for row in board:
        for i in range(4):
            set_row_check_var = set(row[i:i+4])
            unset_row_check_var = row[i:i+4]
            if {'| _ |'} not in set_row_check_var and len(set_row_check_var) == 1:
                print("SUM HERE")
gusty shard
#
def check(board):
    for row in board:
        for i in range(4):
            set_row_check_var = set(row[i:i+4])
            unset_row_check_var = row[i:i+4]
            if ('| _ |' not in set_row_check_var) and (len(set_row_check_var) == 1):
                print("SUM HERE")
#

can u please try this

flint ferry
#

sure

gusty shard
flint ferry
#

IT WORKED

#

YUHHHHHHHHHHHH

#

TY

gusty shard
#

k

#

so

#

tl;dr

kindred grail
flint ferry
gusty shard
#

it was trying to do this

#

for some reason

flint ferry
#

oooooooh

gusty shard
flint ferry
#

i gotchu

#

i think ima go sleep

kindred grail
#

@gusty shard

kindred grail
#

*recode

kindred grail
gusty shard
#

but then he wouldnt learn it

kindred grail
#

Ok thanks

gusty shard
# kindred grail Ok thanks

the only reason i spent god knows how long asking him the right questions is to lead him into thinking the right way

#

and now guy went from that if spam to being able to implement sliding window algorithm 80% on his own

#

which is a major improvement if u ask me

flint ferry
#

ya that was very helpful

#

bro

#

i love code hs

#

cus its like

kindred grail
flint ferry
#

bro

#

it teaches alot

gusty shard
#

we nowhere near done

#

anyways

flint ferry
#

yep

gusty shard
#

ur row check works now @flint ferry

flint ferry
#

ya

gusty shard
#

u pick next direction

flint ferry
#

im very happy

#

sleep

gusty shard
#

input function

flint ferry
#

is the next

#

direction

gusty shard
#

more win con check

#

or wat

flint ferry
#

sleeping

#

i need sleep

#

😭

gusty shard
#

time.sleep(1)

#

now wat

flint ferry
#

naaa

#

i would say

#

time.sleep(28800)

#

na like

#

im gon go

#

to sleep

#

for the night

#

cus i ahve a haircut tmr

kindred grail
gusty shard
#

just ask away

flint ferry
#

ill just keep this post open

#

wait

#

does it get

#

deleted

#

now it wont

kindred grail
#

1h later it gets closed if no one speak

flint ferry
#

1 week

#

now if no one speek for 1 week

#

it will be closed

#

so i can go to sleep in peace now

#

cya

gusty shard
#

cya

kindred grail
#
some_code
    ╰──some annotation

Do you think a python script that generate annotation like this would be helpful for you to help people? @gusty shard

gusty shard
#

wdym annotation

#

just a description of what code does ?

kindred grail
#

Ye

#

Help explain the code

#

And issue

gusty shard
#

there could be uses but most of time u want people to think on their own so they actually learn the logic behind it

#

there is times where u just straight up explain a piece of code

#

but usually that comes with comments embeded in middle of code to explain each bit

kindred grail
#

Just checking if you think it's helpful for you because I notice you quite passionate about helping people

#

If you think it's helpful, I will send the code so you could use it

gusty shard
#

it also can be excruciating depending on person

#

but thats not here or there to talk about

kindred grail
#

It's slightly modded one for my pydroid (don't have the original one on me rn)

#

It's mainly useful for explaining error (i.e. chain of event that cause an error) for me

#

From conversation: #discord-bots message

toxic flint
#

wow, over 1500 messages in one help post 😲
this must be one of the longest posts i've seen yet

gusty shard
#

not on this server

#

but ye i was teaching guy how to think

#

so took a few hours

#

still a lot of room for improvement but infinietly better

toxic flint
#

yes, ignoring the code in the last big while True loop it is night and day compared to the first one

#

still a lot of placeholder code there, so not close to finished yet i see
a bit more work and refactoring and it probably be alright
but looks like this project has been a very good and valuable learning experience for the OP πŸ‘

gusty shard
#

today i even walked guy throught a sliding window algorithm

#

he improving

toxic flint
solemn roost
#

1,521 messages feelsWoahMan

gusty shard
solemn roost
gusty shard
nova nebulaBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.