We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
==
& inequality!=
operators compare values only:https://processing.org/reference/equality.html & https://processing.org/reference/inequality.html
https://processing.org/reference/String_equals_.html
if ( "ja".equals(row.getString("gewonnen")) )
Thanks for the tip GoToLoop, that solved my issue. I
m not sure if I
m 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?
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-)
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:
Glad you've made it! Just for fun, I've tweaked some of your functions: :D
Also,
"data/statistics.csv"
can be replaced w/dataPath("statistics.csv")
:-bdThanks for the tweaks, it's learning the fast way with your comments! :)