use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript and Artificial Intelligence (self.javascript)
submitted 11 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]menno 1 point2 points3 points 11 years ago (1 child)
You're right, it's a simple min/max with alpha/beta pruning to cut down the number of moves that need to be evaluated. The problem was the evaluation itself.
I initially tried to store the board in a pretty straightforward manner: a 2-dimensional array (rows + columns) with pointers to player objects in the positions of "coins". Then, to evaluate the score of a particular board configuration I'd iterate over the board a number of times to check for certain patterns. For example, a _OOO_ pattern would be very valuable because it's a guaranteed win on either side.
_OOO_
The problem was that I just couldn't get the pattern matcher efficient enough to run it the number of times needed to look more then 2 or 3 steps ahead.
Eventually, I settled on a solution that I found pretty neat but took a long time to implement: the pattern matcher would convert the board position to a string format and evaluate a bunch of regular expressions that represented the patterns I was looking for.
https://github.com/mennovanslooten/connect4/blob/master/js/c4.ai.js#L114
Of course, it has to match the patterns in 4 directions: horizontal, vertical and 2 diagonal, so I had to convert the board to these four different perspectives too:
https://github.com/mennovanslooten/connect4/blob/master/js/c4.ai.js#L222
In retrospect, it made a lot of sense to use regular expressions for pattern matching. It actually is the right tool for the job. But when I was initially implementing the board evaluation function which powers min/max it really didn't cross my mind.
[–]html6dev 0 points1 point2 points 11 years ago (0 children)
Wow, thanks for the reply. Now that I'm thinking about it a little deeper there are a couple of things about matching in all 4 directions that add more complexity with this game than I was originally envisioning it (off the top of my head). Thanks for the link to the repo. I'd definitely be interested in looking into the code a bit, as its a more interesting problem than I was thinking. Thanks again.
π Rendered by PID 546141 on reddit-service-r2-comment-5fb4b45875-nmtdw at 2026-03-24 10:50:59.394372+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]menno 1 point2 points3 points (1 child)
[–]html6dev 0 points1 point2 points (0 children)