• Post Reply Bookmark Topic Watch Topic
  • New Topic

More design questions - referencing a variable or object from an argument?  RSS feed

 
Roland Wynn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, folks. I asked a couple of questions a while ago on some design and implementation questions for a project (game) I am working on. I got some great input and have worked out some new designs but I ran into some other questions I was wondering if any of you could ask.

Firstly this project is a small game I am developing that deals with special cards that do various actions. But, they really only change values and flags. Anyway, I have a Card interface and about 40 different specific card classes that implement it. I did this because I can use the Card type reference when dealing with things like passing arguments and creating a Stack full of those specific types. Basically, so I can deal with those objects without knowing exactly which card I am dealing with. Correct me if I am wrong in that area, please.

Now, my main question is how to create the deck by creating these objects (about 250 because there are duplicates of some cards) and pushing them onto a LIFO stack. There's only one constructor argument for each card, and that's a unique identifier int. So far, I've created a method that accepts an argument of type Card and and int value. The method then loops through the specified number of times and creates a new object of the Card argument with the constructor argument supplied by a universal counter. Like...

Since I have to make the object and then push it onto the Stack, I thought it might be easier to first put it inside an array, and then loop through that array with a mainDeck.push(cardCounter[i]) loop or something. I don't know if that's a practical way of doing it or not, so let me know if you have an alternative.

The problem that I have and shares the name of this topic title is passing that argument. NetBeans is giving me an error that it can't find the symbol variable. I'm pretty sure it's not a class or package issue, just a programming practice issue. Can I pass a reference to another variable name like that? Right now, a lot of the cards have similar quantities, so I can simply make the 7 or 8 loops needed to throw all those same-amount cards into their own loop and be done with that. I guess my question is, if there's another way to do it without getting too complicated.

How would pro's do it? If they had hundreds or, perhaps, thousands of objects needed to be created? Well, they'd probably use C++ or C# or something, but still...?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roland Wynn wrote:Well, they'd probably use C++ or C# or something


Oh no they wouldn't. You can create 1000000 objects in Java just as quickly as in C, C++ or C#.

You appear to have three problems, which are independent, but you are confusing the two. You have a compiler error somewhere; you haven't provided enough details, but "cannot find symbol" may represent a simple spelling mistake in the name "variable."

The other two are design things: why have you got the universalPosition field outside the Card class.It appears this is associated with Cards, so why doesn't it belong inside the Card class? You can do it like this Account classGet rid of the number argument to the constructor, and let the Card class set up the universalPosition inside. Also get rid of any setUniversalPosition methods.

Why are you passing a Card argument to that method and not using it? Never a good idea to have unused arguments/parameters like that.

Push the cards directly onto the Stack. Don't use an array unless you need an array.

 
Roland Wynn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Roland Wynn wrote:Well, they'd probably use C++ or C# or something


Oh no they wouldn't. You can create 1000000 objects in Java just as quickly as in C, C++ or C#.

Haha, well, I didn't mean it like that. I've just never seen a java program so large that it would create that many objects and it would seem like a large undertaking. But, then again, I've only played mini-games with java, so... :p

You appear to have three problems, which are independent, but you are confusing the two. You have a compiler error somewhere; you haven't provided enough details, but "cannot find symbol" may represent a simple spelling mistake in the name "variable."

I've double-checked the capitalization and spelling but it still has the error. The question I was having was whether or not passing an argument of another variable was possible. In this game, there's about 5 or 6 flags that can be set. Some cards flip them to true, and some back to false but only on a specific flag. Then, there's come cards that can change ANY of those flag states, which the user chooses. So, I have written a method that accepts two arguments, the player and the target, and it checks that argument with if-else if statements and carries out that appropriate action.

This isn't that big of a deal now since there's only a few thing it needs to check, but I'm wondering what to do if you can have 10, or 20, or 100 possibilities and you don't know which one it will be at run-time. But, this has caused me to alter my interface a bit to handle that extra method, which most only have that one. Also, the other classes have that empty method. I'm just wondering if there's a better, more practical way to do it. Look at this example-


Please note, this is something I whipped together just now. It was only for testing and has no practical use, save an attempt to explain my problem.

So, you see, if that class had multiple variables and you wanted the ability to change any of them, how would you do it? It seems I can't just tell the method, 'hey, change this variable. I don't know the exact name, but you'll know when it runs because I'll tell you'.

The other two are design things: why have you got the universalPosition field outside the Card class.It appears this is associated with Cards, so why doesn't it belong inside the Card class? You can do it like this Account classGet rid of the number argument to the constructor, and let the Card class set up the universalPosition inside. Also get rid of any setUniversalPosition methods.

Yes, that's a good idea, I haven't thought of that. But..

Why are you passing a Card argument to that method and not using it? Never a good idea to have unused arguments/parameters like that.

Push the cards directly onto the Stack. Don't use an array unless you need an array.

It was using it. When the Card object was being created inside that array, it was taking the argument of type Card, storing it as createCard and then instantiating at that array element. This was the main question I had, if I could reference objects like that (see above example).

The reason I used an array, is because I need to make 200+ of these objects, all from the 40 or so classes that i have defined. They all need to have unique names, right? So, I use an array which allows me to 'number' each object. Then, I can simply loop once more and copy the contents of the array of cards into a Stack but using a loop.

Unless I can do something like, Card newCard + i = new Card. Haha
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bet you have seen a Java application that big . . . What about the Google search engine, probably the largest Java application anywhere? In fact it is quite easy to create 1000000 objects; you simply tell a method to create them and . . .

You can pass any variable to your methods; the method tells the compiler what sort of parameter it requires, then the compiler checks whether all objects using that method pass the correct sort of argument. You didn't show us where you are using the "variable" name, so I can't help any more about that.

Your returnVar method doesn't do anything, except send back what has just been sent to it. At least as you have written it.
The createCards method you wrote does not use the Card parameter. It uses a class which just happens to have the same name createCard, and you nowhere use the Card parameter at all. Maybe you had a different method in "real life" but I can only see what you wrote here.
By the way, you would need to set the size of the cardCounter array, otherwise you might get problems with out of bounds exceptions.

Maybe you should set the array member and "push" the object onto the stack in the loop where you create them. Probably quicker that way.

If you want examples of methods which access different values, look at something like the get and set and roll methods of Calendar. I suspect they use a switch block to decide which field to change. So you can pass numbers representing which fields to change, and values to change them to/by.

That's all I can suggest for the time being.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!