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

Thread: Connect Four FAIL....

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connect Four FAIL....

    Lab 7 - #2

    Implement a Connect 4 game. The program should allow two players to play each other. For each turn, have the user enter the column they want to play in, validate the user has made a good move, and then place the piece on the board. Then the program should check for a winner and a tie, and assuming there is neither, redisplay the board and get the next players move. Display the moves using print/printf/println commands, and use an X for player 1 and an O for player 2. When the game ends, the program should display congratulations to the winner or announce there was a tie, and then allow the players to choose to play again or exit.

    You must use a 2D array for this lab.


    I believe i am on the right track but i am getting lost, any help would be so appreciated.


    import java.util.Scanner;
    public class Lab7
    {
    	public static void main(String args[])
    	{
    		// create board
    		int cFBoard[][] = new int[7][8];
    		Scanner input = new Scanner(System.in);
    		//loop
     
     
    			//init board
     
     
    			// currentPlayer = 1, numMoves = 0;
    			int cPlayer = 1;
    			int pAgain;
    			int nMoves=0;
     
     
    			//LOOP
     
    			// display board
    			System.out.println("\t1\t2\t3\t4\t5\t6\t7");
     
    			for (int r = 1; r <= 6; r++)
    			{
    				System.out.print(r);
     
    				for(int c = 1; c <= 7; c++)
    				{
    					System.out.print("\t" + cFBoard[r][c]);
    				}
    					System.out.println();
    			}
     
    				int c;
     
    			// get move
    			do
    			{
     
    					System.out.print("Input desired column: ");
    					  	C = input.nextInt();
     
    			}
    			while (c < 1||c > 7|| cFBoard[6][c] !=0);
     
     
     
     
    			// place piece
     
     
    			for(int r = 1; r<=6; r++);
    			 {
     
    				if (cFBoard[r][c]==0)
    				{
     					cFBoard[r][c] = cPlayer;
     					playerConvert();
     
    				}
     
    			 }
    			//Check winner
    	}
    }
     
     
    class playerConvert
    {
    public String convert(int cPlayer)
    		{
    			if (cPlayer == 1)
    			{
    				return "X";
    			}
    			else if (cPlayer == 2)
    			{
    				return "O";
    			}
    			else
    			{
    				return "-";
    			}
    		}
    }


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four FAIL....

    I even have these notes but i am still lost.

    Lab 7 notes

     
    ALGORITHM
    // create board
    // LOOP	
    	// init board
    	// currentPlayer = 1, numMoves = 0;
    	LOOP UP TO 42 TIMES
    		// display board
    		// get move (includes validation)
    		// place piece
    		// check winner
    		// check tie
    		// change currentPlayer
    	END LOOP
    	// Display congratulations/tie
    	// do you want to play again
    // END LOOP
     
    DATA NEEDED
    Int board[][]
    Int currentPlayer
    bool playAgain
    int numMoves
     
    GAME BOARD
    int board [] [] = new int[7][8]	
    Using rows 1-6 and columns 1-7 and ignoring element 0
     
    	1	2	3	4	5	6	7
    6	6,1	6,2	6,3	6,4	6,5	6,6	6,7
    5	5,1	5,2	5,3	5,4	5,5	5,6	5,7
    4	4,1	4,2	4,3	4,4	4,5	4,6	4,7
    3	3,1	3,2	3,3	3,4	3,5	3,6	3,7
    2	2,1	2,2	2,3	2,4	2,5	2,6	2,7
    1	1,1	1,2	1,3	1,4	1,5	1,6	1,7
     
    DISPLAYING BOARD
    See the last in class problem
     
     
    CONVERT ROUTINE
    public String convert(int i)
    {
    	if (I == 1)
    {
    		Return “X”;
    }
    	else if (I == 2)
    {
    		Return “O”;
    }
    	else
    {
    		Return “-”;
    }
    }
     
    VALIDATE MOVE
    do
    {
    // get move
    }
    while (col < 1 || col <> 7 || board [6][col] != 0);
     
    PLACEMENT
    for(int i = 1; i<=6; i++);
     {
    	if (board[i][col]==0)
    	{
    		board[i][col] = current player;
    		break;
    	}
     }
     
    HORIZONTAL CHECK FOR WIN
    for (int r = 1; r < = 6; r++)
    {
        for int c = 1; c <=4; c++)
    	    {
     	        if (board [r][c] == board [r][c+1] && 
    board[r][c+1] == board[r][c+2] &&                                        
    board[r][c+2] == board[r][c+3] && 
                  board[r][c] != 0)
            {
                  win = true;
             }
         }
    }

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

    Default Re: Connect Four FAIL....

    Ask a specific question, get a specific answer. Ask a vague question (or none at all), and get a link to the following:
    http://www.javaprogrammingforums.com...-get-help.html
    I recommend reading the above, and use the suggestions within to provide context and questions to your post, which always increases your chances of receiving the help you want.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four FAIL....

    I dont have a specific question, there is to many to ask. I have no idea how to do this.... i was fine with if statements and loops. but as soon as i go to arrays i loose it all.

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

    Default Re: Connect Four FAIL....

    Quote Originally Posted by melinko928 View Post
    I dont have a specific question, there is to many to ask. I have no idea how to do this.... i was fine with if statements and loops. but as soon as i go to arrays i loose it all.
    That last sentence gives an idea as to where you are stuck. Here are some links that might help
    http://www.javaprogrammingforums.com...e-posting.html
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. Connect Four GUI
    By gromacs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 09:42 AM
  2. Need help to connect to PostgreSql
    By stab in forum JDBC & Databases
    Replies: 3
    Last Post: June 3rd, 2011, 09:01 AM
  3. Why do these things fail sometimes? Annoying program!
    By Scotty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 3rd, 2011, 07:28 AM
  4. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  5. applet fail
    By wolfgar in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 21st, 2010, 06:24 AM