• Post Reply Bookmark Topic Watch Topic
  • New Topic

implementation question  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As my first Java project, I've been trying to implement a game for a few months now, and I need some feedback on one aspect of my implementation because it's causing me some problems.

The game is played on a checkerboard. At the end of the game, players score points for "regions" of adjacent markers in their color.

I'm representing the "contents" of each square ("red marker", "white marker", "empty") as a string in a 2D array.

For scoring purposes, I'd planned:

1) start at [0][0]
2) check for adjacent squares with the same contents
3) if any match, add them to a list of "squares that are part of this scoring region but have not yet been checked to resolve their own adjacencies"
4) check the squares from (3) for their adjacencies, add any new squares to the list, check THOSE squares for their adjacencies, etc. until there are now more squares left to check on the (3) list. (i.e. this region is completely checked)

My problem is: how to I tell the list in step (3) to add square [4][5] to it?

Would a better implementation have been to have each "square" be an object, with attributes for "contents", "checked", and "needs to be checked" that could be referenced by the scoring code?

Sorry for the long explanation.

Thanks for any help.

Mike
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is this "list" in step 3?

(Or maybe this is what you're asking? )
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is this "list" in step 3?


Well, originally I thought it would be a 2D arraylist, so I could add 2D references into it as-needed.

Now that I'm trying to implement it, I realize that it can't work the way I originally thought (if I add "[4][5]" to the list, what happens to "[4][0]", "[4][1]", etc.? Do they get added also?)

So now I'm wondering "what can the list in step (3) possibly be? (given the code that I've already implemented). Is there any way to make it work like I'd originally expected, or do I need to change my implementation? (like going with the "each board square is an object in an array" idea I floated)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it sounds like your list basically needs to store pairs of ints. You could do this using a 2-dimensional array, but managing this could get tricky. For one thing, the size of the array would need to be set when it's created. Then you would need to handle "bookkeeping" to keep track of what index you're adding items to, which items you've handled, etc.

One way of simplifying this might be to define objects that represent the squares (like a class that just has a row int and a column int), and then adding/removing these from an actual List (like an ArrayList).
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the whole manual "bookkeeping" aspect is looking more and more ornery.


One way of simplifying this might be to define objects that represent the squares (like a class that just has a row int and a column int), and then adding/removing these from an actual List (like an ArrayList).


I like that idea - it dovetails nicely with the alternative I'd been thinking about. I guess when I'm initializing these "square" objects, I could just populate the row and column ints by using nested for loops, couldn't I?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by MR Chen:
...I guess when I'm initializing these "square" objects, I could just populate the row and column ints by using nested for loops, couldn't I?

Instead of systematically creating a representation for each square (which wouldn't be very helpful as a list), I was thinking of just creating them as needed.

For example, if your list is "squaresToCheck," and you find that the square at [4][5] needs to be added to the list, then create a new instance to identify that square and add it to the list...

squaresToCheck.add(new SquareID(4, 5));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!