There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
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?
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?Justine Trudeau wrote:Is there a way that I can fill it and declare that it can be either black red or empty?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
fred rosenberger wrote:
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?Justine Trudeau wrote:Is there a way that I can fill it and declare that it can be either black red or empty?
Don't get me started about those stupid light bulbs. |