So I have made these objects from my class Player
Player Silva = new Player("André Silva", 2, 5);
Player Moses = new Player("Victor Moses", 3, 3);
Player Batshuayi = new Player("Michy Batshuayi", 3, 4);
Player Medel = new Player("Gary Medel", 4, 4);
And then I have objects made from my class Club
(Dont worry about the values in any of these, just football related stuff for those who does not know)
Club Barcelona = new Club("FC Barcelona", 5);
Club RealMadrid = new Club("Real Madrid", 5);
Club Inter = new Club("Inter", 4);
Club Liverpool = new Club("Liverpool", 4);
Anyways, I would like to random slump one of the players, and one of the clubs and compare their values in an if statement. As of now I have put all of these in arrays
int[] Playerability = {Silva.ability,Moses.ability,Batshuayi.ability,Medel.ability};
int[] Clubrating = { Barcelona.clubrating, RealMadrid.clubrating, Inter.clubrating};
So I can use a simple random and put that variable in the array
int randomclub = randomnumber.Next(16);
int randomplayer = randomnumber.Next(28);
if (Playerability[randomplayer] >= Clubrating[randomclub] ||
Playerpotential[randomplayer] >= Clubrating[randomclub])
but this seems very innefective if I want many more objects, so I am wondering if there is an easier solution to this? Sorry if its obvious but I cant seem to find it. Language is c# btw
A way to handle this would be to have a static List in your Club and Player classes, which would be filled with the different instances created by your program. This list would be filled in the constructor of your class by adding the newly created instance in the list.
For instance, for your Player class, you would have :
public class Player
{
// Static list keeping all your instances
public static List<Player> players = new List<Player>();
// Constructor
public Player(/* Your different parameters */)
{
// Your class initialization
players.Add(this);
}
// Rest of your class definition
}
Then you have the choice. You can add in your class a static method to get a random element of this list, like this :
public static Player GetRandomPlayer()
{
Player player = players[randomnumber.Next(players.Length)];
return player;
}
Or simply call it in your code by doing :
Player player = Player.players[randomnumber.Next(Player.players.Length)];
In the end, if you also apply the same idea for your Club class, your if statement would look like this :
Player player = Player.GetRandomPlayer();
Club club = Club.GetRandomClub();
if (player.ability >= club.clubrating || player.potential >= club.clubrating)
{
// What you want to do at that point
}
If you put your Club and Player objects in a List for example you could do this:
var listOfPlayers = new List<Player>() { Silva, Moses };
var listOfClubs = new List<Club>() { Barcelona, RealMadrid };
var randomPlayer = listOfPlayers[randomnumber.Next(listOfPlayers.Length)];
var randomClub = listOfClubs [randomnumber.Next(listOfClubs.Length)];
if (randomPlayer.ability >= randomClub.clubrating ||
randomPlayer.potential >= randomClub.clubrating)
{
//Do your stuff
}
The same works if you put your objects into an array. The point is you can work with objects and not store the properties of the objects into new arrays.
This way you just need to fill new Club and Player objects into the appropriate lists and don't have to change the rest of the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With