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

Thread: Connect 4 Help

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

    Default Connect 4 Help

    I have my connect four game but the checkwin algorithms won't work I have two other files called GameBoard and BoardSquare. The BoardSquare file sets the colors and types for the checkered pieces and the GameBoard file sets the 2d array...Can anyone tell me whats wrong?
    public class ConnectFour extends JFrame {
    	// making a gameboard object
    	static JLabel label = new JLabel(" ");
    	static GameBoard board = new GameBoard();
    	static public int i=2;
    	static final int empty = 1;
    	static boolean win = false;
    	static int CurrentPlayer = 0;
        public ConnectFour() {
        	setLayout(new BorderLayout());
    		// putting board on frame
    		add(board,BorderLayout.CENTER);
    		add(label,BorderLayout.NORTH);
        }
     
        public static void main(String[] args) {
    		//setting my frame to the connect four frame
        	JFrame frame = new ConnectFour();
        	frame.setSize(700, 600);
        	frame.setLocationRelativeTo(null); // Center the frame
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
     
        }
     
        public static void updateBoard(int row, int col){
     		//checks to see if spot is empty
     		//two seprate loops to find bottom square in column
     		//
    		BoardSquare clicked = board.Array[row][col];
    		//if its empty then change the players turn
    		int l = row-1;
    		while(row<5 && board.Array[row+1][col].GetType() == empty){
    			clicked = board.Array[row+1][col];
    			row = row+1;
    		}
        	if (clicked.GetType() == 1){
    			clicked.setType(i);
    			if (i==3){
    				i=2;
    				label.setText("red's turn");
    				checkWin(board.Array);
    			}
    			else{
    				i=3;
    				label.setText("blacks turn");
    				checkWin(board.Array);
    			}
        	}
    		}
    		public static void checkWin(BoardSquare [][]board){
    			int numMove = 42;
    			win = false;
    			checkHorizontal(6, 7);
    			checkVertical(6, 7);
    			checkBackDiag(6, 7);
    			checkDiag(6, 7);
     
     
     
    //checks for tie game
    			 if(win == false && numMove == 6*7){
    				System.out.println("Tie");
    }
    }
    	public static void checkHorizontal (int row, int column){ // checks for a horizontal win
    			int count = 0;
     
    			for (int i = 0; i < 6 && board.Array[row][i].GetType() == CurrentPlayer; i++){
    				count++; // increase count for every continuous piece from the same player
    			}
     
    			for (int i = 0; i >= 0 && board.Array[row][i].GetType()== CurrentPlayer; i--){
    				count++;
    			}
    			if (count >= 4){ // if count is greater then the number of continuous pieces required to win, the current player is declared the winner
    				win = true;
    				System.out.print("you win");          //declares a win
    			}
    			}
     
    //checks for a vertical win
    	public static void checkVertical (int row, int column){
    			int count = 0;
    			for (int i = row; i < row && board.Array[column][i].GetType() == CurrentPlayer; i++){
    				count++; // increase count for every continuous piece from the same player
    			}
     
    			for (int i = row; i < row && board.Array[column][i].GetType() == CurrentPlayer; i--){
    				count++;
    			}
    			if (count >= 4){
    				win = true;
    				System.out.print("you win");        //declares a win
    }
     
    }
    	public static void checkDiag (int row, int column){
    			int count = 0;
    			int j;
    			for (i = row, j = column;i >= 0 && j < 6 && board.Array[i][j].GetType()==CurrentPlayer;i--,j++){
    				count++;
    				}
    			for (i = row, j = column;i >= 0 && j < 6 && board.Array[i][j].GetType()==CurrentPlayer;i++,j--){
    				count++;
    }
    			if (count >=4){
    				win = true;
    				}
    				}
    	public static void checkBackDiag(int row, int column){
    			int count = 0;
    			int j;
    			for (i = row, j = column;i >= 0 && j < 6 && board.Array[i][j].GetType()==CurrentPlayer;i++,j++){
    				count++;
    			}
    			for (i = row, j = column;i >= 0 && j < 6 && board.Array[i][j].GetType()==CurrentPlayer;i--,j--){
    				count++;
    			}
    			if (count >= 4){
    				win = true;
    				System.out.println("Winner");
    }
     
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Connect 4 Help

    Can anyone tell me whats wrong?
    You first...does it compile? Are there exceptions? Does it misbehave, and if so what do you expect and what do you get?

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

    Default Re: Connect 4 Help

    It Compiles I got the checkHorizomntal code working by changing it to
    public static void checkHorizontal (){ // checks for a horizontal win
     
    			for (int row = 0; row < 6; row++) {
    				for (int col = 0; col < 4; col++) {
    					int count = 0;
    					for (int i = 0; i < 4 && board.Array[row][col+i].GetType() == CurrentPlayer+2; i++){
    						count++; // increase count for every continuous piece from the same player
    					}
     
    					if (count >= 4){ // if count is greater then the number of continuous pieces required to win, the current player is declared the winner
    						win = true;
    						System.out.print("you win");          //declares a win
    					}
    				}
    			}
     
    			}
    But now I'm getting an exception in my vertical code where if i place all the checkers in a single column I dont get an exception, but if I place two of the same colored pieces on top of each other I get exception java.lang.ArrayIndexOutOfBoundsException: 6
    My vertical code is
    	public static void checkVertical (){
    			for(int col = 0; col < 7; col++){
    				for(int row = 0; row< 6; row++){
    					int count = 0;
    					for (int i = 0; i < 4 && board.Array[row+i][col].GetType() == CurrentPlayer+2; i++){
    				count++; // increase count for every continuous piece from the same player
    				}
     
    			if (count >= 4){
    				win = true;
    				System.out.print("you win");        //declares a win
    }	}
    			}
    }

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Connect 4 Help

    Well onto a potential issue I see. Ok your nested loops do these part on paper.
    // Single chip near the top
    Column 0 - 6
    Row 5
    i = 1 && board.Array[row + i][col].getType()
     
    // 2 Same chips so adds to i to check 3rd chip
    Column 0-6
    Row 4
    i = 2 && board.Array[row + i][col].getType()
     
    // 3 Same chips so adds to i to check 4th chip
    Row = 3
    i = 3 && board.Array[row + i][col].getType()

    What row will it be searching for? I am thinking this is where your Out of bounds is happening

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect 4 Help

    Okay I got it all working thank you Ubiquitous, but how would I got about making the GUI in-editable once a winner is declared?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Connect 4 Help

    setEnabled
    Improving the world one idiot at a time!

Similar Threads

  1. Can't connect to the database.
    By djokovic in forum JDBC & Databases
    Replies: 6
    Last Post: February 5th, 2013, 03:57 PM
  2. Connect Four GUI
    By gromacs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 09:42 AM
  3. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  4. Cannot Connect to Server
    By Brt93yoda in forum Java Networking
    Replies: 0
    Last Post: December 9th, 2010, 05:54 PM

Tags for this Thread