# Method for legal moves in connect four.

Printable View

• December 20th, 2009, 02:56 AM
mandar9589
Method for legal moves in connect four.
I have made a two player connect four console game in Java.
However I wish to extend this and make an AI for this game.

I have broken down the program in few steps.
Out of which I am stuck on the first step.

i.e the legal move generation. This method will contain an array of all the possible legal moves available to be made. Which would be then passed to the other decision making algorithm.

The problem I am facing is the method does not give all possible legal moves when invoked.
In the code I have used a method which will return an array to the decision making method.

The variables used are described below.
int row=>the current row.
int col=> the current col.
movesLegal[]=> One dimensional array of length 7,which will store the legal moves.This array would be then passed to the decision making method.(At any possible position, there are maximum of 7 or less than 7 legal moves but never more than 7.)

Right now this method is incomplete.

Please help.
-Thank you.
• December 25th, 2009, 07:47 PM
maikeru
Re: Method for legal moves in connect four.
I may be able to help, but I need more information. Aren't there 7 columns? Columns resemble vertical lines and rows resemble horizontal lines.

How are you using a single integer to represent the location of the move with:

movesLegal[col] = 7 * row + col;
movesLegal[col] = 7 * (row - 1) + col;
• December 27th, 2009, 12:12 AM
maikeru
Re: Method for legal moves in connect four.
Why not use an array of arrays, for example: board[6][7]? This way you could make a loop something like this:

@Code
For every column, starting at the last row and backing up, look for the next available empty spot and determine if it's a legal move, then add it if it meets these requirements.
Code :

```int legalMoveIndex = 0;   for( int columnIndex = 0; columnIndex < 7; columnIndex++ ) { for( int rowIndex = 5; rowIndex > 0 && foundLegal == false; rowIndex-- ) { if( board[row][column] == EMPTY && isLegal(row, column) = true ) //EMPTY is defined wherever to whatever { legalMoves[legalMoveIndex++] = new Move(column, row); //@ newMove(column, row); 'row' represents the depth or number of rows left that are legal and empty foundLegal = true; } }   foundLegal= false; }```

I must ask what represents an illegal move? I couldn't Google anything on it.
• December 27th, 2009, 04:28 PM
maikeru
Re: Method for legal moves in connect four.
Are you trying to say that if you know all the legal moves, then all other moves are illegal? I don't see how it's possible to make an illegal move in this game unless if a column is completely filled.
Quote:

So if legal moves are known then the illegal moves are all moves- legal moves
You can use a similar method to what I wrote above for using strings. Start at the last row string and test each char against the value that represents an empty space. Then, I guess take these moves found and test them for the legal status, whatever that is.