-4

We are building an application atm in which you can change the rules for a game and start the game from the mainmenuactivity

Looks like this:

MainMenu - RuleList -RuleEdit (Rule management) - PlayerActiivty - DrawCard (Game)

And we are also creating cards in the class GameLogic with a static array in it where the 13 cardtypes are saved with their rules.

So when we are changing the rule for a card we are calling GameLogic.setRuleNumber(int pos,int ruleNumber) //pos is the position of the card in the cardArray

Problem is: When we are creating the cards and start the game the cards are created and saved in GameLogic and if you enter the game now you can go through all the cards. But if you go back to choose the MainMenu (it changes right to it) the cardArray is suddenly empty and when you are entering the game again it is empty, but still accessible so not null.

GameLogic cards

public class GameLogic { 
     public final static List<ICard> cards = new ArrayList<>();

     public void createCards() {
         DatabaseConnector dbc = new DatabaseConnector(context);
         int rankLength = ICard.Rank.values().length;
         int suitLength = ICard.Suit.values().length;
         int ruleNumber;
         ArrayList<IRule> rules = dbc.getAllRules();
         int a = 0;
         for (int i = 0; i < rankLength; i++) {
           for (int j = 0; j < suitLength; j++) {
             ruleNumber = rules.get(i).getRuleNumber();
             cards.add((suitLength * i + j), new Card(ICard.Rank.values()[i], ICard.Suit.values()[j], ruleNumber));
        }
    }
}

` Creating cards happens in the Menu:

//Main Menu Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main_menu);
   BackgroundMusic.setBackgroundMusic(this);
   GameLogic gameLogic = new GameLogic(this); //Creating the GameLogic we want to work with
   gameLogic.createCards(); //Creating the cards
}

DrawCard Activity:

//DrawCard
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw_card);

    dbc = new DatabaseConnector(this);

    //init deck
    initDeck();

    //init playerlist
    playerList = CreatePlayer.playerList;
    playerNameText = (TextView) findViewById(R.id.activePlayerName);
    setPlayerName();

    //init rule
    ruleList = dbc.getAllRules();
}

public void initDeck() {
    GameLogic.shuffleDeck(GameLogic.getCards());
    cardStack = GameLogic.getCardStack();
}

What is happening here between the different activities? The problem is that in there are values saved in the Cards so we through an Intent we would have to give 5 different arrays. We don't think thats an appropriate solution.

Thanks for your help :)

Zujiry
  • 5
  • 3

1 Answers1

0

I highly recommend this library to store data in a persistent manner.

Example key-value pair write:

Paper.book().write("key", value);

Example read:

Object value = Paper.book().read("key");
Michele
  • 751
  • 1
  • 8
  • 16
  • we want to save an array with cards which themselves save a ruleNumber and their rank and suite. Does that work with this library? – Zujiry Oct 13 '16 at 12:48
  • I'm sorry, I don't get how to import this into my project. Can I take the module of paperdb? since just adding it as a dependency does not work, and all the other ways do not work either. Do you maybe have a helpful link or tipp? – Zujiry Oct 13 '16 at 14:08
  • What errors do you get? Did you add "compile 'io.paperdb:paperdb:1.5'" in your gradle file? – Michele Oct 13 '16 at 14:10
  • Error:(9, 0) Could not find method compile() for arguments [io.paperdb:paperdb:1.5] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. Open File – Zujiry Oct 13 '16 at 14:20
  • Thank you! This library works like a charm :) Still, do you know why the card ArrayList is seemingly discarded when we are starting the game and then going back? I would like to know how it comes to that. – Zujiry Oct 13 '16 at 14:42
  • You should override the "onStop()" method on your activity. At that point, store all of your data using Paper. :) – Michele Oct 13 '16 at 14:51
  • Sorry to ask again... but is there a way to save the Paper database for a restart of the application? – Zujiry Oct 14 '16 at 14:35
  • No problem ;) I'm not sure I've understood your question. Are you asking if Paperdb store data persistently even if you restart the app? In this case, it does it automatically when you do Paper.write() – Michele Oct 14 '16 at 14:37
  • Yes, that was the question. Seems I have overlooked something. Thank you again, you are a great help :) – Zujiry Oct 14 '16 at 14:44
  • Then I will ask another one: I didnt meant the implementation of Paper with my other question. I just wanted to know: I have a cardArray in my GameLogic I intitialize it in the first Activity, main menu. I start the game (another activity, stopping the main menu) The cardArray is still full so the game is correctly initialized But when I go back to the mainmenu and start the game again, the cardArray is empty. Shouldnt it have been still saved in the GameLogic, since its just static in there and nothing can influence it. Do you have any idea for that? – Zujiry Oct 14 '16 at 14:51
  • Well, that's a very domain specific question. I should see the code to answer correctly. However, I recommend you to carefully read [this](https://developer.android.com/training/basics/activity-lifecycle/index.html) article that shows you how to correctly handle Activities and their lifecycle. – Michele Oct 14 '16 at 15:00