Android Question Mind as crumbled Need to find the easiest way to get winning player (Solved)

Colin Evans

Active Member
Licensed User
Longtime User
Hi, my mind is mush, just wondered if someone had a quick way to find the player with the highest score

I have eight players, and at the end of the game the winner is the player with the highest score, i.e.

Player 1 = 122
Player 2 = 78
Player 3 = 142
Player 4 = 67
Player 5 = 43
Player 6 = 156
Player 7 = 77
Player 8 =11

SO how do I specify that in this case Player 6 is the winner, I realise I could have multi what if statements but seems a long way round, any help greatly appreciated
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
How about something like this . . .
B4X:
Sub Process_Globals
    Private fx As JFX
    
    Type player(name As String, score As Int)
    
    Dim players As List
    
End Sub

Sub findWinner As player
    Dim winner As player
    Dim highest As Int = -1
    For Each p As player In players
        If (p.score > highest) Then
            winner = p
            highest = p.score
        End If
    Next
    Return winner
End Sub
 
Upvote 0

zed

Active Member
Licensed User
Put the results in a list and sort the list.
Sort:
Private WinnerList As List
WinnerList.Initialize
WinnerList.AddAll(Array As Int(122,122,78,142,67,43,156,77,11))
WinnerList.Sort(True)
Log(WinnerList)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you want to get the name of the player, you can use a Map.

I have developed MinimaList library to simplify this.

B4X:
Dim Player1 As Map = CreateMap("Name": "Player 1", "Score": 122)
Dim Player2 As Map = CreateMap("Name": "Player 2", "Score": 78)
Dim Player3 As Map = CreateMap("Name": "Player 3", "Score": 142)
Dim Player4 As Map = CreateMap("Name": "Player 4", "Score": 67)
Dim Player5 As Map = CreateMap("Name": "Player 5", "Score": 43)
Dim Player6 As Map = CreateMap("Name": "Player 6", "Score": 156)
Dim Player7 As Map = CreateMap("Name": "Player 7", "Score": 77)
Dim Player8 As Map = CreateMap("Name": "Player 8", "Score": 11)

Dim AllPlayers As MinimaList
AllPlayers.Initialize
AllPlayers.Add(Player1)
AllPlayers.Add(Player2)
AllPlayers.Add(Player3)
AllPlayers.Add(Player4)
AllPlayers.Add(Player5)
AllPlayers.Add(Player6)
AllPlayers.Add(Player7)
AllPlayers.Add(Player8)

AllPlayers.SortByKey("Score", False)

Log("The winner is " & AllPlayers.First.Get("Name"))
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Type Player (name As String, count As Int)

End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim players As List
    players.Initialize
    players.Add(CreatePlayer("Player 1",122)) ' Add Player to the list
    players.Add(CreatePlayer("Player 2",78))' Add Player to the list
    players.Add(CreatePlayer("Player 3",142))' Add Player to the list
    players.Add(CreatePlayer("Player 4",67))' Add Player to the list
    players.Add(CreatePlayer("Player 5",43))' Add Player to the list
    players.Add(CreatePlayer("Player 6",156))' Add Player to the list
    players.Add(CreatePlayer("Player 7",77))' Add Player to the list
    players.Add(CreatePlayer("Player 8",11))' Add Player to the list
    players.SortType("count",False) ' Sorts the list by "count" DESC of the Customtype
   
    Log(players) ' Log all Players
    Dim p1 As Player = players.Get(0) ' Get the topmost Player
    Log($"Player ${p1.name}: ${p1.count} Points"$) ' LOGS Player Player 6: 156 Points
   
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Public Sub CreatePlayer (name As String, count As Int) As Player
    Dim t1 As Player
    t1.Initialize
    t1.name = name
    t1.count = count
    Return t1
 
Last edited:
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Aeric, many thanks where can i get the MiniaList library please
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Found it and it works a treat, many thanks
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thanks
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Many thanks for all the answers, I'm sure all of them would work, this occasion I went with Aeric's option which worked brilliantly
B4X:
Sub GameEnd
    'calc winner
    Dim cPlayer1 As Map = CreateMap("Name": lblPlayer1.Text, "Score": lblPScr1.Text)
    Dim cPlayer2 As Map = CreateMap("Name": lblPlayer2.Text, "Score": lblPScr1.Text)
    Dim cPlayer3 As Map = CreateMap("Name": lblPlayer3.Text, "Score": lblPScr1.Text)
    Dim cPlayer4 As Map = CreateMap("Name": lblPlayer4.Text, "Score": lblPScr1.Text)
    Dim cPlayer5 As Map = CreateMap("Name": lblPlayer5.Text, "Score": lblPScr1.Text)
    Dim cPlayer6 As Map = CreateMap("Name": lblPlayer6.Text, "Score": lblPScr1.Text)
    Dim cPlayer7 As Map = CreateMap("Name": lblPlayer7.Text, "Score": lblPScr1.Text)
    Dim cPlayer8 As Map = CreateMap("Name": lblPlayer8.Text, "Score": lblPScr1.Text)

    Dim AllPlayers As MinimaList
    AllPlayers.Initialize
    AllPlayers.Add(cPlayer1)
    AllPlayers.Add(cPlayer2)
    AllPlayers.Add(cPlayer3)
    AllPlayers.Add(cPlayer4)
    AllPlayers.Add(cPlayer5)
    AllPlayers.Add(cPlayer6)
    AllPlayers.Add(cPlayer7)
    AllPlayers.Add(cPlayer8)

    AllPlayers.SortByKey("Score", False)

    Log("The winner is " & AllPlayers.First.Get("Name"))
    Log("With a score of " & AllPlayers.first.get("Score"))
        btnSendH.Enabled = False
        pnlExit.Visible = True
    lblWinner.Text = "Player " & AllPlayers.First.Get("Name") & " wins"
End Sub

Thanks again
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Dim cPlayer1 As Map = CreateMap("Name": lblPlayer1.Text, "Score": lblPScr1.Text)
Dim cPlayer2 As Map = CreateMap("Name": lblPlayer2.Text, "Score": lblPScr1.Text)
I believe the second line is getting value from lblPScr2.Text,
third will be lblPScr3.Text, etc...
B4X:
Dim cPlayer2 As Map = CreateMap("Name": lblPlayer2.Text, "Score": lblPScr2.Text)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
I told you my mind was mushed, you are correct and so is my code now, many thanks
B4X:
Dim cPlayer1 As Map = CreateMap("Name": lblPlayer1.Text, "Score": lblPScr1.Text)
    Dim cPlayer2 As Map = CreateMap("Name": lblPlayer2.Text, "Score": lblPScr2.Text)
    Dim cPlayer3 As Map = CreateMap("Name": lblPlayer3.Text, "Score": lblPScr3.Text)
    Dim cPlayer4 As Map = CreateMap("Name": lblPlayer4.Text, "Score": lblPScr4.Text)
    Dim cPlayer5 As Map = CreateMap("Name": lblPlayer5.Text, "Score": lblPScr5.Text)
    Dim cPlayer6 As Map = CreateMap("Name": lblPlayer6.Text, "Score": lblPScr6.Text)
    Dim cPlayer7 As Map = CreateMap("Name": lblPlayer7.Text, "Score": lblPScr7.Text)
    Dim cPlayer8 As Map = CreateMap("Name": lblPlayer8.Text, "Score": lblPScr8.Text)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Aeric, just one problem I noticed if say there were three players and the resulting scores were

Player A = 6
Player B = 41
Player C = 14

the winner should be Player B but the code states Player A is the winner, I'm assuming because the first character is the highest, any idea's, many thanks if you get chance to have a look
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Can you show your code for this example?
Are you using String value for the Score?

Try:
B4X:
Dim Player1 As Map = CreateMap("Name": "Player A", "Score": lblPScr1.Text.As(Int))
Dim Player2 As Map = CreateMap("Name": "Player B", "Score": lblPScr2.Text.As(Int))
Dim Player3 As Map = CreateMap("Name": "Player C", "Score": lblPScr3.Text.As(Int))

Dim AllPlayers As MinimaList
AllPlayers.Initialize
AllPlayers.Add(Player1)
AllPlayers.Add(Player2)
AllPlayers.Add(Player3)

AllPlayers.SortByKey("Score", False)
Log("The winner is " & AllPlayers.First.Get("Name")) ' The winner is Player B
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check this example.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Aeric, yes I am using string values, your example works but the game allows for 1 to 8 players, so presumably I would need to set the text to 0 for non players in the map
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Aeric, yes I am using string values, your example works but the game allows for 1 to 8 players, so presumably I would need to set the text to 0 for non players in the map
So setting the scores at the start and restart to
B4X:
    lblPScr1.Text = 0
    lblPScr2.Text = 0
    lblPScr3.Text = 0
    lblPScr4.Text = 0
    lblPScr5.Text = 0
    lblPScr6.Text = 0
    lblPScr7.Text = 0
    lblPScr8.Text = 0
and the following now works perfectly
B4X:
Sub GameEnd
    'calc winner
    Dim cPlayer1 As Map = CreateMap("Name": lblPlayer1.Text, "Score": lblPScr1.Text.As(Int))
    Dim cPlayer2 As Map = CreateMap("Name": lblPlayer2.Text, "Score": lblPScr2.Text.As(Int))
    Dim cPlayer3 As Map = CreateMap("Name": lblPlayer3.Text, "Score": lblPScr3.Text.As(Int))
    Dim cPlayer4 As Map = CreateMap("Name": lblPlayer4.Text, "Score": lblPScr4.Text.As(Int))
    Dim cPlayer5 As Map = CreateMap("Name": lblPlayer5.Text, "Score": lblPScr5.Text.As(Int))
    Dim cPlayer6 As Map = CreateMap("Name": lblPlayer6.Text, "Score": lblPScr6.Text.As(Int))
    Dim cPlayer7 As Map = CreateMap("Name": lblPlayer7.Text, "Score": lblPScr7.Text.As(Int))
    Dim cPlayer8 As Map = CreateMap("Name": lblPlayer8.Text, "Score": lblPScr8.Text.As(Int))
    Dim AllPlayers As MinimaList
    AllPlayers.Initialize
    AllPlayers.Add(cPlayer1)
    AllPlayers.Add(cPlayer2)
    AllPlayers.Add(cPlayer3)
    AllPlayers.Add(cPlayer4)
    AllPlayers.Add(cPlayer5)
    AllPlayers.Add(cPlayer6)
    AllPlayers.Add(cPlayer7)
    AllPlayers.Add(cPlayer8)
    AllPlayers.SortByKey("Score", False)
    btnSendH.Enabled = False
    btnExitGame.Visible = True
    lblWinner.Text = "Player " & AllPlayers.First.Get("Name") & " wins"
    pnlExit.Visible = True
End Sub

Many thanks for taking the time to sort my problem out,
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…