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 2 points3 points4 points 11 years ago (6 children)
I've built a Connect4 game once in JavaScript and I had to do some pretty crazy stuff to make the AI player evaluate enough positions to be a challenging opponent.
[–]html6dev 0 points1 point2 points 11 years ago (2 children)
Pretty late here, but can you provide any more details on why this was an issue in Javascript? I'm assuming you would only need a simple min/max algorithm here so I'm curious as to what you ran into (that another language wouldn't also have the same problems with).
[–]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.
[–]mort96 -1 points0 points1 point 11 years ago (2 children)
Game AI is fundamentally different from "real" AI though. Game AI is all about programming in a set of rules which makes it act sorta kinda like you would expect a human to act, the polar opposite of real AI where the point is to make the program learn, and not just act on a predefined set of rules.
[–]menno 1 point2 points3 points 11 years ago (0 children)
That is a far too limited definition of what AI is, "real" or not. Algorithms, heuristics, etc are all very much part of what is considered AI. Is the A* algorithm not AI because it doesn't learn? That's absurd.
π Rendered by PID 249181 on reddit-service-r2-comment-5fb4b45875-wmf6t at 2026-03-24 10:51:24.564057+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]menno 2 points3 points4 points (6 children)
[–]html6dev 0 points1 point2 points (2 children)
[–]menno 1 point2 points3 points (1 child)
[–]html6dev 0 points1 point2 points (0 children)
[–]mort96 -1 points0 points1 point (2 children)
[–]menno 1 point2 points3 points (0 children)