

Show HN: C4 – Connect Four, with AI - kenrick95
http://kenrick95.github.io/c4/demo/

======
kenrick95
How I build this project: It was mid-2013 when I wanted to apply what I've
learned from the MOOC CS188.1x Artificial Intelligence offered at edx.org.
Around December 2013, I joined a bootcamp held in my university (Nanyang
Technological University, Singapore) in which some of the speaker came from
Mozilla. Here, I learned a lot of stuffs on JavaScript (they also introduced
us to HN). Later, I thought of applying what I've learned to a game. I then
started the project and finished it during January 2014.

~~~
mrborgen
Nice job, do you recommend the Artificial Intelligence MOOC? Is it fun?

~~~
kenrick95
It's amazing. Although quite time-consuming, the projects are nice: it was
building an AI for Pac-man to maximize its score in various scenarios.

The next offering is starting soon:

[https://www.edx.org/course/artificial-intelligence-uc-
berkel...](https://www.edx.org/course/artificial-intelligence-uc-berkeleyx-
cs188-1x-0)

------
smilliken
We did a friendly programming competition at MixRank last year writing AIs for
Connect Four.

It's really a great game for playing with AI strategies since it's so simple.
You can borrow our game runner and write your own:
[https://github.com/smilliken/aigames/tree/master/connect-
fou...](https://github.com/smilliken/aigames/tree/master/connect-four)

You can use any language since it just talks over pipes. There's a few bots in
there you can play against.

~~~
smilliken
Also, if you'd like some light reading, Victor Allis proved a winning strategy
in 1988: [http://www.informatik.uni-
trier.de/~fernau/DSL0607/Masterthe...](http://www.informatik.uni-
trier.de/~fernau/DSL0607/Masterthesis-Viergewinnt.pdf). It's a great read, I
recommend it.

------
RMarcus
I wrote a C version of a connect 4 AI and used emscripten to build a
JavaScript / AngularJS version. You can check it out for comparison:
[http://rmarcus.info/blog/2014/12/23/connect4.html](http://rmarcus.info/blog/2014/12/23/connect4.html)

Edit --

Github is here:
[https://github.com/RyanMarcus/connect4](https://github.com/RyanMarcus/connect4)

Looks like my AI beats your AI. ;)

~~~
guidopallemans
Just beat yours at 5 moves ahead

~~~
RMarcus
I've been playing at 8 for a challenge. Can't win yet, but I can draw. I left
it at 5 as the default because it works well on tablets. :)

------
cl42
Not to be a pessimist, but Connect Four is a solved problem:
[http://en.wikipedia.org/wiki/Connect_Four](http://en.wikipedia.org/wiki/Connect_Four)
Wouldn't that mean the best AI should always win if it's first (alternatively,
you should always win if you're first)?

~~~
kenrick95
I'm aware of the fact that this is a solved problem. When I experimented by
making it my AI vs my AI, it did not use the optimal first move. This shows
that my AI is not perfect :)

------
vapter
Nice work! I was actually planning to do a similar project in the near future.
However, it troubles me that I was able to win when your AI didn't "block" me
when I put 3 aligned circles. Shouldn't that be the very first rule for such
an AI?

~~~
kenrick95
Normally it tries its best to avoid that situation, but sometimes your
scenario happens and I did not know why.

------
austinhutch
Finally got a win, took me about 10 rounds. Really cool thanks for sharing.

~~~
dirkk0
Had a win in the first round but that was pure luck. :-)

~~~
philbarr
Had a win in the first round because I played Connect 4 aggressively against
my brother for years when I was a kid!

------
mlamat
I won in the 2nd round.

------
lwgdhn
Every once in a while a new game pops up on HN and I always pay the price in
time. Not complaining!

------
hitchhiker999
Note to future readers: This is how we ended up training our new robot
overlords wasn't it?

------
bluehazed
Wow, I really suck at Connect Four.

------
kenrick
Wow another developer named kenrick :)

~~~
kenrick95
Hi, another Kenrick :)

------
noso
Really nice work and very cool

------
JamyDev
Nice, I just beat it :)

~~~
kenrick95
I admit that the AI is not perfect. :)

~~~
tromp
My Fhourstones solver and a perfect playing applet may be found at
[http://tromp.github.io/c4/c4.html](http://tromp.github.io/c4/c4.html)

------
alacritythief
I want to note that you can win every time by dropping wherever first, then on
your next moves dropping every piece on top of the AI's pieces.

~~~
inglesp
Unless I've totally misunderstood, I don't think you're right.

For instance, on your first go, drop in column 0. Then, the AI drops in
columns 1, 2, 3, 4, leading to this:

    
    
        . . . . . . .
        . . . . . . .
        . . . . . . .
        . . . . . . .
        . X X X . . .
        X O O O O . .

~~~
alacritythief
It could have been fixed after my post, I won 8 times in a row and it seemed
to be some kind of fluke.

After trying it now, the AI actually tries harder to beat you, and my previous
method is not effective anymore.

~~~
kenrick95
Just a clarification: The latest commit is around a month ago.
[https://github.com/kenrick95/c4](https://github.com/kenrick95/c4)

