#π Connect four python program
1532 messages Β· Page 2 of 2 (latest)
rows is simplest
collumns is the next on complexity list
then last but not least diagonal
thats one is hell
oh damn
anyways
anyways
lets leave this scuffe winc on check for later
lets start on a rows win con check
cause thats simplest
bro is on drugs
row
mm ok
row = ["X", "X", "_", "_", "O", "O", "O"]
so
lets say we have this row
we want to start at the far left right
yes
yuh
from 0 to what number is the first set of 4 entries
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
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 ?
yes
lets slide that window over till it hits edge of list
what are the 4 numbers we looking at
for first entry yes
now slide that window over to the right till it hits end of list
and what we looking at now
yuh
what are the 4
are we adding it or
nah overriding
oh ok we're not
only 3
unless
we starting at
the second
_
fi so
if*
_ o o o
that is correct
that is the last 4 in the list
i can "animate" a sliding window for u if that doesnt make sense
na i can do that
with time
i know
i understand that type of stuff
i love slicing
X X _ _ ||O O O||
||X|| X _ _ O ||O O||
||X X|| _ _ O O ||O||
||X X _|| _ O O O
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
mmmmmm ok
does it make sense
im just denoting the indexes of where stuff appears
this type of stuff
now
yuh
btw im really happy about this idea of using spoiler to demonstrate the window
that shit was genius
loop over row so that it prints out all the windows of size 4
like in that spoiler message
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
that list slicing is correct tho yes
if i was an int that is
ye
should be this
row = ["X", "X", "_", "_", "O", "O", "O"]
for i in range(4):
print(row[i:i + 4])
thats closer
but clearly at one point it falls off and doesnt print 4
why u think it doesnt print 4
in here when i gets to 5 or more
it doesnt print 4 entries
why is that
technical answer is more than that but im give it to ya
ye
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)
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
what does set do
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'}
ok
play around with it
python is very fun
there we go
OF 4
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
it helps a lot when doing these kinds of things
so it prints both the set and unset
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
crazy
isnt it cool
aw
ye
u gotta loop throught board
to make sure u apply this to every row
i dont gotta go
lol
its 2 for m
anyways
u just never stopped once u found a connect 4
ahhh ok
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
yaaa
and even if u do find another
its prob the same player that somehow got 2 connect 4
at once
or 3
issue is that u never checked if whats in the len 1 set is either an X or an O
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
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
== not = inside the if
yep lol just got that error
anyways i got a funny idea for u
?
why
u dont want to mess up wth size of board
the easiest way here
oh ok nvm
is to check whats inside the set of lenght 1
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
u checking if its in the non-set version
ya
u most likely want to use the set version
so ye
u found issue
now to make sure it ignores when that string is only one in set
!e
a = {'|_|'}
if '|_|' not in a:
print('test')
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
u can see how code above works
u dont want to check it inside unset in case its something like _ _ _ X X X X
oh ok
hopefully u see why
copy the string it printed that it found inside set here
and paste it into ur if in code
WAIT
do i have to put
()
around it
nvm
uh
ok
oh wait
those are
{}
gang is this a dictionary
its a set
they use same brackets as dictionaries
sense
u not crazy
Wtf 1394 message
Anyone have tldr context
im teaching this guy how to think so he fixes his 1000 line yandere codebase
fucking god knows how long it has been
Greatπ
na bro
its great ye
would be less if his adhd didnt pivot him into win con check
can u post that function here
yes
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")
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
sure
wanna see his code from yesterday
Sure
ill send it
if '| _ |' not in (set_row_check_var and len(set_row_check_var) == 1):
it was trying to do this
for some reason
Lovely
oooooooh
ye python is lovely
I want an opinion: Surely recoding this is faster than the this 1.4k message right π€
@gusty shard
wdym recording this
wanna see it now
*recode
Also sure
ye it would be 10000000000x faster
but then he wouldnt learn it
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
Some improvement could exist but sure
ye
we nowhere near done
anyways
yep
ur row check works now @flint ferry
ya
u pick next direction
input function
naaa
i would say
time.sleep(28800)
na like
im gon go
to sleep
for the night
cus i ahve a haircut tmr
Can you chat in ot if this post closed for a bit?
guy going to sleep so this thread is p much dead
just ask away
1h later it gets closed if no one speak
1 week
now if no one speek for 1 week
it will be closed
so i can go to sleep in peace now
cya
cya
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
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
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
ye it can be fun
it also can be excruciating depending on person
but thats not here or there to talk about
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
https://paste.pythondiscord.com/JNCIZC3HWXFICW2AIGIXIUE7HU
Nice example of usage
From conversation: #discord-bots message
wow, over 1500 messages in one help post π²
this must be one of the longest posts i've seen yet
i seen ones with 4000+
not on this server
but ye i was teaching guy how to think
so took a few hours
yesterday code if u curious: https://paste.pythondiscord.com/Y6SA
today's code after guy went to sleep: https://paste.pythondiscord.com/QICQ
still a lot of room for improvement but infinietly better
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 π
ye def
today i even walked guy throught a sliding window algorithm
he improving
that is a good sign π
1,521 messages 
u can read explanation above for why its so many
where?
.
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.