• 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

more than 50 fields and counting ?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, this is a school project I'm working on and I have to make a game program. the game can be played on 3 different maps which translates to easy, medium and hard. the first part of the project is to make a game
which can be played from the command line by the players alternating turns and the second part must have a GUI..so I'm on the first part command line part.. now I implemented the easiest map to implement but
I ended up with about 50 fields in a class..implementing the other two maps may give me more than 100 fields in a single class..is this possible and normal? well i guess is possible but is it normal?
now I'm going to post the code of the map..and i will upload a picture of the map that must I implemented..

[img]

so as can be seen in the image of the map, it is made up of hex grids..every grid is called a sector..so I made a sector class and then a map Fermi class, fermi being the name of this particular map





the next map I have to implement will have about 100 sectors, thinking that I have create a field for all 100 sectors...mmm....is there a better way or I just have to sit down and get to work ?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the way you did it is not normal. In fact, it's quite horrific. You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Finaly I got the map...it is in this link

[moderation comment] fixed image link as it contained double 'http'
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you have a lot of duplicated code. That's even more horrific. Extract the essence of all those methods and put them in a single method. Then use parameterized values to account for anything that can vary.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No, the way you did it is not normal. In fact, it's quite horrific. You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.



ok..
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No, the way you did it is not normal. In fact, it's quite horrific. You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.



where are the duplicated codes? I can't see them..
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:

Junilu Lacar wrote:No, the way you did it is not normal. In fact, it's quite horrific. You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.



where are the duplicated codes? I can't see them..


can you suggest a practical way to deal with the problem? I mean with a short example code? I still don't get how to reduce all those fields since they are effectily a part of map therefore the class..
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:where are the duplicated codes? I can't see them..



 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

mike Vigor wrote:where are the duplicated codes? I can't see them..





they seem the same but they are not the same lol..there is a very small change there..but well these are not the problems why I'm here..now I thinking of a way of generating those sectors for the class fermi so that I don't have to type all those fields
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This...can be replaced with...
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This...

Looks to be the same as...
Perhaps that's not what you intended.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for a reason or so it seems this site does not permit editing the original message..so I have to write in the comment..I just tried to correct implement the map in a new way and I think is far better now..what do you guys think..now I post is here
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:but well these are not the problems why I'm here..now I thinking of a way of generating those sectors for the class fermi so that I don't have to type all those fields  



Junilu Lacar wrote: You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.



I would use a Map, something like:

...or maybe even
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

mike Vigor wrote:but well these are not the problems why I'm here..now I thinking of a way of generating those sectors for the class fermi so that I don't have to type all those fields  



Junilu Lacar wrote: You need some kind of data structure to hold all those different but essentially similar things. Like a collection of Sector of objects, each one distinguishable from all the others via a unique identifier.



I would use a Map, something like:

...or maybe even



this solution is not ideal..with a map of 100 sectors I will have too many things to type..lol..infact I'm now using some couples of loops and mutual esclusion to set the type of the sector...
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:this solution is not ideal..with a map of 100 sectors I will have too many things to type..lol..infact I'm now using some couples of loops and mutual esclusion to set the type of the sector...


Ultimately I think you'll need to load a game layout from a text file rather than hard code it. This will still mean that you'd have to fill in the text file though.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your use of letter+number for the location is something that I feel should be objectized into a class, e.g. Location. Perhaps something like this. The game has a sort-of grid layout for letter+number but the movement on the diagonals is dependent on whether or not the column letter is odd or even.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike Vigor wrote:. . . ..what do you guys think..now I post is here  . . .

Not much better. Your String array doesn't represent areas.
You do appear to have a Sector class, but that doesn't relate to other Sectors. Strings don't count. Each Sector shou‍ld have fields for adjacent Sectors. It is quite possible that some of those fields will legitimately point to null because there are no other Sectors in that direction, so you will have to allow for those nulls when making your moves.
I suggest you find out about enumerated types, which you can use for the status of your different areas. You might be able to use a Direction enumerated type, too.

Please use the code button rather than writing tags by hand. Somebody has corrected the tags for you.
And welcome to the Ranch again.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You do appear to have a Sector class, but that doesn't relate to other Sectors. Strings don't count. Each Sector shou‍ld have fields for adjacent Sectors.

That might not be necessary. If you look closely the Sectors are laid out in a grid with up/down jogs depending on whether the letter is odd or even.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a link to a write up for the rules of the game?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Campbell Ritchie wrote:You do appear to have a Sector class, but that doesn't relate to other Sectors. Strings don't count. Each Sector shou‍ld have fields for adjacent Sectors.

That might not be necessary. If you look closely the Sectors are laid out in a grid with up/down jogs depending on whether the letter is odd or even.


In fact, you could take this even further by assigning Sectors in a 2D array (aka array of arrays).
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP: A big part of the problem is that you seem to have a very procedural programming mindset. The fact that you can't recognize duplication and opportunities to turn your thinking around and code in terms of objects instead shows this.

In the moveSouth and moveNorth methods, practically everything is the same except for the sign of your increment. I suppose the same situation is there for a moveWest and moveWest, right? This is very procedural style code. A more object-oriented API would turn that around into something like:

Until you switch to a proper object-oriented mindset, however, you will struggle to see how the above can be achieved.

[EDIT: I just noticed the image of the game board that OP posted. I also see I've already been beaten to the punch with the Direction enum ; didn't notice that reply when I first posted this response from my tablet when I woke up this morning   ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This could actually be a very fun project to write. It's unfortunate that OP seems to have been shortchanged (once again, as we have observed a number of times with other posters) by instruction that falls short of giving students a way to frame problems within an object-oriented mindset. Even the suggestion to use a 2D array is a vestige of a non-object-oriented mindset.

A useful concept in this case is reification. This is one thing you can learn from Corey Haines' book, "Understanding the Four Rules of Simple Design" where he applies the idea to Conway's Game of Life to turn parameters like (int x, int y) into a real object such that you'd have (Location loc) instead. Doing this for this problem could lead to a graph representation of the board where a Sector is a node on the graph and it can have up to six neighbors, one in each Direction (N, S, NE, SE, NW, SW).

You could then define a way to describe a game board configuration based on this.  Just off the top of my head, a JSON-based game board configuration definition might look something like this:

and a sectors element would look something like this:

Code that unmarshals this JSON configuration would have logic that makes it possible to avoid duplication in the configuration JSON and keep it manageable by considering the fact that if  Sector S1 has a neighbor S2 in the SOUTHWEST direction, then it follows that S2 has a neighbor S1 in the NORTHEAST direction.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wrote:... A more object-oriented API would turn that around into something like:


This is what Player might look like:

The Sector.NONE constant is a Null Object
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,
your approach, while object oriented, ignores the fact that there is a specific organization to the letter+number scheme and requires the JSON file to explicitly capture this organization. Given what we know (so far) from the OP, all that should be needed in the JSON (or flat) file is the letter+number and the "mode"(?), e.g. "dangerous". The relationships can be inferred by the letter+number. The organization can be held in a Map<Location,Sector>, or in a 2D array. Either way it would reduce the size of your JSON file by (perhaps) three quarters, and thereby reduce the risk of errors.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

I wrote:... A more object-oriented API would turn that around into something like:


See line 28 of the sample code I provided. It would allow Sector to implement this without the explicit organization being necessary.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:See line 28 of the sample code I provided. It would allow Sector to implement this without the explicit organization being necessary.


Looking more closely at the graphic, I see what you mean. However, the semantics of Location.move() does not seem natural. I can't imagine telling a Location object to move. I can imagine telling a Player object to move in some direction though and I could find it reasonable to expect an indication of what the new Location of that player is after it moved as a result.

Implementation details aside, the point I wanted to make really was that OP has limited himself to very procedural style solutions because it doesn't appear that he has been given instruction on how to think in terms of objects, attributes, and behaviors. If you look at his code, it's him, the programmer, manipulating information in objects with his code rather than defining objects and their behaviors and then orchestrating the actions of various objects from the main program logic. He's just using Java objects as data holders. Most of the code he has written are really disjointed procedures masquerading as object methods.

This is not meant to be a criticism of OP since he doesn't know any better; rather, as I said before, it's just more commentary about the same kind of thing we see in many posts here where students are taught to write programs in a very procedural manner from the start, ostensibly to make it easier to "get" what programming is about, but then when faced with problems where OO thinking is needed, they struggle mightily and produce some pretty horrific code and designs.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:See line 28 of the sample code I provided. It would allow Sector to implement this without the explicit organization being necessary.


Going back to implementation details, you might want to refactor that code. It still seems like there's a lot of duplication. I would start by experimenting with a couple of methods like this:

In the Direction enum, you can define the offsets involved in calculating the change in Location:

The Location class can then make necessary adjustments when applying those offsets based on Odd/Even row/column.  Just a thought. I'm trying these ideas out in code right now.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I would use a Map, something like:


It seems like a Map<Location, Sector> would be better. As Carey and I have shown, a Location seems to need enough behavior to justify reifying it rather than just using a String to represent it. I think that would make it easier to deal with Location objects in a Map and avoid having to use someLocation.toString() when just someLocation would do.
 
My cellmate was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic