Page 1 of 3 123 LastLast
Results 1 to 20 of 49
Like Tree1Likes

Thread: How to make a simple connect four program WITHOUT gui

  1. #1
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Unhappy How to make a simple connect four program WITHOUT gui

    My code below is trying to make a connect four program without GUI. I'm having trouble with getting the players to make their moves. What should I put in my "makeAMove" method? Can someone help me, please?

    CODE SO FAR

    Java Code:
    public class Connect {
    	
    
    	// Jaylynn Jenkins
    	// Project 2
    	// 12/2/14
    	
    	final static int MAXROW = 6;
    	final static int MAXCOL = 7;
    	
    	public static void main(String[] args){
    
    
    	// Gameboard
    	char [][] board = new char [MAXROW][MAXCOL];	
    	
    	makeAMove();
    	clearBoard(board);
    	displayBoard(board);
    	
    	}	
    	 public static void makeAMove() {	
    		
    		 
    		       
    	        
    } 
    	 public static void displayBoard(char[][] board) {
    		// Keyboard as input
    			Scanner keyboard = new Scanner (System.in);
    		 System.out.println("Welcome to Connect-Four!");
    		 System.out.println();
    	        for (int row = 0; row < MAXROW; row++) {
    	            System.out.print("| ");
    	            for (int col = 0; col < MAXCOL; col++)
    	                System.out.print(board[row][col] +"| ");
    	            System.out.println();
    	        }
    	        for (int col = 0; col < MAXCOL; col++)
    	            System.out.print("---");
    	        	System.out.println();
    	        	System.out.println(" 1 | 2| 3| 4| 5| 6| 7");       
    	 }  
    	 public static void clearBoard(char[][]board){
    		 for(int row=0;row<MAXROW;row++){
    			 for(int col=0;col<MAXCOL;col++){
    				 board[row][col]=' ';
    			 }
    		 }
    	 } 
    	 
    		 //Check win horizontally
    		 public boolean Winner1 (char[][]board){
    			 for(int row=0;row<MAXROW;row++){
    				 for(int col=0;col<MAXCOL-3;col++){
    					 char p = board [row][col];
    					 if((p==board[row][col+1]) && (p==board[row][col+2]) && (p==board[row][col+3])){
    				 return true;
    			 }
    		}
    				
    	}
    	return false;	
    		 }
    
    	
    	
    		//Check win vertically
    	 public boolean Winner2 (char[][]board){
    		 for(int col=0;col<MAXCOL-3;col++){
    			 for(int row=0;row<MAXROW;row++){
    				 char p = board [row][col];
    				 if((p==board[row++][col]) && (p==board[row++][col]) && (p==board[row++][col])){
    			 return true;
    		 }
    	}
    			
    }
    		return false;
    	 }
    		
    		//Check win diagonally
    		public boolean Winner3 (char[][]board){
    			 for(int row=0;row<MAXROW-3;row++){
    				 for(int col=0;col<MAXCOL-3;col++){
    					 char p = board [row][col];
    					 if((p==board[row+1][col+1]) && (p==board[row+2][col+2]) && (p==board[row+3][col+3])){
    				 return true;
    			 }
    		}
    				
    	}
    	return false;
    		}
    	
    			//Check win for other diagonal
    		public boolean Winner4 (char[][]board){
    			for(int row=MAXROW-1;row>3;row--){
    				for(int col=0; col<MAXCOL-3;col++){
    					char p = board [row][col];
    					 if((p==board[row-1][col+1]) && (p==board[row-2][col+2]) && (p==board[row-3][col+3])){
    						 return true;
    				}
    			}
    		}
    		return false;
    		}
    }
    Last edited by lynn775; 11-19-2014 at 09:36 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    Just did.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    What should I put in my "makeAMove" method?
    Can you describe what the method is supposed to do? What input does it take? What output does it return?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    I need my makeAMove method to prompt a red player to make a move on the game board (display board), as well for a yellow player. After each move, the game will check for winning conditions. I just can't put the logic into this method



    I need this output:

    Welcome to Connect-Four!

    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    -------------
    1 2 3 4 5 6 7
    Red player choose a column (1-7)
    Last edited by lynn775; 11-19-2014 at 10:02 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    I need my makeAMove method to prompt a red player to make a move
    Does the method ask the user where to move and then make the move that the user has chosen?
    What does the user enter for the method to read? One or more numbers? A letter? What?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    The red or yellow user will be prompted for their column choice, and then their choice will be entered onto the board.

  8. #8
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    something like this:

    Welcome to Connect-Four!

    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 1 (R), choose a column (1-7): 1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 2 (Y), choose a column (1-7): 2
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R |Y | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    AND THEN BACK TO PLAYER 1 (R) Player 1 (R), choose a column (1-7):

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    The Scanner class has methods for reading user input.

    If the user gives the program the column for his move, how does the program determine what row to place the move at?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    I have this so far now:


    Java Code:
    public class ConnectFourGame {
    
    	// Jaylynn Jenkins
    	// Project 2
    	// 12/2/14
    
    	static final int ROWS = 6;
    	static final int COLS = 7;
    	static Scanner keyboard;
    	static char[][] board;
    	static char[] piece = { 'R', 'Y' };
    
    	public static void main(String[] args) {
    		// Keyboard as input
    		keyboard = new Scanner(System.in);
    
    		// Gameboard
    		board = new char[ROWS][COLS];
    
    		System.out.println("Welcome to Connect-Four!\n");
    
    		clearBoard();
    
    		int won = 0;
    
    		while (true) {
    			displayBoard();
    			if (makeAMove(1)) {
    				won = 1;
    				break;
    			}
    
    			displayBoard();
    			if (makeAMove(2)) {
    				won = 2;
    				break;
    			}
    		}
    
    		System.out.println("Congratulations, Player " + won + ", you have won!");
    		keyboard.close();
    	}
    
    	/* returns true if move has won the game */
    	private static boolean makeAMove(int player) {
    
    		int choice;
    		while (true) {
    			do {
    				System.out.print("\nPlayer " + player + " (" + piece[player - 1] + "), choose a column (1-7): ");
    				choice = keyboard.nextLine().charAt(0);
    				if (choice == 'q') System.exit(0);  // exit on "quit" or "q"
    			} while (choice < '1' || choice > '7');
    
    			// turn char into COL index
    			choice -= 49; // '1' -> 0, '2' -> 1, etc
    
    			// insert returns false if column is already full
    			if (insert(choice, player))
    				break; // new piece was inserted, exit loop here
    			System.out.println("Column " + (choice + 1) + " is already full.");
    		}
    
    		/* finally, perform winning condition check and return result */
    		return fourFound();
    	}
    
    	/* check if last move has completed four in a row */
    	private static boolean fourFound() {
    		// TODO: check for four in a row
    		return false;
    	}
    
    	/* if col already full, return false */
    	private static boolean insert(int col, int player) {
    		if (board[0][col] != ' ')
    			return false;
    		for (int row = ROWS - 1; row >= 0; row--) {
    			if (board[row][col] == ' ') {
    				board[row][col] = piece[player - 1]; // 1 -> '1', 2 -> '2'
    				break;
    			}
    		}
    		return true;
    	}
    
    	private static void displayBoard() {
    
    		for (int row = 0; row < ROWS; row++) {
    			System.out.print("| ");
    			for (int col = 0; col < COLS; col++)
    				System.out.print(board[row][col] + " | ");
    			System.out.println();
    		}
    		for (int col = 0; col < COLS; col++)
    			System.out.print("----");
    		System.out.println();
    		System.out.println("  1 | 2 | 3 | 4 | 5 | 6 | 7");
    	}
    
    	public static void clearBoard() {
    		for (int row = 0; row < ROWS; row++) {
    			for (int col = 0; col < COLS; col++) {
    				board[row][col] = ' ';
    			}
    		}
    	}
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    What happens when you compile and execute the program?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    Player 1 (R), choose a column (1-7): 1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 2 (Y), choose a column (1-7): 2
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | Y | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 1 (R), choose a column (1-7): 1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | Y | | | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 2 (Y), choose a column (1-7): 4
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | Y | | Y | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 1 (R), choose a column (1-7): 1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | | | | | | |
    | R | Y | | Y | | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 2 (Y), choose a column (1-7): 5
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | | | | | | |
    | R | Y | | Y | Y | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 1 (R), choose a column (1-7): 1
    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | | | | | | |
    | R | | | | | | |
    | R | Y | | Y | Y | | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Now I need to check for a winning condition, because as you realize, there are 4 consecutive R's in column 1

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    Ok, what does the code need to do to solve that?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    I already have that part, now I just need help with checking for a winning condition. Diagonally, vertically, and horizontally, and keep the score of each player

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    Ok. Are you having problems?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    Yes, I need to check for winning conditions diagonally, vertically, and horizontally. So aren't those for loops? Check this for me:

    Java Code:
    //Checking horizontally
    
    public boolean Winner (char [][] board){
    	for(int row=0;row<ROWS;row++){
    		for(int col=0; col<COLS-3;col++){
    			char p = board[row][col];
    			if((p==board[row][col+1] && (p==board[row][col+2]) && (p==board[row][col+3]))){
    		return true;
    			
    			}
    		
    	}
    	}
    	return false;
    }
    }

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    What happens when the code is compiled and executed?

    Hint for easier testing: Load a String of the users' responses into the Scanner class's constructor:
    Java Code:
      keyboard = new Scanner("1\n2\n3\n4\n5\n6\n7\n1\n2\n3\n4\n5\n6\n1\n2\n3\n4\n5\n6\n2");
    Change what I have there to something shorter for quick tests. Later change the input for more complicated tests.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    It still doesn't say who the winner is, I need the round to stop when there are 4 consecutive game pieces in a column like below. It needs to display the winner after a win, and then prompt for another round:


    | | | | | | | |
    | | | | | | | |
    | R | | | | | | |
    | R | | | | Y | | |
    | R | | | | Y | | |
    | R | Y | | | Y | R | |
    ----------------------------
    1 | 2 | 3 | 4 | 5 | 6 | 7

    Player 2 (Y), choose a column (1-7):

  19. #19
    lynn775 is offline Member
    Join Date
    Nov 2014
    Posts
    25
    Rep Power
    0

    Default Re: How to make a simple connect four program WITHOUT gui

    My code so far:

    Java Code:
    public class ConnectFourGame {
    
    	// Jaylynn Jenkins
    	// Project 2
    	// 12/2/14
    
    	static final int ROWS = 6;
    	static final int COLS = 7;
    	static Scanner keyboard;
    	static char[][] board;
    	static char[] piece = { 'R', 'Y' };
    
    	public static void main(String[] args) {
    		// Keyboard as input
    		keyboard = new Scanner(System.in);
    
    		// Gameboard
    		board = new char[ROWS][COLS];
    
    		System.out.println("Welcome to Connect-Four!\n");
    
    		clearBoard();
    
    		int won = 0;
    
    		while (true) {
    			displayBoard();
    			if (makeAMove(1)) {
    				won = 1;
    				break;
    			}
    
    			displayBoard();
    			if (makeAMove(2)) {
    				won = 2;
    				break;
    			}
    		}
    
    		System.out.println("Congratulations, Player " + won + ", you have won!");
    		keyboard.close();
    	}
    
    	/* returns true if move has won the game */
    	private static boolean makeAMove(int player) {
    
    		int choice;
    		while (true) {
    			do {
    				System.out.print("\nPlayer " + player + " (" + piece[player - 1] + "), choose a column (1-7): ");
    				choice = keyboard.nextLine().charAt(0);
    				if (choice == 'q') System.exit(0);  // exit on "quit" or "q"
    			} while (choice < '1' || choice > '7');
    
    			// turn char into COL index
    			choice -= 49; // '1' -> 0, '2' -> 1, etc
    
    			// insert returns false if column is already full
    			if (insert(choice, player))
    				break; // new piece was inserted, exit loop here
    			System.out.println("Column " + (choice + 1) + " is already full.");
    		}
    
    		/* finally, perform winning condition check and return result */
    		return fourFound();
    	}
    
    	/* check if last move has completed four in a row */
    	private static boolean fourFound() {
    		// TODO: check for four in a row
    		return false;
    	}
    
    	/* if col already full, return false */
    	private static boolean insert(int col, int player) {
    		if (board[0][col] != ' ')
    			return false;
    		for (int row = ROWS - 1; row >= 0; row--) {
    			if (board[row][col] == ' ') {
    				board[row][col] = piece[player - 1]; // 1 -> '1', 2 -> '2'
    				break;
    			}
    		}
    		return true;
    	}
    
    	private static void displayBoard() {
    
    		for (int row = 0; row < ROWS; row++) {
    			System.out.print("| ");
    			for (int col = 0; col < COLS; col++)
    				System.out.print(board[row][col] + " | ");
    			System.out.println();
    		}
    		for (int col = 0; col < COLS; col++)
    			System.out.print("----");
    		System.out.println();
    		System.out.println("  1 | 2 | 3 | 4 | 5 | 6 | 7");
    	}
    
    	public static void clearBoard() {
    		for (int row = 0; row < ROWS; row++) {
    			for (int col = 0; col < COLS; col++) {
    				board[row][col] = ' ';
    			}
    		}
    	}
    	
    	
    	
    	//Checking for consecutive 4
    	
    	public static void checkConsecutiveFour(){
    		
    		
    	}
    
    
    
    //Checking horizontally
    
    public boolean Winner (char [][] board){
    	for(int row=0;row<ROWS;row++){
    		for(int col=0; col<COLS-3;col++){
    			char p = board[row][col];
    			if((p==board[row][col+1] && (p==board[row][col+2]) && (p==board[row][col+3]))){
    		return true;
    			
    			}
    		
    	}
    	}
    	return false;
    }
    
    
    
    //Checking vertically
    
    public boolean Winner2(char[][]board){
    	for (int col=0;col<COLS-3;col++){
    		for(int row=0;row<ROWS;row++){
    			char p = board[row][col];
    			if((p==board[row++][col] && (p==board[row++][col]) && (p==board[row++][col]))){
    				return true;
    		}
    	}
    }
    	return false;
    }	
    }

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to make a simple connect four program WITHOUT gui

    Time for some debugging. Make the simplest String for the Scanner's input. Add some println() statements in the code that is testing for a winner so you can see what the computer sees when the program is executed. Execute the code and look at the output to see where the logic is wrong. I assume you know what the code is supposed to do, so seeing what it does should help you fix it.

    For faster testing, preload the array with 4 winning moves and call the "find a win" method to see if it can find the win in the array.
    Last edited by Norm; 11-20-2014 at 05:02 PM.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 03-14-2013, 11:39 AM
  2. Make my main program simple
    By unknown2 in forum New To Java
    Replies: 3
    Last Post: 02-20-2013, 12:28 AM
  3. Replies: 0
    Last Post: 05-20-2012, 10:15 AM
  4. Replies: 11
    Last Post: 01-25-2012, 03:06 PM
  5. Replies: 4
    Last Post: 07-23-2011, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •