• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connect 4 and two dimensional arrays

 
Walid Sakr
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a side note: this would be a perfect program to do in the Greenfoot environment.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49379
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Walid Sakr
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic