Connect Four Game problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koonda
    New Member
    • Oct 2006
    • 34

    Connect Four Game problem

    Hi all,

    I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The Assignment is about creating a Connect Four Game. I have designed the board and two players can play and fill the board. But when one playe fills 4 columns that player should win. I have problems implementing it like this. The other problem is to decide about Vertical, Horizontal and Diagonal win. Would you guys be able to help me with these problems for connect four game. I really appreciate your help. Below is the hasWon() method which decides for Horizontal Win. But it is not working and it does not decide about the win and some times it give ArrayIndexOutof Bound Exception.




    //check for horizontal win

    public int hasWon()
    {
    int status = 0;


    for (int row=0; row<6; row++)
    {
    for (int col=0; col<4; col++)
    {
    if (ConnectFourArr ay[col][row] != 0 &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+1][row] &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+2][row] &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+3][row])
    {status = 1;}
    //status = true;//int winner;

    if(status == 1)
    {


    System.out.prin tln("Player 1 is the winner");
    }

    else if(status == 0)
    {


    System.out.prin tln("Player 2 is the winner" );
    }

    }//end inner for loop
    }// end outer for loop
    } // end method Winner

    return status;
    }





    Try to also send me some code so I can finish my project by then.

    Thanks a lot for your help. Looking forward to your reply.

    Koonda
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by koonda
    Hi all,

    I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The Assignment is about creating a Connect Four Game. I have designed the board and two players can play and fill the board. But when one playe fills 4 columns that player should win. I have problems implementing it like this. The other problem is to decide about Vertical, Horizontal and Diagonal win. Would you guys be able to help me with these problems for connect four game. I really appreciate your help. Below is the hasWon() method which decides for Horizontal Win. But it is not working and it does not decide about the win and some times it give ArrayIndexOutof Bound Exception.




    //check for horizontal win

    public int hasWon()
    {
    int status = 0;


    for (int row=0; row<6; row++)
    {
    for (int col=0; col<4; col++)
    {
    if (ConnectFourArr ay[col][row] != 0 &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+1][row] &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+2][row] &&
    ConnectFourArra y[col][row] == ConnectFourArra y[col+3][row])
    {status = 1;}
    //status = true;//int winner;

    if(status == 1)
    {


    System.out.prin tln("Player 1 is the winner");
    }

    else if(status == 0)
    {


    System.out.prin tln("Player 2 is the winner" );
    }

    }//end inner for loop
    }// end outer for loop
    } // end method Winner

    return status;
    }





    Try to also send me some code so I can finish my project by then.

    Thanks a lot for your help. Looking forward to your reply.

    Koonda
    1.) When posting code, please use code tags.
    2.) We won't write the codes and send them to you. We can only help you to write them correctly and efficiently.
    3.) If you are sure your design is correct, write your code part by part and post if you get problems on any specific area.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I simply repeat my previous answer from your other thread here and I'm really
      curious what you didn't understand about my hint:

      If your 'logical' board measures 7x6 you should use a 'physical' board of 9x8:
      Code:
      +---+---+---+---+---+---+---+---+---+
      | X | X | X | X | X | X | X | X | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X |   |   |   |   |   |   |   | X |
      +---+---+---+---+---+---+---+---+---+
      | X | X | X | X | X | X | X | X | X |
      +---+---+---+---+---+---+---+---+---+
      the 'X' values represent a non-empty square not taken by any of the two players.
      That way you don't need to do any explicit boundary checks in any of your
      methods. The logical playing board ranges from [1 ... 7] and [1 ... 6] and any
      cell outside that range is invalid (although it physically exists).

      w.r.t. my second remark: if none of the players has won and a current move
      causes one of the players to win then the position of that last current move
      is part of the winning configuration. If it weren't, a previous configuration would
      have been a winning situation for one of the players therefore contradicting the
      previous assumption that none of the players did win already.

      So checking in any possible directions starting from that last move is sufficient
      to find a possible winning configuration.

      kind regards,

      Jos

      Comment

      • koonda
        New Member
        • Oct 2006
        • 34

        #4
        Thank you all for your help. Now the game is working, I only have some problem to implement the Automatic player. I made the two human players working, they play with each and one wins. But I have a little problem with the Automatic Player I mean the Computer Player. The Automatic Player plays with the Human player but never wins. I tried my best to make very bad moves for the Human Player to let the Computer win but it won't. I just want the Automatic Player to win.

        Here is the code for the Automatic Play. Would you guys make some corrections in the code if it is wrong. But it still plays and generates random numbers.

        Code:
        public int move () { 
                try {
                    
                    Random automaticplayer = new Random();
                    
                    int randomnumber;
                    
                    randomnumber = automaticplayer.nextInt(6);
                    
                    return randomnumber;
        
                            } catch (Exception exn) {
                    return (this.move ());
                }
            }
        Again thanks a lot for your help I really appreciate that. Looking forward to your reply.

        Thanks

        Koonda

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          The optimal (or a near optimal) strategy for a connect-four game hasn't found yet
          so you have to fall back to sub-optimal heuristics here. One of the worst is a
          random move (as you did). Google for "alpha-beta pruning" for quite some clever
          heuristic game search routines.

          kind regards,

          Jos

          Comment

          • alidou
            New Member
            • May 2007
            • 1

            #6
            what is the code for 2D board to connect4?
            how many classes contains the projects connect4?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by alidou
              what is the code for 2D board to connect4?
              how many classes contains the projects connect4?
              Short answers:

              1) nobody knows; it's a secret.
              2) see above.

              Longer answers:

              you do realize that there are several implementations of that game, spread all
              over the world and every single implementation uses different ideas, methods
              and classes. All the implementations try to aim for the same thing: play the
              Connect-4 game, either with two human players, a human against the computer
              and even AI-based programs where two computers play against each other and
              both computers attempt to learn from their mistakes.

              I hope you realize that your question cannot be answered in a unique way.

              kind regards,

              Jos

              Comment

              • koonda
                New Member
                • Oct 2006
                • 34

                #8
                Thanks all for your help I really appreciate that.

                Koonda

                Comment

                Working...