min() and max() function

hello everyone,
I want to use the min() and max() function to limit the number of players to minimum 0 and maximum 10. But I'm starting in swift and I don't know the algorithm very well yet. Can you help me to do that?
Answered by Claude31 in 658453022
Thanks for feed back.

Don't forget to close the thread by marking the correct answer.

Good continuation.
Don't use min and max for this.

For instance, create a func to test validity:

Code Block
func isValidNumberOfPlayers(_ nb: Int) -> Bool {
return nb > 0 && nb <= 10
}

Then, when you create or read the list of players, for instance in an array with their names:
Code Block
var players: [String] = [] // You populate the array somewhere


In the func where you declare the players, check the value, call
Code Block
if !isValidNumberOfPlayers(players.count) {
print("number of players is not correct")
// And do whatever you need to do
}


If you want to give a more precise test result, you could do this:
Code Block
enum PlayersCheckState {
case ok
case null
case tooMany
}
func isValidNumberOfPlayers(_ nb: Int) -> PlayersCheckState {
switch nb {
case ...0 : return .null
case 1...10: return .ok
default : return .tooMany
}
}

Then call
Code Block
switch isValidNumberOfPlayers(playersCount) {
case .null : print("No player")
case .tooMany: print("too many players")
case .ok: break // do whatever you need to do here
}

Thank you !
Accepted Answer
Thanks for feed back.

Don't forget to close the thread by marking the correct answer.

Good continuation.
min() and max() function
 
 
Q