Hello,
I am writing a basic poker tracker program. The program takes in a poker hand history file (a .txt file type) which contains multiple hands within it, isolates each hand, saves it into the data folder as a unique .txt file, and then creates an object that uses that single hand history .txt file to run different analysis on it.
The first time I run the program on the .txt file everything works as expected. The second time I am running into some problems.
Table variables;
String [] rawhandhistoryfile; //This is where the raw hand history .txt is loaded into
//This stores the names of existing hands in the database to check for duplicates
String [] namesofhands;
//This scrapes the current hand name from the .txt file being uploaded
String currenthandname;
boolean copyhand; //When this is true, the current line of the hand is copied into a String []
boolean duplicate; //Is true if curenthandname exist in the namesofhands String []
Hand [] overalldatabase; //This array of Hand objects stores every hand added into the database
//This Hand object is where the currently loading hand .txt file is placed so it can be
//loaded into the overalldatabase Hand []
Hand newhand;
int numberofhands; //Store the number of hands already in the database at start of program
void setup() {
size(400, 400);
//This loads the stored .csv file into the program, Variables.csv must exist in the "data"
//directory in the processing 3 sketch before running the program for the code to work
variables = loadTable("Variables.csv", "header");
//This loads the overall hand history file from Bovada into the program as a String []
rawhandhistoryfile = loadStrings("Test hands.txt");
//This loads in the existing list of saved hands into the program
namesofhands = loadStrings("Names of hands.txt");
//This loads the existing number of hands in the database into the program
numberofhands = variables.getInt(0, "Value of variable");
//This is the existing overalldatabase at the start of the program
overalldatabase = new Hand[numberofhands];
//This initializes the currenthand String [] to be empty at the start of the program
String [] currenthand = new String[0];
//This resethand Sring [] will stay empty so the currenthand String [] can be reset
String [] resethand = new String[0];
//Make sure the hand history file has a valid input
if (rawhandhistoryfile != null) {
//Search all lines of the rawhandhistoryfile String []
for (int i = 0; i < rawhandhistoryfile.length; i++) {
//If the copyhand boolean is true
if (copyhand) {
//Add the current line of the rawhandhistory String [] into currenthand
currenthand = append(currenthand, rawhandhistoryfile[i]);
}
//If the current line of rawhandhistory has the words "Bovada Hand #" in it
if (rawhandhistoryfile[i].indexOf("Bovada Hand #") >= 0) {
//Set the currenthandname to be "Bovada Hand #SAMPLE_NUMBER"
currenthandname = trim(rawhandhistoryfile[i].substring(0, rawhandhistoryfile[i].indexOf("Zone")));
//Turn the String [] namesofhands into one long String
String joinedhandnames = join(namesofhands, " : ");
//If the current hand name exists in the joinedhandnames String
if (joinedhandnames.indexOf(currenthandname) >= 0) {
//Turn copyhand false
copyhand = false;
//The currenthandname is a duplicate and shouldn't be added to the database
duplicate = true;
//Print a message to the consol that the hand already exists
println(currenthandname + " already exists in database");
//If the currenthandname is not in the joinedhandnames String
} else {
//Set copyhand to true
copyhand = true;
//The hand is not a duplicate
duplicate = false;
//Add the currenthandname to the namesofhands String [] so it isn't added a second time
namesofhands = append(namesofhands, currenthandname);
//Save over the existing Names of Hands.txt
saveStrings("data/Names of hands.txt", namesofhands);
//Add the current line of rawhandhistory to the currenthand
currenthand = append(currenthand, rawhandhistoryfile[i]);
}
}
//If the current line of the rawhandhistory String [] has "*** SUMMARY ***" in it
if (rawhandhistoryfile[i].indexOf("*** SUMMARY ***") >= 0) {
//If that hand is not a duplicate
if (!duplicate) {
//Set copyhand to false because that is the last line of the current hand
copyhand = false;
//Save the hand history text in its own .txt file
saveStrings("C:/Users/steve/Documents/Processing/My Saves/Poker_Tracker_v2/data/Saved Hands/hand" + numberofhands + ".txt", currenthand);
//Increase the numberofhands by 1
numberofhands++;
//Print a message to the consol that the hand has been added
println(currenthandname + " has now been added to the database");
//Save the numberofhands variable in the variables spread sheet so when the program
//opens next time that value is saved (this comment is for the next two lines)
variables.setInt(0, "Value of variable", numberofhands);
saveTable(variables, "data/Variables.csv");
//Reset the current hand back to being empty
currenthand = resethand;
}
}
}
} else { //If the file uploaded has some kind of error
println("Invalid file location, no hands uploaded");
}
//This for-loop adds the new hand elements into the database
//For each number 0 to the numberofhands integer
for (int i = 0; i < numberofhands; i++) {
//Create a new Hand object from the corresponding hand.txt file
newhand = new Hand(loadStrings("Saved Hands/hand" + i + ".txt"));
//Add that hand into the database
overalldatabase = (Hand[])append(overalldatabase, newhand);
}
class Hand{
String [] rawtext;
String name;
Hand(String[] handtext) {
rawtext = handtext;
name = trim(rawtext[0].substring(0, rawtext[0].indexOf("Zone")));
}
}
The sample hands I am testing the program with is a .txt file with 26 different hands in it, no duplicates. When I run the program the first time, I get 26 .txt files saved into the data/Saved Hands directory, overalldatabase has 26 Hand objects in it, the listofnames.txt file has 26 hand names in it, and my Variables table has a value of 26 in the location where I store the int numberofhands. When I run the program a second time, all of that remains the same except the overalldatabase Hand [] doubles to 52 with each overalldatabase[0] through overalldatabase[25] being duplicated in overalldatabase[26] to overalldatabase[51]. When I run the program a third time, there is no change from the results the second time.
How can I fix my code so the overallhanddatabase[] does not duplicate on the second usage?