Results 1 to 6 of 6
  1. #1
    clipper is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Help with my connect 4 game!!!

    Hey guys/gals i'm new to java here so bare with me. Alright so I'm making a connect four game and basically after many many - many hours, I have finally got it to compile. However i'm running into many logic errors that I cannot seem to figure out. I believe one of my errors lies at the beginning of my program.

    Java Code:
    public class Board {
    
    	final int NUMROWS = 6;
    	final int NUMCOLS = 8;
        
    	[b]Piece[][] theBoard = {Piece.BLACK, Piece.RED, Piece.EMPTY}[/b]
    
               ...
               ...
    If I write what I have in bold I get a compile-time error.

    the given instructions for that part is: "A 2-d array of type Piece called theBoard representing the piece on each square. The type Piece is defined using an enum in the file Piece.java and can take 1 of 3 values: RED, BLACK, EMPTY"

    What should be written instead?

    Thanks!

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    You need to define enum types in your Piece.java file. For more details about enums, refer here: Enum Types

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    clipper is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Hey goldest! the enum types are already defined in my Piece.java file!

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Seems like your multi-dimensional array is not declared in the right way.

    Here is a small example,

    Java Code:
    		int my2dArray[][] = { { 1, 2 }, { 3, 4 } };
    		for (int i = 0; i < my2dArray.length; i++) {
    			for (int j = 0; j < my2dArray[i].length; j++) {
    				{
    				System.out.println(my2dArray[i][j]);
    				}
    			}
    		}
    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    clipper is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    hey goldest! thanks for the help! I essentially made it this far:

    Java Code:
    public class Board {
    
    	final int NUMROWS = 6;
    	final int NUMCOLS = 8;
        
    	Piece[][] theBoard = new Piece [NUMROWS][NUMCOLS];
    	
    	{
    	
    	for(int i = 0; i < NUMROWS; i++) {
    	   
    		for(int j = 0; j < NUMCOLS; j++) {
    			
    			theBoard [i][j] = Piece.EMPTY;
    		}
    		
    	}
    But when i run it through my test program it says that I should be getting black or red as well, not just empty.

    What can I do so that I can pretty much declare that each of these spaces in the connect four game can have Piece.BLACK, Piece.RED, Piece.EMPTY?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by clipper View Post
    But when i run it through my test program it says that I should be getting black or red as well, not just empty.

    If you're getting an error message, then please post the actual message and the codes causing the error.

    What can I do so that I can pretty much declare that each of these spaces in the connect four game can have Piece.BLACK, Piece.RED, Piece.EMPTY?
    As far as I can tell, you've already done that. You have a 2-dimensional array of Piece, and each array can refer to any one of the three Piece enum objects.

Similar Threads

  1. Connect to wap
    By HoangNgoc in forum CLDC and MIDP
    Replies: 2
    Last Post: 12-23-2010, 09:13 AM
  2. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  3. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  5. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 06:34 AM

Posting Permissions

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