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

Thread: ConnectFour

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ConnectFour

    We have to make a ConnectFour game in java for a project. I think I have managed to code most of it properly (Let me know if this is wrong). I''m having trouble figuering out how to decide who wins. I know you can win connect 4 horizontally, vertically, and diagonally( if the board size allows it).
    Here's the link for reference: CMSC 202 - Spring 2013 - Project 2

    Here's my ConnectFour.java class

    package proj2;
     
    public class ConnectFour 
    {
    	private String player1 = "";
    	private String player2 = "";
    	private int boardArea = 0;
    	private int playerPieces = 0;
    	private boolean turn = true;
    	private int rows = 0;
    	private int columns = 0;
    	private int spot = 0;
    	Move[][] board = new Move [rows][columns];
     
     
    	public ConnectFour( int rows, int columns, String player1, String player2 )
    	{
    		this.rows = rows;
    		this.columns = columns;
    		this.player1 = player1;
    		this.player2 = player2;
     
     
    		for(int i = 0; i < rows; i++)
    		{
    			for( int j = 0;  j < columns ; j++)
    			{
    				board[i][j] = Move.EMPTY;
    			}
    		}
     
    	}
     
    	public int getBoardArea()
    	{
    		boardArea = rows * columns;
    		return boardArea;
    	}
     
    	public void turn(int columns, boolean playerTurn)
    	{//sorts through the column by rows. Starting at the top row.
     
    		if(board[0][columns] != Move.EMPTY)
    		{
    			throw new RuntimeException("Columns is full. Try again.");
    		}
     
    		for(int i = 0; i < rows ; i++ )
    		{
    			if(board[i][columns] != Move.EMPTY)
    			{
    				spot = i;
    			}
    		}
     
    	    if(playerTurn == true)//if it's player ones turn place piece 
    	    						//on the board
    		{
    			board[spot][columns] = Move.PLAYER1;
    		}
     
    		else if(playerTurn == false)//if it's player twos turn place piece 
    									//on the board
    		{
    			board[spot][columns] = Move.PLAYER2;
    		}
     
    	}
     
    	public void setBoardArea()
    	{
    		//if(boardArea )
    	}
     
    	public void setPieces(int redPieces, int blackPieces )
    	{
    		//sets red and black pieces to players. P1 gets red P2 gets black.
    		playerPieces = redPieces;
    		playerPieces = blackPieces;
    	}
     
    	public void winCondition()//checks if a player has won
    	{
     
    	}
     
     
    }

    Here's my Enum Move

    package proj2;
     
    public enum Move {
    	 PLAYER1, PLAYER2, EMPTY
    }

    Here's my Project2.java class

    package proj2;
    import java.util.Scanner;
    public class Project2 {
     
    	/** 
    	 * This class drives the Connect Four game.
    	 * @version 3/10/2013
    	 * @author Nathan Graddick <grad1@umbc.edu>
    	 * @project CMSC 202 - Spring 2013 - Project 2
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String selection = "";
    		String player1 = "";
    		String player2 = "";
    		int rows = 0;
    		int columns = 0;
    	    boolean move = true;
     
     
     
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Player1: Enter your name:");
    		 player1 = scan.nextLine();
     
    		System.out.println("Player2: Enter your name:");
    		player2 = scan.nextLine();
     
    		System.out.println("How many rows do you want?");
    		rows = scan.nextInt();
     
    		System.out.println("How many columns do you want?");
    		columns = scan.nextInt();
     
    		ConnectFour board = new ConnectFour(rows, columns, player1, player2);
    		System.out.println("The area of the board is: " + board.getBoardArea());
     
    		do
    		{
    			/*for(int i = 0; i < rows; i++)
    			{
    				board[i][i] = scan.nextLine();
    				for(int j = 0; i < columns; i++)
    				{
    					board[j][j] = scan.nextLine();
    				}
    				System.out.println(columns);
    			}
    			System.out.println(rows);*/
     
     
     
    		}while(!selection.equalsIgnoreCase("Q"));//exits the do while loop
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    What is the input to the game? What should the user enter to the program's prompts?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ConnectFour

    My fault. The users are prompt to input their names and then they are asked to input the size of the board that they want to play on. However, the board must have at least 25 spaces. So you can have a board of 4x7 (I don't know why you would want that) but if it was 4x4 it would not work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    What problems are you having with the code now?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    A T.A pointed out that I have a problem with my for loop in my turn method in the ConnectFour Class. I keep getting an out of bounds error and he thinks that this may be the case as I have no way of exiting the for loop. I know I need to return a condition to exit it, but if I do I break my program. Also, for the life of me, I've no idea how to write the toString method so I can display the board. It has to look like the example in the link.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    I keep getting an out of bounds error
    Why are you getting that error? You need to fix that before going on.
    Where does the error happen? What index is out of bounds? What is the full text of the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    I get this error.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at proj2.ConnectFour.<init>(ConnectFour.java:27)
    at proj2.Project2.main(Project2.java:36)

    I know I need to fix it, but I'm not sure how. The compiler says the problem happens where I put a comment at. I'm unsure why it's wrong and how to fix it.

    public ConnectFour( int rows, int columns, String player1, String player2 )
    	{
    		this.rows = rows;
    		this.columns = columns;
    		this.player1 = player1;
    		this.player2 = player2;
     
    		for(int i = 0; i < rows; i++)//Makes the entire board empty
    		{
    			for( int j = 0;  j < columns ; j++)
    			{
    				board[i][j] = Move.EMPTY;//problem happens here
    			}
    		}
     
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    java.lang.ArrayIndexOutOfBoundsException: 0
    The error message says that an index of 0 is past the end of the array. That means that the array has NO elements.
    It's empty.
    Check where the code defines the array and make sure that it defines the array with a positive size, not zero.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    So, I think I fixed the problem. I copied that for loop and put it in a setter method. That fixed the out of bounds problem. As, of now I'm working on a toString method to display the state of the board. Thank you for you help.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    OK. We'll wait for your next problem.

    That fixed the out of bounds problem.
    Is the array now being defined with a positive size, instead of 0?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    Yes. I have also managed to get a toString method to print out the board. All I need to do now is figuring out how to do the win conditions and properly displaying the players pieces on the board.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    One step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    So....I hit another snag. I'm trying to come up with conditions on wining in all 3 directions. I know I need a separate method for each, but what I have isn't working. I'm getting an out of bounds error again. I am at an absolute loss as to what do to.

    package proj2;
     
    public class ConnectFour 
    {
    	private String player1 = "";
    	private String player2 = "";
    	private int boardArea = 0;
    	private String playerPieces = "";
    	private boolean turn = true;
    	private int rows = 0;
    	private int columns = 0;
    	private int spot = 0;
    	private String dash = "-";
    	private int MIN_BOARD = 25;
    	Move[][] board;
     
     
     
     
    	public ConnectFour( int rows, int columns, String player1, String player2)
    	{
    		this.rows = rows;
    		this.columns = columns;
    		this.player1 = player1;
    		this.player2 = player2;
    	}
     
    	public int getBoardArea()
    	{
    		boardArea = rows * columns;
    		return boardArea;
    	}
     
    	public void setBoard()
    	{
    		 board = new Move [rows][columns];
    		for(int i = 0; i < rows; i++)//Makes the entire board empty
    		{
    			for( int j = 0;  j < columns ; j++)
    			{
    				board[i][j] = Move.EMPTY;
    			}
    		}
    	}
     
    	public int turn(int columns, boolean playerTurn)
    	{
    		//sorts through the column by rows. Starting at the bottom row.
    		boolean spotFound;
    		int win = 1;
     
    		if(board[0][columns] != Move.EMPTY)
    		{
    			throw new RuntimeException("The column is full. Try again.");
    		}
     
    		spotFound = false;
    		for(int i = board.length -1; i >= 0; i-- )
    		{
    			if(board[i][columns] == Move.EMPTY && spotFound == false)
    			{
    				spot = i;
    				//creates a boolean flag to get out of the loop
    				spotFound = true;
    			}
    		}
     
    	    if(playerTurn == true)//if it's player one's turn place piece 
    	    						//on the board
    		{
    			board[spot][columns] = Move.PLAYER1;
    			//after you place the piece calls win method to see if they won
    			win = winCondition(true);
    		}
     
    		else if(playerTurn == false)//if it's player two's turn place piece 
    									//on the board
    		{
    			board[spot][columns] = Move.PLAYER2;
    			//after you place the piece calls win method to see if they won
    			win = winCondition(false);	
     
    		}
    	    if (win == 0)
    	    {
    	    	return 0;
     
    	    }
    	    else
    	    	return 1;
     
     
    	}
     
     
    	public int winCondition(boolean playerTurn)//checks if a player has won
    	{
    		int win = 0;
    		if(playerTurn == true)
    		{
    		  win = checkVertical(true);
    		  if(win == 0)
    		  {
    			  System.out.println("You win!");
    			  return 0;
    		  }
    		  else return 1;
    		}
    		else
    		{
    		  win = checkVertical(false);
    			  if(win == 0)
    			  {
    				  System.out.println("You win!");
    				  return 0;
    			  }
    			  else return 1;
     
    		}
     
    	}
     
    	public int checkVertical(boolean playerTurn)
    	{
    		int count = 0;
     
    		if(playerTurn == true)//checks to see if playerOne has won vertically
    		{
    			for(int i = 0; i < rows && board[columns][i] == Move.PLAYER1; i++)//this line is the problem
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
     
    		else//checks to see if playerTwo has won vertically
    		{
    			for(int i = 0; i < rows && board[columns][i] == Move.PLAYER2; i++)
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				System.out.println("You win!");
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
    	}
     
    	public int checkHorizontal(boolean playerTurn)
    	{
    		int count = 0;
     
    		if(playerTurn == true)//checks to see if playerOne has won vertically
    		{
    			for(int i = 0; i < columns && board[rows][i] == Move.PLAYER1; i++)
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				System.out.println("You win!");
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
     
    		else//checks to see if playerTwo has won vertically
    		{
    			for(int i = 0; i < columns && board[rows][i] == Move.PLAYER2; i++)
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				System.out.println("You win!");
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
    	}
     
    	/*public int checkDiagonally(boolean playerTurn)
    	{
    		int count = 0;
     
    		if(playerTurn == true)//checks to see if playerOne has won vertically
    		{
    			for(int i = 0; i < columns && board[rows][i] == Move.PLAYER1; i++)
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				System.out.println("You win!");
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
     
    		else//checks to see if playerTwo has won vertically
    		{
    			for(int i = 0; i < columns && board[rows][i] == Move.PLAYER2; i++)
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				System.out.println("You win!");
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}
    	}*/
     
    	public String toString()
    	{
    		String str = "";
    		for(int i = 0; i < rows; i ++)
    		{
    			for(int j = 0; j < columns; j++)
    			{
    				if(board[i][j] == Move.EMPTY)
    				{
    					str += dash;
    				}
     
    				if(board[i][j] == Move.PLAYER1)
    				{
    					str += "X";
    				}
     
    				if(board[i][j] == Move.PLAYER2)
    				{
    					str += "O";
    				}
    			}
    			str += "\n";
    		}
     
    		return str;
    	}	
    }

    This is the line that the compiler says is broken:

    if(playerTurn == true)//checks to see if playerOne has won vertically
    		{
    			for(int i = 0; i < rows && board[columns][i] == Move.PLAYER1; i++)//this line is the problem
    			{
    				count++;
    			}
    			if(count > 3)
    			{
    				return 0;
    			}
    			else
    			{
    				return 1;
    			}
    		}

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    I'm getting an out of bounds error
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ConnectFour

    My apologies:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at proj2.ConnectFour.checkVertical(ConnectFour.java:1 59)
    at proj2.ConnectFour.winCondition(ConnectFour.java:13 1)
    at proj2.ConnectFour.turn(ConnectFour.java:99)
    at proj2.Project2.main(Project2.java:57)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ConnectFour

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at proj2.ConnectFour.checkVertical(ConnectFour.java:1 59)
    The index to the array at line 159 is past the end of the array. The code should make sure it does not use an index past the end of an array.
    Array indexes range from 0 to the array length-1
    If you don't understand my answer, don't ignore it, ask a question.