Hi, I was writing a program that would select a random player from my array:
let players = ["Ja Morant", "Luka Doncic", "Shai Gilgeous-Alexander", "Joel Embiid", "Nikola Jokic", "Giannis Antetokoumpo"]
It would select a random player from this array as well as a random amount of votes between 1 and 10 to assign to them. The winner is supposed to at least 6 votes so I wanted to create a function that would eliminate a player that had less votes from the array, but I don't know how to. Please help
ibex10
2
To start with, do you already have something like this?
struct Player {
let name: String
var votes: Int
init (_ name: String, _ votes: Int = 0) {
self.name = name
self.votes = votes
}
}
struct Game {
var players: [Player] = [
.init ("Ja Morant"),
.init ("Luka Doncic"),
.init ("Shai Gilgeous-Alexander"),
.init ("Joel Embiid"),
.init ("Nikola Jokic"),
.init ("Giannis Antetokoumpo")
]
mutating func selectRandomPlayer () -> Player {
let index = Int.random (in: 0..<players.count)
let votes = Int.random (in: 1...10)
players [index].votes = votes
return players [index]
}
func eliminatePlayer (_ player: Player) {
...
}
}
yes I have a structure with the name and votes, the only thing I didn't know how to do was the function to eliminate the players.
Here is my current code:
struct Player {
var name: String
var votes: Int
init (_ name: String, _ votes: Int = 0) {
self.name = name
self.votes = votes
}
}
func eliminatePlayer (_ player: Player) {
// What to write here
}
let randPlayer = player.randomElement()
let votes = Int.random(in: 0..<10)
var players: [Player] = [
.init ("Ja Morant"),
.init ("Luka Doncic"),
.init ("Shai Gilgeous-Alexander"),
.init ("Joel Embiid"),
.init ("Nikola Jokic"),
.init ("Giannis Antetokoumpo")
]
I do not have a function to select a random player, I just have players.randomElement() and I don't have a Game structure.