Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

arrays - What is the best way to have a card deck, which consists of multiple combinations of cards, but never exceeds a certain number?

As described in my question I am currently working on a card game. I have a class in which you can choose between 20 Cards and another class where you can see which cards you chose (the "deck"). I want that deck to not exceed 10 cards. However, there are a few problems I am struggling with:

  1. How do I send the Information which cards are chosen between those Activitys?

  2. How does the Array -or whatever is best to use here- know when no more cards can be added?

  3. If I un-choose a Card, how does the Array know that it has to delete that card from the deck?

  4. If a card already is in the deck, what do I have to do that it can't be chosen again?

Probably not that hard to do, but I am not a professional... I hope y'all can help me with this :)

(I do not really have any useful code in those two classes, except the cards names, but you can just refer to them as card1; card2; etc.)

question from:https://stackoverflow.com/questions/65830031/what-is-the-best-way-to-have-a-card-deck-which-consists-of-multiple-combination

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. You could store the data in an object which is accessible from both Activities
  2. Before adding, check the size of the list. If it's already at the maximum size, don't add more.
  3. You can use a MutableList here (such as ArrayList) which has a remove function
  4. Before adding, check if any card in the list is equal to the card you wish to add. If so, don't add it again.

It really all does not have to be all that complicated, but a lot depends on how you represent for example a Card and a Deck in code. So sharing what you have is still a good idea.
Using an ArrayList instead of an Array should already solve some of your issues, as a List has a dynamic size compared to the fixed-size Array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...