Highscores help!

Blitz3D Forums/Blitz3D Programming/Highscores help!

blade007(Posted 2007) [#1]
I need help for writing a highscores function. I don't understand why it won't work. so far here's what it looks like


;pn1 and pn2 are player 1's socre and player 2's score
;time4# is time used in secs
;time5 is time used in mins ex: time5+":"+time4#
;lock1 is the number of human players

Function record(pn1,pn2,time4#,time5,lock1)
sett = OpenFile("settings.data")
If lock1 = 1 Then ; check if its a one player game
If pn1 > pn2 Then ; check if player 1 won
ReadLine(sett)
ReadLine(sett)
ReadLine(sett)
ReadLine(sett);move the cursor to the right position

ReadLine(sett);name
pn3t5=ReadLine(sett);mins
pn3t4#=ReadLine(sett);secs
ReadLine(sett);level
If time5 = pn3t5 Then; check if you beat the last highscore
Else
If time5 > pn3t5 Then Goto highscored
If time4# > pn3t4# Then
.highscored
SeekFile(sett,0);start back at first line
ReadLine(sett)
ReadLine(sett)
ReadLine(sett)
ReadLine(sett) ; this brings the cursor to the right spot

Print "congrats you made the highscores enter your name! "
name$=Input$("")
Trim$(name$)
Print"writing data..."
WriteLine(sett,name$)
WriteLine(sett,time5)
WriteLine(sett,time4#)
WriteLine(sett,level)
Print"data written!"

EndIf
EndIf
EndIf
EndIf
CloseFile(sett)
End Function


Rob Farley(Posted 2007) [#2]
Before looking into that you need to clean up your code, that is seriously ugly.

I just tried tabbing that out and I still made no sense of it.

It looks like you've got a label in the middle of an if statement?
If time5 > pn3t5 Then Goto highscored
If time4# > pn3t4# Then
.highscored
Why not just
If (Time5 > PN3t3) or (Time4 > PN3t4) Then
     blah
End if
Also, personally I'd read the file into a type or array, work on that, then write the whole thing back rather than mess about with seeking.


semar(Posted 2007) [#3]
It's quite hard to read your code. A couple of hints:

1) use variable names that help to understand what they are for. If you need to store the number of players, then use a variable named num_of_players, instead of a cryptic 'lock1'.

The same applyes to the following variables:
;time4# is time used in secs
;time5 is time used in mins ex: time5+":"+time4#
;sett for read the settings
I would suggest:
t_seconds for seconds (no float needed since seconds are integers);
t_minutes to store the minutes;
settings to store the file pointer (settings.data)

and so on.

Of course you don't need to use very long variable names, which lead to typos, but at least use a name that resembles what the variable is for; otherwise your code will be hard to be read even from you !
Example: which of the following function declarations is more readable ?
Function record(pn1,pn2,time4#,time5,lock1)

or
Function record(player_1,player_2,t_seconds,t_minutes,num_of_players)


2) Indent your code and use If..Then..Else..Endif wisely.

3) DO NOT use GOTO for any reason. Use functions instead, or a well formatted if-then-else.

To summarize, don't be lazy and use understandable variable names, clean up your code and indent it. The code will gain readability, and you will be able to spot the error in a snap.

Oh, and if you need to post a code snippet on this forum, then enclose your code between the <code> and </code> directives (substitute the <> with []) like so:
;your code here


Hope it helps,
Sergio.


blade007(Posted 2007) [#4]
EDIT: deleted post


blade007(Posted 2007) [#5]
ok i rewrote the code EDIT: now that my code is readable can someone please tell me why it doesn't work.

Function record(points1,points2,t_secs#,t_mins,num_of_players)
	settings_file = OpenFile("settings.data")
		If num_of_players = 1 Then
			If points1 > points2 Then
				ReadLine(settings_file)
				ReadLine(settings_file)
				ReadLine(settings_file)
				ReadLine(settings_file)

				ReadLine(settings_file);name
				hiscore_t_mins=ReadLine(settings_file);mins
				hiscore_t_secs#=ReadLine(settings_file);secs
				ReadLine(settings_file);level
					If Not t_mins = hiscore_t_mins Then
					If t_mins > hiscore_t_mins Or t_secs# > hiscore_t_secs# Then
						SeekFile(settings_file,0)
					ReadLine(settings_file)
					ReadLine(settings_file)
					ReadLine(settings_file)
					ReadLine(settings_file)
					Print "congrats you made the high scores enter your name! "
						name$=Input$("")
							Trim$(name$)
					Print"writing data..."
						WriteLine(settings_file,name$)
						WriteLine(settings_file,t_mins)
						WriteLine(settings_file,t_secs#)
						WriteLine(settings_file,level)
					Print"data written!"

				EndIf
			EndIf
		EndIf
	EndIf
CloseFile(settings_file)
End Function



semar(Posted 2007) [#6]
@blade0007,
I don't have Blitz at the moment, so I can't try your code; but as a rule of thumb, I would suggest you two more things:

1) use some debuglog.print and display the values that you compare in the debug window (you may turn the debug mode on in your IDE). This way you will find the bug in a couple of minutes, I tell you.

For example, before the line:
If (Not (t_mins = hiscore_t_mins)) Then
I would write:
Debuglog.Print "t_mins = <" + t_mins + ">"
Debuglog.Print "hiscore_t_mins = <" + hiscore_t_mins + ">"
and also here:

Debuglog.Print "t_mins = <" + t_mins + ">"
Debuglog.Print "hiscore_t_mins = <" + hiscore_t_mins + ">"
Debuglog.Print "t_secs# = <" + t_secs# + ">"
Debuglog.Print "hiscore_t_secs# = <" + hiscore_t_secs# + ">"
If ((t_mins > hiscore_t_mins) Or (t_secs# > hiscore_t_secs#)) Then


2) I would enclose the logical conditions in your If statements, so that they don't lead to confusion (expecially when there are multiple conditions with Or or And, which seems to be your case):


If Not t_mins = hiscore_t_mins Then
I would write instead:
If (Not (t_mins = hiscore_t_mins)) Then
and also the line:
If t_mins > hiscore_t_mins Or t_secs# > hiscore_t_secs#
I would rewrite it like:
If ((t_mins > hiscore_t_mins) Or (t_secs# > hiscore_t_secs#)) Then

Pleae note the parenthesis.

Also don't forget to put some Debuglog.Print when you write the data to the file, to see what you really are writing there.

Good luck by bug hunting...
Sergio.

P.S.
Your code is much more readable now - well done. Are you sure you need a float to store the seconds ? I would use an integer instead - just my opinion.


blade007(Posted 2007) [#7]
ya thanks this speed up my "code reading" but this doesn't solve my question. Is there a bug with writeline? err i mean that the code will get to the input part but it seems to ignore the writeline part