• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making connect 4 game

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic