GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes connect 4 and two dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "connect 4 and two dimensional arrays " Watch "connect 4 and two dimensional arrays " New topic
Author

connect 4 and two dimensional arrays

Walid Sakr
Greenhorn

Joined: Apr 04, 2013
Posts: 2
Hi guys i am new here and actually i am new to Java and to programming but i am trying to teach myself and learn ... all your help will be highly appreciated. ok my problem is i am trying to write the code for a simple connect 4 game (see the code below i have written so far) simply i created a 2 dim array of characters "board" i filled it with the letter 'E' for empty then i ask the player to enter a column number for the new disk and pass the data to the "addDisk" method but unfortunately when the array is displayed no change is happening to the array and it shows all cells with the letter 'E' what is the mistake i am doing i dont know i went through it 100 times but i can't spot it ... please help
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

Well, for starters, you don't have a 2-d array. Java doesn't have any such construct. Your board is really a 1-d array.

However, what it happens to hold are arrays.

When your code isn't doing what you want, then the best thing to do is stick in a bunch of print statements. Validate that what you THINK is happening really is. print out things at each stage. print out the board before you pass it in. print out the board inside the method - both at the beginning and the end. Print out the board after you have returned from the method.

You have somewhere assumed your code is doing something it's not. So, the best way to find that is to validate what it is doing at each and every step. System.out.println is your new best friend.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Walid Sakr wrote:please help

Follow Fred's advice and you'll soon work out what's wrong.

My advice is more general: Your solution is very procedural.

How do I know that?
1. You only have one class.
2. All your methods are static.
3. Your main() method is very large.
Now there's nothing particularly terrible about that, but Java is an object-oriented language, so it generally works best with objects. What you've written could have been done in C or Basic.

For example, if you had a Board class, you could hide all that fiddly logic inside it, viz:and your main() might then look something like:Furthermore, you could then put your Board object inside a ConnectFour class (possibly a modification of the one you've already got) whose business it is to run the game. Then your main() might look something like:and there you have it: A complete game driver in 6 lines.

Please note that the above is just for illustration; there are MANY ways to do this, but the best ones are likely to involve making objects do the work for you.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

Just a side note: this would be a perfect program to do in the Greenfoot environment.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Junilu Lacar wrote:Just a side note: this would be a perfect program to do in the Greenfoot environment.

Hmmm. Veeery interrestink. Bookmarked.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
And welcome to the Ranch
Walid Sakr
Greenhorn

Joined: Apr 04, 2013
Posts: 2
Winston Gutkowski wrote:
Walid Sakr wrote:please help

Follow Fred's advice and you'll soon work out what's wrong.

My advice is more general: Your solution is very procedural.

How do I know that?
1. You only have one class.
2. All your methods are static.
3. Your main() method is very large.
Now there's nothing particularly terrible about that, but Java is an object-oriented language, so it generally works best with objects. What you've written could have been done in C or Basic.

For example, if you had a Board class, you could hide all that fiddly logic inside it, viz:and your main() might then look something like:Furthermore, you could then put your Board object inside a ConnectFour class (possibly a modification of the one you've already got) whose business it is to run the game. Then your main() might look something like:and there you have it: A complete game driver in 6 lines.

Please note that the above is just for illustration; there are MANY ways to do this, but the best ones are likely to involve making objects do the work for you.

HIH

Winston


i really do appreciate the advise and that is exactly what i was going to do but things doesn't look that clear to me from the first step it takes sometime to arrange it into classes and methods and so on i do that at a later stage, maybe it is strange to you or doesn't sound to professional but this is how my mind figures it out first i concentrate on the engine "how this problem will be solved" then i re arrange it into classes and objects.
getting back to my problem i have tested this part of the program using all possible ways "i know of " of course including what Fred mentioned in his post above but the addDisk method is not returning any result actually it has no impact on the board array i was seeking technical advise (a wrong assumption, passing the array to the method in an unusual way .....) anything that your expert eyes can see that i can't see

finally thanks for the replies and the assistance you are providing
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Walid Sakr wrote:...but this is how my mind figures it out first i concentrate on the engine "how this problem will be solved" then i re arrange it into classes and objects.

I quite understand. I'm an old C hack myself, and it took me a long time to "re-educate" myself.

I suspect that the main problem you're encountering is that you're focused far too much on HOW you're going to solve the problem, rather that WHAT you need to do.
It's a subtle distinction, but code is all about "how"; and with a language like Java, you want to be a designer, not a coder.

Have a look at the WhatNotHow page, because it may explain it better.

And I really suggest that you get into the habit of designing first because, as the problems you tackle get more and more complex, the less likely you are to be able to simply "code a solution".

getting back to my problem i have tested this part of the program using all possible ways "i know of " of course including what Fred mentioned in his post above but the addDisk method is not returning any result actually it has no impact on the board array i was seeking technical advise (a wrong assumption, passing the array to the method in an unusual way .....) anything that your expert eyes can see that i can't see

Well, the main problem I see is in line 33:
for (int i=5 ; i==0 ; i--) {
but in order to work out why it's wrong, you need to StopCoding (←click) and write out on paper what it does.

Winston
 
GeeCON Prague 2014
 
subject: connect 4 and two dimensional arrays