March 2012

Arduino Plays Connect4

I was playing a game of Connect4 with my daughter one day and it occurred to me there might be a nice way of sensing the moves in the physical game in order to introduce a computer player. Looking at the top of the game board, I thought there just might be enough room to place photo interrupters in each position to watch as the pieces are dropped. It turns out the GP1A57HRJ00F sold at Sparkfun fits nicely.  I figured an Arduino would work nicely to provide the computer player for the game, so I set out to create a circuit board of the right geometry for the game board.
I would have liked to use this as an opportunity to learn Eagle and to have a board made but it appeared that the dimensions of my board exceeded those allowable for the free edition -- not because of the complexity of the circuit but rather to make it fit the game board.  So I decided to mill the board with my CNC instead using some copper clad board.  I started to use Google SketchUp to do that layout using the free g-code generation plug-in from Phlatboyz and realized that although I liked using SketchUp to preview the layout, generating it parametrically with software would be easier and allow me to more easily make adjustments.  So I just coded a Ruby plug-in to SketchUp that did the layout I needed and made calls to the Phlatboyz routines for g-code generation as necessary.  I have some videos of the milling at the end of the post.
Other than power and ground, the design uses a single line for each of the seven slots. The lines both control the LED and deliver the sense data from the photo interrupter. The design is very simple consisting solely of the photo interrupters, resistors, and LEDs. The biggest challenge was the geometry of the slots (for the checkers to drop) and the mounting of the photo interrupters.  The LEDs would provide a way to indicate where the computer player wanted to move.  But I thought it might be nice to make the game more conversational, so I added a Wave Shield from Adafruit.  I prepared a script of some things the computer player might say during the game and my daughter had a fun time recording the clips which were then downloaded to the SDCard on the shield.  I then had to design the computer player's AI -- I wrote a Minimax search.  But I soon discovered with my daughter's play with the game that I'd better limit the look-ahead of the computer player and make it easier for people to win.  Actually, playing against an Arduino with my 5 year old daughter's voice is somewhat disarming ;-) I took a couple quick video examples -- below the human wins.
And in the second video, the Arduino wins. I stepped up to an Arduino Mega when I added the Wave Shield (which required some minor tweaking) because I needed the extra lines. I used an old Sun speaker as I have on several of my other projects. You may notice in the second video that one of the LEDs has a loose connection and isn't lighting. In some sense, play can tolerate a single LED failure if you know which column to go to when you don't see one lit ;-)
The video below shows the isolation routing being performed.
The next shows the holes being drilled with a dremel.
Next, the cuts are made.
And the last shows the finished unpopulated board.
That's a quick walk through of the project. I'd be happy to share full details including the board design, layout code, and Arduinocode. Let me know if there's interest and I'll add more detail.