Hello,
I'm designing a C++ Program for a connect 4 game. I have the matrix set up, the test to determine if there is a winner, and now I need to design the computer to go against the player. This seems to be quite a challenge. First, I need to make it block if there are 3 in a row. So far, I can't even accomplish that....

I made a function, and it looks like this:

int computertest(char matrix[6][7])
{
int row,column,b;
b=9;
for (row=5;row>2;row--)
{
for(column=0;column<7;column++)
{
if (matrix[row][column]==matrix[row-1][column] && matrix[row][column]== matrix[row-2][column] && matrix[row][column]!='-' && matrix[row-3][column]=='-')
{
b=column;
return b;
}

This is only the first part of the program...


the '-' is what I use to display my matrix. I put it '-' for blank spaces, so the player knows what space is open.

Now..when I test it...the computer can't seem to place an 'X' when there is a vertical threat (when there are 3 "O's" in a row vertically up)..the computer just blanks out and takes a very long time to respond.

Can anyone tell me what is wrong?

Also, can someone teach me how I could program a computer to "think"?...I looked it up on the internet and you're supposed to put "weights" on certain columns...like if there is a potential threat, then the computer puts something to counter it early on.

Hey zellex,

I wrote a connect 4 AI agent a while ago. You can play it at
http://students.csci.unt.edu/~jdl0107/conn4.exe

I know our code is different, but this is a link to my source code for my AI:
http://students.csci.unt.edu/~jdl0107/leon.cpp

You are welcome to look through it. My leon::move method is the one that has all the AI. At the beginning of the method I documented the different ways it chooses which space to move. It doesn't use any recursion(I never got around to it). Adding some recursion to determine the move was proven to beat my method. Anyway, I hope you can get some use out of it.

Uh, sorry, but what can I use to open your file? I used note pad, but all the code is in a straight line and it takes me awhile to shift it. Could you elaborate more on how you made it?
*EDIT*
I realized that you had some things that tell which space to move...but could you correspond that to a certain part in your program?

For example, "Can I make a double threat?" - [code] whatever code you had [\code] so I can see it work? >.<. Sorry, I'm more or less a beginner at this..

Do you have AIM so I can chat with you about your program?

Comments
dumass request

bool is like true or false, 1 or 0.

AI for connect four = simple as pie. What's the problem.

Are you using any learning networks? Hmm.

what does

bool bad_move[7]

do?

It creates an array of bool.

what does the bool do overall?

Answer already given in previous post.

Comments
ff6767@yahoo.com

The problem is that I don't know many strategies...Also, the program that nanodano gave me is something very foreign to me, so I'm trying to decipher it.
For example, how do you make the computer see if it can make a double threat? (Please include the concept if you are answering my question...like...for instance, the worst move to make in connect four is a move where you place a piece in a column and if the opponent places his or her piece on top of the piece you just placed, then the opponent will win. So, concept wise, you create a copy of the board, put your piece, then put the opponent's piece, and test to see if the opponent will win.)
I do not quite understand how nanodano tests for that in his program...would anyone like to give me a few strategies?

*FYI, I started programming at the beginning of this year, and I learned matrices about a month ago...so this is kinda hard for me..*

its very difficult to explain how to make the game.. if u ve any doubts they can be solved easily.. it is about scanning ur matrix in which u re storing ur current board postions.. and for every scan get some parameters on the basis of which to define the move.. with these parameters u then calculate ur move and carry on..

Anyone know what's the best move for the second player if it is his first move?

Moreover, does anyone know of an efficient was of making a double threat?

This article has been dead for over six months. Start a new discussion instead.