• 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

Two dimensional tile-manegmenet game

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all. I have to do my homework, and it's urgent, in about 2 days is deadline. I would like, here with you experienced java enthusiasts, to solve this step by step, but in a way that can be understandable to me. If you don't like this kind of approach where I bumped and asking for your help, without any credit, it's OK and I will remove my Q., but please take in consideration that I truly want to understand and solve this. For more info about what I have to do, please, click here.

























 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome!

What is your question? What can we help you with? We won't write the code for you (NotACodeMill <- click).
And thank you for using code tags in your first post
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so it all boils down to creating 5 classes: Board, GreedyPlayer, AdjacentGemPolicy, WholeRowColGemPolicy, and ChainedGemPolicy.

Let's start with one.
I made a few tiny changes: Board is now final and implements Cloneable. If you're going to give clone() an implementation, the class should probably be Cloneable. Also, clearFlags and doRemovals are now package private (they don't have the public access specifier). The reason is that these methods should only be used by the Game class, and not by the public at large.

Now, don't start adding fields blindly. You really don't need an int[][] array if you already have a Gem[][] array to keep track of the data for you. You also don't need an int rows or an int cols, because these can be found by the size of gems.

So! Let's start with the rows() and cols() methods. If you have a Gem[][], how would you implement these?
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:Welcome!

What is your question? What can we help you with? We won't write the code for you (NotACodeMill <- click).
And thank you for using code tags in your first post



Yes! That's exactly what I want, do NOT write code for me, but try to help me!

Stephan van Hulst wrote:Okay, so it all boils down to creating 5 classes: Board, GreedyPlayer, AdjacentGemPolicy, WholeRowColGemPolicy, and ChainedGemPolicy.

Let's start with one.
I made a few tiny changes: Board is now final and implements Cloneable. If you're going to give clone() an implementation, the class should probably be Cloneable. Also, clearFlags and doRemovals are now package private (they don't have the public access specifier). The reason is that these methods should only be used by the Game class, and not by the public at large.

Now, don't start adding fields blindly. You really don't need an int[][] array if you already have a Gem[][] array to keep track of the data for you. You also don't need an int rows or an int cols, because these can be found by the size of gems.

So! Let's start with the rows() and cols() methods. If you have a Gem[][], how would you implement these?



I would have to search for a) specific row and b) specific column. So I was thinking to call Gem[][] and try to find wanted row in that gem[][].
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is how I did for rows.

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear at all what you're trying to do in this method, but whatever it is, it's not going to work. You're iterating over the length of some random row in gems, and for each iteration returning a random value from a null array.

Maybe you've lost track of what the rows() method (without the rows parameter; don't change any of the method signatures) is supposed to do. It simply returns the number of rows in your grid.

If you have Gem[][] gems, and access a element from this array using gems[row][col], how would you determine how many rows there are in gems?
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using .length command.
Something like


Also do I need to have getters and setters for rows and cols in Gem class, right?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to change anything about the Gem class. It's provided.

Something like


Are you guessing? Note that gems is an array of arrays. gems[row] returns a column, so you're returning the length of a single column. At least, you would be, if the variable rows was defined.
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I really don't know how to get it. I tried on google but nothing pops out. Can you show me on some example?

What troubles me is following.
How can I know how many rows does Gem have? How to do that?
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is what I tried. Now I don't know how to get this array to int? After all wasn't this method supposed to be of int[] rather than int!? What should I return now?
Lets say I have following matrix:

3 4 5 9 2 9
5 3 5 6 4 7
0 8 4 8 3 2

Now I want to take row 2( getrow(1) ), it would be
5 3 5 6 4 7 ( ARRAY )
not
535647 (int, even though it has to be long )

so I am completely lost here.


And how to cast Gem[] to int[]? Is there any special method for such?
 
Michael Jefferson
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OMG! I just didn't read! After careful reading, this is what is it all about. ( I suppose, correct me if I am mistaken )


Now, how to do a hasValidGem method? How to check it if it was in the bounds?
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes! I have done this too.
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand now. How can I remove element from an two dimensional array?



I need it for doRemovals() method.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Kaptain Javathor", please check your purple mooseages for an important administrative matter. Thanks.
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello @Bear. I have done it. Please, check your admin panel.

Now back to my task!
Regarding my current task about doRemoval() method, there is following statement about this method which states :


" Any gem flagged (see method Gem.flagged()) for removal will be removed. Blocks that should "fall" will do so now, and colums that should shift left will also do so now. "



This is a characteristic of lists! So does it mean I now have to get it to lists?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job so far!

Now, the assignment didn't state anything about removing cells. If you look at the samples, you will see that the cells are still there, just that they've become "empty" somehow. So you don't have to change your Gem[][] structure, just the content.

Now, you can make empty Gems. Look at the methods provided by the Gem class.

Hint: doRemovals() is a tough method to implement. You may want to break it up in other methods. For instance, it might be worthwhile to first replace all the flagged cells with empty cells, then move all the "floating" gems for each column downward, and finally move all the empty columns to the right. These are all distinct pieces of logic, perfect for their own method.
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
!

OK, so it has to be something like this,
1. Iterate through each element of a 2 dimensional array.
2. If element is flagged true, make empty.
3. Now I am here confused. How to move "floating gems" for each column downward? Should I make new array with a size +1 of existing gems array and store elements in that new one?

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to solve this problem:

By the way: your makeEmpty() method call is doing nothing. Note that makeEmpty() is a static method that simply creates a new empty gem. It does nothing with an existing one (it's static).
 
Michael Jefferson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I have done sorting, but what troubles me is the following:
Why should I have second parameter int[] to, since I don't have anything from it? Method is void, right? Am I mistaken and on a good way?


 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It never said you should sort it.

When you implement a method, read the description and don't change the signature. When someone provides you the signature and contract (the comments above it), you should treat them as sacred, and you may only change the body of the method, so that it fits the contract.

If from looks like {-1, 1, 3, -2, 2, -1, -2, 1, 5} at the start, to should look like {1, 3, 2, 1, 5, 0, 0, 0, 0} at the end. Positive integers are all on the left, in the same order they were encountered in from.

Hint: Use a counter for from (let's say toMove) that keeps track of the current element to move, and use a counter for to (nextEmpty) that tracks the next empty position you can move to. You don't want to iterate through to for every iteration of toMove, so don't declare nextEmpty within the loop!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic