# Top 5 scores algorithm

• (2 Pages)
• 1
• 2

## 15 Replies - 348 Views - Last Post: 29 November 2018 - 11:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=413830&amp;s=2b13f5bc42b1df1c895f9c6f27e2b2fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

# Top 5 scores algorithm

Posted 28 November 2018 - 01:28 PM

hi, Im struggling a little with getting my head around the logic or exactly what it is im trying to code.I hope you can help me out with some pointers. Im perfectly happy to code it my self but need a push in the right direction to help me.

I have created a simple game, and need to create a simple way of writing the score to a file, if it is in the top 5. The file I am using will be a csv file:

At the end of the game, I need to write the score to the csv file if it is in the top 5. I cant quite get my head around how to do this. The code I have so far is below...

```        Dim path As String = "P:\scores.csv"
Dim split() As String
Dim new_score As Integer = Label1.Text
Dim scores_arr() As String
Dim i As Integer = 0

For Each Line As String In System.IO.File.ReadLines(path)    'loop through the lines of the file
split = Line.Split(",")                             'split around the comma
'MsgBox(split(1))

If split(1) > new_score Then    'the new score is less than the lowest high score
MsgBox("new score is not in the top 5") 'do nothing as not a high score
Else
If new_score < split(1) Then     '?? HOW TO WORK OUT HOW TO WRITE IT IN THE LIST IF IT IS A HIGH SCORE
scores_arr(i) = new_score
i += 1
End If
End If
Next

```

any help would be much appreciated.

Is This A Good Question/Topic? 0

## Replies To: Top 5 scores algorithm

### #2 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 01:31 PM

I would suggest using the StreamWriter.

https://docs.microso...framework-4.7.2

### #3 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 01:43 PM

THanks, I will when I get to that part. What Im asking is how to work out if a new score should be in the highscore file, and how that would be achieved

### #4 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 01:50 PM

A basic file is not very smart, right? Not something you can easily search. What you would need to do is read in the entire file, split it up as you see fit (array of strings), and then use a loop to examine each item in the array. Compare against what your new score. If it needs to be added then replace it in the array. Sort as needed. Write out using streamwriter.

### #5 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 01:57 PM

YEah that's what I a trying to do. thanks I will have a try. Writing it back out to the file is the easy part for me..Is the code I posted along the right lines?

### #6 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 01:58 PM

No. Your loop should go through the array crated by the split.. not before it, or around it. AFTER it.

### #7 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 02:43 PM

what do you think to this? It kind of works, but just replaces the new score in the right place in the array..however I would need to move all the other scores down one..

```    Dim path As String = "P:\scores.csv"
Dim score_data(4, 1) As String '= System.IO.File.ReadAllLines(path)
Dim i As Integer = 0
Dim bits() As String
Dim newscore As Integer = 3 'get this for real from the game

'read data from file into array
For Each Line As String In System.IO.File.ReadLines(path)
' MsgBox(Line)
bits = Line.Split(",")

score_data(i, 0) = bits(0)
score_data(i, 1) = bits(1)
i += 1
Next

'loop through array and add the score
For i = 0 To 4
If score_data(i, 1) > newscore Then

score_data(i, 1) = newscore
Exit For
End If
Next
```

### #8 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 02:51 PM

Again.. read them into a collection. You can use the collection's sort, or roll you own, and then write out how ever many X you need.

### #9 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 03:36 PM

Appologies for being a bit dense. I am pretty new to this programming lark. I have come up with this so far..hopefully this is more what you had in mind:

```
Public al As ArrayList = New ArrayList()

'////////////////////////////////////////
Dim i As Integer
Dim newscore As Integer
Dim count As Integer = 0

newscore = InputBox("enternew score")

al.Sort()

If al.Count > 5 Then
al.RemoveAt(0)
End If

'show whats in the array list in a textbox (temp)
TextBox1.Text = ""
For Each i In al
TextBox1.Text &= i
TextBox1.Text &= vbNewLine
Next
```

How could I modify this so that I could store the persons name as well as the score, without having parallel arrays which I assume is a bad idea..

This post has been edited by skip2111: 28 November 2018 - 03:38 PM

### #10 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 03:39 PM

Create a class that contains a variable for 'name' and a variable for 'score'. That point you would probably need your data separated by lines and commas.

aaa, 1123
bbb, 12
ccc, 9
... etc

### #11 Sheepings

• Senior Programmer

Reputation: 195
• Posts: 1,089
• Joined: 05-December 13

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 03:42 PM

Wait, If this is a game, why are the scores on a file? Wouldn't it be best to keep track of the scores of the game in a array of integers inside the game?

If you want to continue the way you are with the file idea. Consider encrypting the scores later on, so they can't be changed by editing the file.

### #12 modi123_1

• Suitor #2

Reputation: 14771
• Posts: 59,043
• Joined: 12-June 08

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 03:45 PM

@sheep - I think that is a little overboard for this homework assignment.

### #13 Sheepings

• Senior Programmer

Reputation: 195
• Posts: 1,089
• Joined: 05-December 13

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 04:00 PM

Ah yea, what am I thinking

### #14 skip2111

Reputation: 0
• Posts: 17
• Joined: 08-October 18

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 04:44 PM

I have just learned how to create a simple class..I dont quite understand how this should like with my collection?

### #15 Sheepings

• Senior Programmer

Reputation: 195
• Posts: 1,089
• Joined: 05-December 13

## Re: Top 5 scores algorithm

Posted 28 November 2018 - 07:09 PM

It outputs like so ::

Output when player 1 is winning ::
```Player1 has a score of 3
Player2 has a score of 1
It is 3::1 to Player1
```

Output when player 2 is winning ::
```Player1 has a score of 1
Player2 has a score of 3
It is 3::1 to Player2
```

Output when its a draw ::
```Player1 has a score of 3
Player2 has a score of 3
It is a draw between Player1 and Player2, Scoring 3::3
```

Maybe the use of this will push you in the direction you want to go with your game. Try this ::

```Namespace Game
Public Class Score
Public Shared Dictionary As New Dictionary(Of Integer, Integer)
Private Shared Player1CurrentScore As Integer
Private Shared Player2CurrentScore As Integer

Public Shared Property GetSetP1CurrentScore() As Integer
Get
Return Player1CurrentScore
End Get
Set(ByVal value As Integer)
Player1CurrentScore = value
End Set
End Property
Public Shared Property GetSetP2CurrentScore() As Integer
Get
Return Player2CurrentScore
End Get
Set(ByVal value As Integer)
Player2CurrentScore = value
End Set
End Property
End Class
Public Class Users

Public Enum Players
Player1
Player2
End Enum
Public Shared Property Player1Score() As Integer
Get
If (Score.Dictionary.TryGetValue(Players.Player1, Score.GetSetP1CurrentScore)) Then 'Check the current score, and return score during check
Return Score.GetSetP1CurrentScore 'This is the current score
End If
Return 0 'We don't have a score yet
End Get

Set(ByVal SetScore As Integer)
If (Score.Dictionary.ContainsKey(Players.Player1)) Then 'Check if the key exists
With Score.Dictionary 'if it does set the value
.Item(Players.Player1) = SetScore
End With
Else 'It wasn't inserted yet, so lets add it
With Score.Dictionary
End With
End If
End Set
End Property
Public Shared Property Player2Score() As Integer
Get
If (Score.Dictionary.TryGetValue(Players.Player2, Score.GetSetP2CurrentScore)) Then 'Check the current score, and return score during check
Return Score.GetSetP2CurrentScore 'This is the current score
End If
Return 0 'We don't have a score yet
End Get

Set(ByVal SetScore As Integer)
If (Score.Dictionary.ContainsKey(Players.Player2)) Then 'Check if the key exists
With Score.Dictionary 'if it does set the value
.Item(Players.Player2) = SetScore
End With
Else 'It wasn't inserted yet, so lets add it
With Score.Dictionary
End With
End If
End Set
End Property
End Class
End Namespace

Public Class Form1

Game.Users.Player1Score() = 5 'Set the score
Dim varP1 As Integer = Game.Users.Player1Score() 'Get player1's current score
Console.WriteLine(Game.Users.Players.Player1.ToString & " has a score of " & varP1)
Game.Users.Player2Score() = 2 'Set the score
Dim varP2 As Integer = Game.Users.Player2Score() 'Get player2's current score
Console.WriteLine(Game.Users.Players.Player2.ToString & " has a score of " & varP2)
If varP1 > varP2 Then
Console.WriteLine("It is " & varP1 & "::" & varP2 & " to " & Game.Users.Players.Player1.ToString)
ElseIf varP1 = varP2 Then
Console.WriteLine("It is a draw between " & Game.Users.Players.Player1.ToString & " and " & Game.Users.Players.Player2.ToString & ", Scoring " & varP2 & "::" & varP1)
Else
Console.WriteLine("It is " & varP2 & "::" & varP1 & " to " & Game.Users.Players.Player2.ToString)
End If
End Sub
End Class

```

Final output after some edits ::

```Player1 has a score of 5
Player2 has a score of 2
It is 5::2 to Player1
```

Study it, and learn to debug if you don't know how-to already, you can read this tutorial on debugging.

Just curious, but what do all the buttons on your application do?

Edit: Altered class

This post has been edited by Sheepings: 29 November 2018 - 02:55 PM