Hello...
I'm trying to write a program in C (using POSIX library) where the parent process should fork up to M processes and play "connect four" or "four-in-a-row" with each-other until one of them wins, and when that happens, the process should terminate. This can be done either via pipes or message queues. The goal of the game is to be the first to place colored piece to make a sequence of 4 pieces in an NxN grid vertically, horizontally, or diagonally.
I'm sure the colors can be represented by simply putting R or B for red or blue, and I'm hoping the grid can be represented in a 2D array...

But I have no idea how to even get started with this problem...fork M processes? I made a mess of my code forking one process a few days back and that was just to write a few random lines of text into a pipe. Any ideas as soon as possible would be highly appreciated. Thanks.

Recommended Answers

All 3 Replies

Break it down into a few big steps (amid smaller one's):

1) Get your connect 4 program using one thread, working PERFECTLY. No crashes, no illegal moves, etc.

It's difficult to debug a multi-threaded program, so get the single threaded version of your code, working really spot on.

2) Now work your single-threaded code, into a multi-threaded program. Imo, let every thread work independently of the other, as much as possible.

3) Now polish it up, and test it out, REALLY well. You may find bugs long after you thought they were all gone.

There is all kinds of example code for both multi-threading and connect 4 on the net, so get in there and study up. This will take more time than you expect, so don't put it off.

thanks for the reply, i think i might be able to pull off the conversion from single to multi-threaded, but i need to figure out how i get to make the 2 processes communicate with each other repeatedly until a condition is reached (i.e. game won) and i still havn't figured out how to initialize the game itself...God save me

There might be a lot of info out there for Connect 4. For years, it was a competition game played between comp sci students in college.

In fact, and this may blow your mind but it's true. A guy named Fabien L developed one of the top C4 programs. Later, he used his mad skills, to write a chess program called "Fruit". By version 2.1, it was kicking most of the top chess programs arses. Today, it still serves as the basis for many of the top chess programs, including Rybka, Houdini, and others.

Most of the chess authors thought "he must have used someone else's code, it's a rip off", because they had never heard of anyone writing such a strong chess program, in such a short amount of time. But it was all due to his years of competing in C4 contests, and he got a bit lucky, no doubt.

As I understand it, you can use either message queues or pipes to communicate from one thread to the other, but I have no expertise in this area. Lots of info on line about this.

If I had to get an answer to something like this (after Googling and etc.), I'd check out www.talkchess.com/ because all the chess program authors are going "Deep" (using multiple threads, etc). This is where the top chess authors on the planet get together and argue. ;) Including Fabien L. Bob Hyatt, Gerd Isenberg, and many other well known chess programming masters.

I don't know how strongly you want your program to play, but maybe just a six or eight ply (half-move) depth Alpha beta search, would be enough.

There are many open source chess programs available to study, if you like. Some are multi-threaded as well, but I'd only use them as a last resort, because they are much more complex than what you're trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.