#π how do i evaluate a connect 4 board position?
101 messages Β· Page 1 of 1 (latest)
@topaz aspen
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
any help is appreciated, might have late replies π
iβve also heard of minimax and done some research
i just need a way to grade a board and iβll be set
well and how did that go?
it went pretty well
i watched a sebastian lague video on it and i understand the tree searching stuff and recursion a little bit now
i just need a way to evaluate the board and i'll be set
but how tf do you evaluate a connect 4 board
I frankly don't have a clue myself, but have you heard about Code Bullet? he mostly does entertainment videos related to programming, but there are specs of technical stuff or, for example, where he has managed to copy the code from π
he happens to have a video on connect 4 (it's pretty far from "AI" despite the title...)
https://www.youtube.com/watch?v=XRVA5PMSKKE
oh yeah i watched that and it's what inspired me to do this project, but i didnt understand the video in terms of technical stuff. thats why i wanted to come here
the tablebase got me 4 moves into the game π
oh wait im dumb
i can just use the sources of what code bullet used
oh wait
i got the test cases from there π
i still dont get how you evaluate a position though, thats the part im struggling with
I'm not entirely sure I will be able to help you with that right now, so uhh... yeah
oh alr
who should i ask?
honestly all I can suggest is just wait for someone to show up and after some time maybe bump it in python general or if this gets automatically closed, open a new post
alr will do 
you want to evaluate win conditions?
:incoming_envelope: :ok_hand: applied timeout to @topaz aspen until <t:1713992114:f> (10 minutes) (reason: emoji spam - sent 42 emojis).
The <@&831776746206265384> have been alerted for review.
how are you storing the board state? As a 2d array?
mods, do you mind unmuting? They're trying to demonstrate their board's state through emojis and tripped the bot
!unmute 566653183774949395
:incoming_envelope: :ok_hand: pardoned infraction timeout for @topaz aspen.
pls fewer emojis
that was a fast 10 minutes
lets find out
just take a picture and send that though
don't worry about showing me the board. I think I can help you regardless
is it a 2d list?
yes
say i had a position like this
how would i evaluate it?
like how you can eval chess positions
let's figure this out together
we should go top to bottom to avoid checking everything every single iteration
(in case you dont know)
yes 
hold on brb just grabbing smth
my first guess is to iterate through the outer list until we find a row that isnt empty. Then check the spaces around it.
i saw someone saying you should check for chained pieces and evaluate it that way
wait, we can optimize further
how in the hell do you map out 8 directions for each of the 42 pieces
yes
well, obviously if we make another move, the positions of the board that weren't near the new piece won't suddenly become wins
so we don't need to evaluate the entire board, just the positions near the new piece
(and so on, checking for a win condition)
like, hold on let me illustrate
i got a half board but its kinda dodgy
This should give you some ideas to build of from
my eyes already hurt
Just read it, it's not using any complicated terminology
no like
small text
we are just trying to evaluate if the current board is in a win state right?
Ah, zoom function
the only thing im stuck on is evaluating the board and giving it a score π
everything else is easy after that
yep π
hold on brb
right, say you put a piece in the last column. Obviously the pieces in the first column won't need to be evaluated again, because they've already been evaluated in the last move
also i just read about connect4 but correct me if i am wrong u basically win if u manage to connect 4 dots either horizontally, vertically, or diagonally
so what you should do is:
- place a piece
- check the spaces immediately surrounding the placed piece (so left, down-left, down, down-right, right)
- repeat step 2 for the piece next to it if the colour matches, and do so until until either there are 4 continuous pieces of the same colour in a straight line, or all "routes" are exhausted. Remember though, they must be in a straight line, so you should have a second "checking" function for straight lines, in addition to the initial proximity check
that is correct
yes
ohh ok i think i see it now
i smell DSA somewhere in here
wait but how does this tie into evaluating the board? do i just count the number of 3s, 2s and just 1s with a relative score like 1 pt for each line of 1, 5 pts for each line of 2 and 10 pts for each line of 3 with infinity (or not at all) if theres 4 because thats a win anyway
do u have a few test cases? @topaz aspen
uh, i had some test cases
you can get them here, lemme get a link
they dont have a file extension which is kinda odd
whne you place a piece on the board, that's the only part of the board that changes. Parts of the board that are unaffected by the change won't magically become relevant to determining whether or not the win condition has been met, and you'll only know once you check. Since you have to start the check somewhere, the best place would be to check where the board saw a change (which is where the new piece was placed)
Notation and score of Connect 4 positions
ty
they have the moves and then an eval
its supposed to calibrate your solver but idfk how to calibrate it π
ah ok i see that. but my question still remains: how do you grade the board? how can i tell the difference (numerically in terms of score) between one board to another?
like with that board piece change, how can i tell if the position is better for the bot or for the opponent?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.

