Keeping a variable alive | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Keeping a variable alive

I've been struggling with this for a while, so I have made a rock, paper and scissor consol based game with a score tracker. It consists out of a couple of diffrent classes, so when ever either the computer or player win the score will go up by one to the respected winner, but it rather reset back to the 0 every time it's called. I know the whole garbage collecter cenario, so is there a way to keep the score alive or is there a diffrent way I can achieve this, any ideas will be welcome.

17th Nov 2016, 5:37 AM
Josias Oberholster
Josias Oberholster - avatar
8 Answers
+ 4
@aditya. Thank you. it's running perfect.
22nd Nov 2016, 4:27 PM
Josias Oberholster
Josias Oberholster - avatar
+ 3
All console based program or any simple program will run on ram. And when you close program it reset to 0. you have to data in file like txt or in SQL. And then you can get it right. and you can show your program code, I will improve a little bit. Above explanation is about leaderboard. I thick their is a logical error in your code.
17th Nov 2016, 6:02 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
@aditya. I followed you, if you can follow me and check the RockPaperScissor code then you can have a look at it. Keep in mind I coded that with diffrent classes is mind, so I had to push it all into one class (not that I really think it'll make a difference in the end result). This is the easiest way I can imagen for you to help me.
21st Nov 2016, 5:41 PM
Josias Oberholster
Josias Oberholster - avatar
+ 3
Hey I could not post your updated code as answer, yeah I have correct it and it is working fine. I have posted in my code section please check it. And copy your code Than I will remove that.
22nd Nov 2016, 6:25 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
Then I remove it.
22nd Nov 2016, 4:29 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Thank you for the reply, well the score doesn't need to stay there when I re-open the file, it's only being used for the time that the program is running.
17th Nov 2016, 6:29 AM
Josias Oberholster
Josias Oberholster - avatar
+ 1
after your program killed, the memory that used for storing your application data will be released too you need to store that score into a file, n everytime your program run, you need to read the file to reload the last score you save there it isn't about garbage collector. it's about saving data to disk
17th Nov 2016, 6:08 AM
Raizal I N Pregnanta
Raizal I N Pregnanta - avatar
+ 1
Please show your code I think there is an logical error
17th Nov 2016, 6:35 AM
Aditya kumar pandey
Aditya kumar pandey - avatar