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 2 of 2

Thread: Discuss this code with me ..."Connect4 game"

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

    Default Discuss this code with me ..."Connect4 game"

    hello everyone
    this assignment is exercise to Control Structures and this is my try!!
    I hope if you degree this

    package connect4;
     
    import java.util.Scanner;
     
    /**
     *
     * @author HanyMoh.
     */
    public class Connect4 {
     
        public static void main(String[] args) {
            Scanner S = new Scanner(System.in);
     
            System.out.println("Welcome to connect4");
            System.out.println("Select game type");
            System.out.println("Enter 1 for \"Player vs. Computer\"");
            System.out.println("Enter 2 for \"Player vs. Player\"");
     
            // prepar empty board //****************************/
            char[][] board = new char[6][7];
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[i].length; j++) {
                    board[i][j] = ' '; // one space = empty input             
                }//endfor j            
            }//endfor i
     
            // select game type //*****************************/
            int game_type = S.nextInt(); // [1]= player+cpu , [2]=player+player
            System.out.println("Starting game");
     
            char input;
            int turn = 1;// turn42
            boolean x = true;
            while (x = true) {
                if (turn % 2 == 1) {
                    System.out.print("Player 1: ");
                    input = 'X';//input player1
                } else {
                    System.out.print("Player 2: ");
                    input = 'O';// input player2
                }
     
                int col;
                if (game_type == 1 && turn % 2 == 0) {
                    col = 1 + (int) (Math.random() * 7); // cpu player
                    System.out.println(col);
                } else {
     
                    // input col index           
                    col = S.nextInt();
                    if (col < 1 || col > 7) {
                        System.out.println("Invalid place, play again");
                        continue; // to play again
                    }
                }
                col--; // because input equal index
     
                // input row index
                int row = 5; // last index row
                // get count of all inputs in col           
                for (int i = 0; i < board.length; i++) {
                    if (board[i][col] == 'X' || board[i][col] == 'O') {
                        row--;
                    }
                }
                //if full col
                if (row < 0) {
                    System.out.println("Invalid place, play again");
                    continue; // to play again
                }
     
                // insert the input by row & col
                board[row][col] = input;
     
                // print board 
                System.out.println(" 1 | 2 | 3 | 4 | 5 | 6 | 7 |");
     
                for (int i = 0; i < board.length; i++) {
                    for (int j = 0; j < board[i].length; j++) {
                        System.out.print(" " + board[i][j] + " |");
                    }//endfor j
                    System.out.println();
                }//endfor i
     
                turn++;
     
     
                /**
                 * ************* checks **************
                 */
                if (turn > 42) {// 67=42 full board inputs
                    System.out.println("Draw!");
                    break;
                }
     
                // check col 
                int count = 0;
                for (int i = 0; i < board.length; i++) {
                    if (board[i][col] == input) {
                        count++; // count if sequance
                        if (count == 4) {
                            break;
                        }//end if
                    } else {
                        count = 0; //not sequance start again
                    }//end if
                }
     
                if (count != 4) {
                    count = 0;// start count again
     
                    // check row
                    for (int i = 0; i < board[row].length; i++) {// row length
                        if (board[row][i] == input) {
                            count++; // count if sequance
                            if (count == 4) {
                                break;
                            }// if=4
                        } else {
                            count = 0; //not sequance start again
                        }//if = input
                    }//endfor
                }// if<> 4
     
                if (count != 4) {
                    count = 0;
     
                    // check diagonal[\] 
                    int down = board.length - row - 1;
                    int r = row + down;
                    int c = col + down;
     
                    // if out of board range
                    while (r >= 0 && c >= 0) {
                        if (!(c <= 6) || !(r <= 5)) {
                            c--;
                            r--;
                            continue;
                        }
     
                        // check count of diagonal
                        if (board[r][c] == input) {
                            count++;
                            if (count == 4) {
                                break;
                            }
                        } else {
                            count = 0;
                        }
                        // move up
                        r--;
                        c--;
                    }// end while
     
                }
     
                if (count != 4) {
                    count = 0;
     
                    // check secnd diagonal[/] 
                    int up = board.length - row - 1;
                    int rw = row + up;
                    int cl = col - up;
     
                    // if out of board range
                    while (rw >= 0 && cl >= 0) {
                        if (!(cl <= 6) || !(rw <= 5)) {
                            cl++;
                            rw--;
                            continue;
                        }
     
                        // check count of diagonal
                        if (board[rw][cl] == input) {
                            count++;
                            if (count == 4) {
                                break;
                            }
                        } else {
                            count = 0;
                        }
                        // move up
                        rw--;
                        cl++;
                    }// end while
     
                }// end if 
     
                // print winner player
                if (count >= 4) {
                    int player = 1;
                    if (turn % 2 == 1) {
                        player = 2;
                    }
                    System.out.println("Player " + player + " is the Winner!");
                    break;
                }// if print
     
            }//end while
        }//end main
    }// end class


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 689 Times in 676 Posts

    Default Re: Discuss this code with me ..."Connect4 game"

    Some random thoughts:

    That's a lot of code to have in one method, but perhaps you haven't learned how to add more methods yet.

    It seems to run okay, but I chose '2' for the Player vs. Computer, and then I don't know what I'm supposed to do. Some instructions or a Help option would be nice. I then won by entering 4, 5, 6, 7, and the computer's moves didn't seem to take my moves into account. In fact, the computer only made 3 moves, probably not moving after I'd won.

    You get extra points for commenting your code.

    'x' is not a good name for a boolean variable. Give it a meaningful name.

    The statement(s?) like

    while ( x = true )

    are incorrect. Think about it. The reason they don't result in an error (I think) is because the value of 'x' is irrelevant to the loop. Also,

    while ( x )
    or
    while ( true )

    would be sufficient as long as 'x' is a boolean.

    There are too many magic numbers. Some are explained, but don't use them at all if you don't have to. Use variables like 'rows', 'columns', and 'squares' = 'rows' x 'columns' etc. . . .

    Programmers are lazy (or seek to be efficient), so repeated code, like

    if ( count != 4 )

    is a signal that there's a better way.

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

    hanymoh (September 17th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  2. "Ask a Question" menu item dumps into "What's Wrong With My Code?"
    By GregBrannon in forum Forum Updates & Feedback
    Replies: 1
    Last Post: August 6th, 2013, 03:37 AM
  3. Replies: 6
    Last Post: February 11th, 2013, 01:08 PM
  4. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM

Tags for this Thread