Please visit my new blog. I will no longer be updating this one, as I created it long ago and my writing abilities/opinion formation were far less evolved.
Thanks,
Daniel
Daniel' s blog
Tuesday, April 2, 2013
Sunday, January 30, 2011
Saving Game Time as a Score in Java
I am writing a Sudoku in Java as my CS project. The Sudoku is loaded from a file and then the user is allowed to enter the solution. This is checked each time for repetitions in rows, columns or squares every time the user fills a cell. (Whether all the cells are filled is also determined.)
Getting to the point of this post, I managed to find a way how to find the game time in order to save it as a score if the user wins. This is done through System.currentTimeMillis(). This built-in method returns a long representing the program's current execution time. Hence saving the start time of the game and then subtracting the current execution time on the game's end. Below is a rough and simple pseudocode algorithm showing how this can be done (my pseudocode is closer to Java):
long startTime = System.currentTimeMillis();
.
.
.
saveScore(startTime-System.currentTimeMillis());
But what if the user exits the game and resumes the game later? One needs to save the time to a file for later retrieval. This is a rather easy task, but I had some trouble with implementing it cause I was passing a long to store the time in the "read time from file method". In Java parameters are passed by value and hence the value read from file will be lost if it is passed as argument, since long is not a reference. So I tried returning the long value this time and it did the trick. Also note that it would be safer to use stream files since it prevents the user from editing the file with a text editor such as Notepad, Vim or Notepad++. In order to read this type of file, a piece of code (program/method) using the same programming language and data format should be implemented, hence making it more difficult for average users to mess around with such files.
So here we go with the save and restore game time algorithm:
long startTime = System.currentTimeMillis();
long duration;
.
.
if user resumes last game
duration = value read from file;
.
.
.
.
.
.
.
.
.
.
.
.
.
.
if user exited the game without solving the sudoku
write duration+startTime-System.currentTimeMillis() to file;
Getting to the point of this post, I managed to find a way how to find the game time in order to save it as a score if the user wins. This is done through System.currentTimeMillis(). This built-in method returns a long representing the program's current execution time. Hence saving the start time of the game and then subtracting the current execution time on the game's end. Below is a rough and simple pseudocode algorithm showing how this can be done (my pseudocode is closer to Java):
long startTime = System.currentTimeMillis();
.
.
.
saveScore(startTime-System.currentTimeMillis());
But what if the user exits the game and resumes the game later? One needs to save the time to a file for later retrieval. This is a rather easy task, but I had some trouble with implementing it cause I was passing a long to store the time in the "read time from file method". In Java parameters are passed by value and hence the value read from file will be lost if it is passed as argument, since long is not a reference. So I tried returning the long value this time and it did the trick. Also note that it would be safer to use stream files since it prevents the user from editing the file with a text editor such as Notepad, Vim or Notepad++. In order to read this type of file, a piece of code (program/method) using the same programming language and data format should be implemented, hence making it more difficult for average users to mess around with such files.
So here we go with the save and restore game time algorithm:
long startTime = System.currentTimeMillis();
long duration;
.
.
if user resumes last game
duration = value read from file;
.
.
.
.
.
.
.
.
.
.
.
.
.
.
if user exited the game without solving the sudoku
write duration+startTime-System.currentTimeMillis() to file;
Thursday, June 3, 2010
exams, testing Firefox 3.6.4, programming a Sudoku
Much is going on lately since I'm very busy studying for exams, attempting to write a Sudoku and testing the new minor update to Firefox: 3.6.4 code-named Lorentz. I am bit disappointed cause of how things are going. Lorentz has a JS speed penalty, I am having an error in an if condition and I am not doing good at exams.
Monday, May 17, 2010
my first blog
Hello people all over the world!
This my first blog in which I will be posting stuff regarding my public life and achievements. I am a Junior College student at Malta studying Math and Computing at A-level. I enjoy playing basketball, going out with friends and taking life a bit lightly sometimes. I am very keen about computers and programming. In fact I volunteered as a live chat agent at Mozilla last summer.
As regards to this blog, I hope to update it as regularly as possible but I cannot promise much cause I'm really busy lately.
This my first blog in which I will be posting stuff regarding my public life and achievements. I am a Junior College student at Malta studying Math and Computing at A-level. I enjoy playing basketball, going out with friends and taking life a bit lightly sometimes. I am very keen about computers and programming. In fact I volunteered as a live chat agent at Mozilla last summer.
As regards to this blog, I hope to update it as regularly as possible but I cannot promise much cause I'm really busy lately.
Subscribe to:
Posts (Atom)