3

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

4
  • What does Slump mean? Commented Oct 24, 2016 at 11:30
  • picking a random object Commented Oct 24, 2016 at 11:31
  • Do you have your Player objects in a List or an Array ? Commented Oct 24, 2016 at 11:33
  • pretty sure its an array, however that shouldnt really matter as I would like to avoid using them Commented Oct 24, 2016 at 11:35

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Cool thanks, will try it. Out of curiosity, is there anyway to do it without using a list or an array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.