Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Method for legal moves in connect four.

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by mandar9589; January 1st, 2010 at 05:31 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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;

  3. The Following User Says Thank You to maikeru For This Useful Post:

    mandar9589 (December 26th, 2009)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.
    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.
    Last edited by maikeru; December 27th, 2009 at 01:14 AM.

  5. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.
    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.

Similar Threads

  1. How to Connect to an Excel Spreadsheet using JDBC in Java
    By JavaPF in forum JDBC and Database Tutorials
    Replies: 14
    Last Post: August 27th, 2013, 11:57 AM
  2. Connect 4 involving minimax.
    By mandar9589 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2009, 10:52 AM