How to save and load tables while overwriting data

edited December 2014 in Questions about Code

Hi Folks,

Question: Why does my code not grab the correct values into the variables wonNotSwitched and wonSwitched

For a project for school I`m working on a little game. The game has statistics about how often someone wins or loses. I would like to keep those statistics in a CSV file for easy loading and saving (at least I thought it would be easy). After some hours trial and error and then some more googling I couldn't find an answer.

I`m loading the CSV file, then play a game. After that I want to append the data. So far so good. The problem is with this part of the code:

    void calculateCurrentStatistics() {
      for (int i = 0; i < statistics.getRowCount (); i++) {
        TableRow row = statistics.getRow(i);
        if ((row.getString("gewonnen") == "ja")) {
          if ((row.getString("gewisseld") == "nee")) {
            wonNotSwitched++;
          } else {
            wonSwitched++;
          }
        }
      }
      println("ws: " + wonSwitched + " ||  ns: " + wonNotSwitched);
    }

I somehow am unable to read the data in position 1,1 and 1,2 (2,1 and 2,2 and so on). I hope my code is somehow readable.

The data in my csv:

 spel nummer,gewisseld,gewonnen
    1,nee,ja
    1,nee,nee
    1,nee,ja
    1,nee,nee
    1,nee,ja

The full code (before this function is called the table has already been loaded):

void processStatistics() {
  saveStatistics();
  loadStatistics();
  calculateCurrentStatistics();
  drawStatistics();
}

void saveStatistics() {
  TableRow newRow = statistics.addRow();
  int gameNumber = statistics.getInt(statistics.getRowCount()-1, 1) + 1;
  newRow.setInt("spel nummer", gameNumber);
  newRow.setString("gewisseld", playerSwitched);
  newRow.setString("gewonnen", playerWon);
  saveTable(statistics, "data/statistics.csv");
}

void loadStatistics() {
  statistics = loadTable("data/statistics.csv", "header");
  for (int i = 1; i < statistics.getRowCount (); i++) {
    TableRow row = statistics.getRow(i);
    if ((row.getString("gewonnen") == "ja")) {
      if ((row.getString("gewisseld") == "nee")) {
        wonNotSwitched = wonNotSwitched + 1;
      } else {
        wonSwitched= wonSwitched + 1;
      }
    }
  }
  println("ws: " + wonSwitched + " ||  ns: " + wonNotSwitched);
}

void calculateCurrentStatistics() {
  for (int i = 0; i < statistics.getRowCount (); i++) {
    TableRow row = statistics.getRow(i);
    if ((row.getString("gewonnen") == "ja")) {
      if ((row.getString("gewisseld") == "nee")) {
        wonNotSwitched++;
      } else {
        wonSwitched++;
      }
    }
  }
  println("ws: " + wonSwitched + " ||  ns: " + wonNotSwitched);
}


void drawStatistics() {
  color wonNotSwitchedColor = 255;
  color wonSwitchedColor = 255;
  if (wonSwitched < wonNotSwitched) {
    wonNotSwitchedColor = #FF0000;
  } else if (wonSwitched > wonNotSwitched) {
    wonSwitchedColor = #FF0000;
  } 
  fill(wonNotSwitchedColor);
  text("Gewonnen zonder wisselen: " + wonNotSwitched + " keer", width/4, height - 40);
  fill(wonSwitchedColor);
  text("Gewonnen met wisselen: " + wonSwitched + " keer", width/4*3, height - 40);
}

Answers

  • edited December 2014 Answer ✓
    if ((row.getString("gewonnen") == "ja")) {
      if ((row.getString("gewisseld") == "nee")) {
    

    if ( "ja".equals(row.getString("gewonnen")) )

  • Thanks for the tip GoToLoop, that solved my issue. Im not sure if Im allowed to keep on asking stuff in the same thread: but now every time my loadStatistics function gets called it adds to my existing numbers--, doubling the variable each time.

    I've tried statistics.clearRows() and statistics = null; on multiple points, but no joy. Any ideas on how I can reset the table before I reload it?

  • Answer ✓

    I'm not sure if I'm allowed to keep on asking stuff in the same thread...

    That's your call. If it's too diff. subject you may decide to start another forum thread.
    In either case, you should re-post your updated code w/ applied fixes! L-)

    ... every time my loadStatistics() function gets called it adds to my existing numbers--, ...

    What are those "numbers--"? Are they wonNotSwitched & wonSwitched by chance?
    Generally, loadStatistics() would be called once when the sketch starts.
    Much probably both wonNotSwitched & wonSwitched are both 0.
    But you can forcibly reset them w/ wonNotSwitched = wonSwitched = 0 as the 1st statement within loadStatistics(). *-:)

  • Gah, it's so simple! :D

    I was trying to clear the table, when all along the variables themselves should have been cleared. Thanks once more. Updated code:

    void processStatistics() {
      saveStatistics();
      updateStatistics();
      loadStatistics();
      drawStatistics();
    }
    
    void updateStatistics() {
      if ("ja".equals(playerSwitched) && "ja".equals(playerWon)) {
        wonSwitched++;
      } else if ("ja".equals(playerSwitched) && "ja".equals(playerWon)) {
        wonNotSwitched++;
      }
    }
    
    void saveStatistics() {
      TableRow newRow = statistics.addRow();
      int gameNumber = statistics.getInt(statistics.getRowCount()-1, 1) + 1;
      newRow.setInt("spel nummer", gameNumber);
      newRow.setString("gewisseld", playerSwitched);
      newRow.setString("gewonnen", playerWon);
      saveTable(statistics, "data/statistics.csv");
    }
    
    void loadStatistics() {
      wonNotSwitched = wonSwitched = 0;
      statistics = loadTable("data/statistics.csv", "header");
      for (int i = 0; i < statistics.getRowCount (); i++) {
        TableRow row = statistics.getRow(i);
        if (("ja".equals(row.getString("gewonnen"))) && ("ja".equals(row.getString("gewisseld")))) {
          wonSwitched++;
        } else if (("ja".equals(row.getString("gewonnen"))) && ("nee".equals(row.getString("gewisseld")))) {
          wonNotSwitched++;
        }
      }
    }
    
    void drawStatistics() {
      color wonNotSwitchedColor = 255;
      color wonSwitchedColor = 255;
      if (wonSwitched < wonNotSwitched) {
        wonNotSwitchedColor = #FF0000;
      } else if (wonSwitched > wonNotSwitched) {
        wonSwitchedColor = #FF0000;
      } 
      fill(wonNotSwitchedColor);
      text("Gewonnen zonder wisselen: " + wonNotSwitched + " keer", width/4, height - 40);
      fill(wonSwitchedColor);
      text("Gewonnen met wisselen: " + wonSwitched + " keer", width/4*3, height - 40);
    }
    
  • edited December 2014 Answer ✓

    Glad you've made it! Just for fun, I've tweaked some of your functions: :D

    Table statistics;
    int wonNotSwitched, wonSwitched;
    
    void loadStatistics() {
      statistics = loadTable("statistics.csv", "header");
      wonNotSwitched = wonSwitched = 0;
    
      for (TableRow row : statistics.rows()) {
        String gewonnen = row.getString("gewonnen"), gewisseld = row.getString("gewisseld");
    
        if      ("ja".equals(gewonnen)  && "ja".equals(gewisseld))   ++wonSwitched;
        else if ("nee".equals(gewonnen) && "nee".equals(gewisseld))  ++wonNotSwitched;
      }
    }
    
    void drawStatistics() {
      fill(wonSwitched < wonNotSwitched? #FF0000 : -1);
      text("Gewonnen zonder wisselen: " + wonNotSwitched + " keer", width>>2, height-40);
    
      fill(wonSwitched > wonNotSwitched? #FF0000 : -1);
      text("Gewonnen met wisselen: " + wonSwitched + " keer", 3*width>>2, height-40);
    }
    

    Also, "data/statistics.csv" can be replaced w/ dataPath("statistics.csv") :-bd

  • Thanks for the tweaks, it's learning the fast way with your comments! :)

Sign In or Register to comment.