• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making connect 4 game  RSS feed

 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



If I write what I have on line 6 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 defi ned using an enum in the fi le Piece.java and can take 1 of 3 values: RED, BLACK, EMPTY"

What should be written instead?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm probably not going to give you the advice you want to hear, but...

If you have spent "many, many hours" getting it to compile, you're not programming right. Most programmers will tell you to only write one or two lines and a time before you recompile. That way, each time you re-compile, you know within one or two lines where the error is. After you get it to compile, you test it to make sure it works. If there is a logic problem, you know the problem exists on those one or two lines. Once you are SURE that is working, you add a few more lines.

Having said that...

Think about the real-world problem. Not java code, but what you'd see in real life. The board, when you first start, is a 8x8 array of empty pieces. It's not until the game starts that you get red or black pieces in.

So, you need to first create a board with 64 positions, and then you need to specifiy that each of those positions is empty.

Piece[][] theBoard = new Piece[8][8];

Now..."theBoard" here is a pointer to an array that can hold 64 pieces - but there are no actual pieces there, red, black or empty. you need to fill the board with 64 "Piece.Empty" objects.

 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright thanks for the tip! Programming is all new to me and I just haven't realized that I should take it one line at a time. Suggestion noted though !

I'll try that and get back to you!
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the logic behind a connect 4 game? Have you written it down? It needs to be written very simply, not in terms a three-year old can understand. Any three-year old can understand "get four in a line" but you have to get a computer to understand it. Remember the pattern-recognition capabilities of the three-year-old's brain and eye are far in excess of a computer's.
If you haven't got that worked out, what is the point in writing any code? You would only have to change it when you work out the logic.

If you have a board looking like this, can you tell whether it is a winning board or not?
rrrx
bbrx
bbrr
brbr

How are you going to program something to accept such an input?

I would suggest you create a Connect4Square class, and a Counter enum with the values RED BLACK (or RED BLACK BLANK). Each square would start with BLANK (or null). You will have to work out how to connect squares to one another, and also that a square cannot have BLACK or RED if the square beneath it is BLANK. You might put the columns into a linked list to achieve that. You might want methods like Square#hasSameColourLeftAndRight(). Can you think of classes which represent four counters in a row, eg HorizontalLine, ObliqueLineUpLeft?
Remember you only have to test whether the last counter entered is a winning counter.
 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright so I did what you told me to do (Or what I think I needed to do).


So I believe I'm making progress over here! Before when I just wrote line 7 and compiled, and ran it through a test program it told me that I was getting null when i should get empty, then I added the for loops to fill the board with Piece.Empty. But when I ran it through the test it said I should expect black but not empty. Is there a way that I can fill it and declare that it can be either black red or empty?
 
Justine Trudeau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is the logic behind a connect 4 game? Have you written it down? It needs to be written very simply, not in terms a three-year old can understand. Any three-year old can understand "get four in a line" but you have to get a computer to understand it. Remember the pattern-recognition capabilities of the three-year-old's brain and eye are far in excess of a computer's.
If you haven't got that worked out, what is the point in writing any code? You would only have to change it when you work out the logic.

If you have a board looking like this, can you tell whether it is a winning board or not?
rrrx
bbrx
bbrr
brbr

How are you going to program something to accept such an input?

I would suggest you create a Connect4Square class, and a Counter enum with the values RED BLACK (or RED BLACK BLANK). Each square would start with BLANK (or null). You will have to work out how to connect squares to one another, and also that a square cannot have BLACK or RED if the square beneath it is BLANK. You might put the columns into a linked list to achieve that. You might want methods like Square#hasSameColourLeftAndRight(). Can you think of classes which represent four counters in a row, eg HorizontalLine, ObliqueLineUpLeft?
Remember you only have to test whether the last counter entered is a winning counter.


Hey Campbell, I have written the full program and I am just debugging it right now, I have not listed all my methods so far! :P
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justine Trudeau wrote:
So I believe I'm making progress over here! Before when I just wrote line 7 and compiled, and ran it through a test program it told me that I was getting null when i should get empty, then I added the for loops to fill the board with Piece.Empty. But when I ran it through the test it said I should expect black but not empty. Is there a way that I can fill it and declare that it can be either black red or empty?


This code probably won't do anything because your loops aren't even in a method! I'm surprised it even compiles; if anyone can explain why it does I'd be grateful. Put them in a Board() constructor. You could make the constructor take the colour of the piece you want it to initially fill with, but I don't see a good reason for you wanting to fill it with anything other than blanks.

Also, you might like to try using an IDE like NetBeans or Eclipse - it will save you the hours of trying to get it compile, because any errors will be highlighted as soon as you type them.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, since Justine's a beginning programmer, I would recommend just using a text editor, rather than a full IDE. I believe that if you have to do all that stuff manually, you will learn a language much more quickly. Later, when you're more confident in your skills, an IDE is a great tool.

As for why the loops are working outside a method, as you can see they are surrounded by curly braces. This is called a initializer block. These blocks of code are run when a new instance of the class is created, and they execute just before the constructor body is executed.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justine Trudeau wrote:Is there a way that I can fill it and declare that it can be either black red or empty?
What are you filling your board with? If I understand correctly, you fill it with Pieces. Doesn't each Piece already have code to make it red, black or empty? So shouldn't that already be done?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, since Justine's a beginning programmer, I would recommend just using a text editor, rather than a full IDE. I believe that if you have to do all that stuff manually, you will learn a language much more quickly. Later, when you're more confident in your skills, an IDE is a great tool.

As for why the loops are working outside a method, as you can see they are surrounded by curly braces. This is called a initializer block. These blocks of code are run when a new instance of the class is created, and they execute just before the constructor body is executed.

Thanks for the initializer block explanation. Head First Java talks about static initializer blocks but I didn't know you could do it non-statically. I guess they think it's better practice to put things in constructors.

We'll have to disagree on IDEs... as someone who started years ago with a very basic editor, then gave up and recently tried again using NetBeans, I can say that NetBeans has taught me far more than Notepad ever did, removed many of the frustrations and kept me interested, while allowing me to experiment and explore the language at probably 3 times the speed I could with just a text editor and command line... I wouldn't go back to Notepad any faster than I would force a Linux user to do all their Java programming in "ed"...

fred rosenberger wrote:
Justine Trudeau wrote:Is there a way that I can fill it and declare that it can be either black red or empty?
What are you filling your board with? If I understand correctly, you fill it with Pieces. Doesn't each Piece already have code to make it red, black or empty? So shouldn't that already be done?

+1... I think if your test code is coming back with nulls, it's more likely there's something wrong with your test!
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone has their preferences ;)

Anyway; yes, constructors are used in preference to initializer blocks. Initializers have their uses when there's a lot you want to do when you have different constructors, and you don't want to repeat the process in each constructor. This can easily be sidestepped by putting the code in a private method, and calling this method from each constructor. That's why initializers are used rarely, if ever.
 
Shawn Smith
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post what your Piece class looks like. What type exactly are Piece.WHITE, Piece.BLACK, etc.? I'm assuming some sort of public static final Piece instances, but it's hard to tell from the snippet you've posted.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!