First post, i'm very new to C# and programming in general.
I'm making a football player generator application solely for practise purposes, where i have a class with a constructor that generates an object with two strings (firstname and last name) and a lot of ints. (different skill attributes, like shooting power, passing etc.
Bear with me, i'll post my chucks of code in a sec.
So i want all of these objects im making to be stored with all its variables in some kind of way. And after a great deal of googling, im down to arrays of different sorts, lists and dictionaries.
first off, this is my class:
class player
{
public string firstName;
public string lastName;
public string playPos;
public int playerId = 999;
public int isKeeper;
public int level;
public int age;
public int dKeeper;
public int dTackle;
public int dMarking;
public int mPlaymaking;
public int mCrossing;
public int fShooting;
public int fShotPower;
public int aDribbling;
public int aHeading;
public int aPassing;
public void autoGenPlayer()
{
//new killing random that i got off of Stack Overflow
Random rAG = new Random(Guid.NewGuid().GetHashCode());
//PlayerId, dont mind it does not work properly yet.
playerId = playerId + 1;
//a level integer just to base the other stats around, to keep the stats from spreading too much
level = rAG.Next(2, 11);
//Generating names, calling the methods a bit lower in this class.
firstName = firstNameGenerator();
lastName = lastNameGenerator();
//giving the player an age
age = rAG.Next(16, 35);
//a one out of seven chance to become a goal keeper.
isKeeper = rAG.Next(1,8);
//skills based on level. Max value is 20
dTackle = rAG.Next(level, level * 2);
dMarking = rAG.Next(level, level * 2);
mPlaymaking = rAG.Next(level, level * 2);
mCrossing = rAG.Next(level, level * 2);
fShooting = rAG.Next(level, level * 2);
fShotPower = rAG.Next(level, level * 2);
aDribbling = rAG.Next(level, level * 2);
aHeading = rAG.Next(level, level * 2);
aPassing = rAG.Next(level, level * 2);
//Checks if the player is a keeper. 5 is chosen at random. There are obv better ways to do this, but it doesnt matter right now.
if (isKeeper == 5)
{
//This basically just makes the keeper a keeper, and a shit outfield player.
dKeeper = rAG.Next(level, level * 2);
playPos = "Goal Keeper";
dTackle = rAG.Next(1, 3);
dMarking = rAG.Next(1, 3);
mPlaymaking = rAG.Next(1, 3);
mCrossing = rAG.Next(1, 3);
fShooting = rAG.Next(1, 3);
fShotPower = rAG.Next(1, 3);
aDribbling = rAG.Next(1, 3);
aHeading = rAG.Next(1, 3);
aPassing = rAG.Next(1, 3);
}
else
{
//if not a keeper, shit keeper attributes, and random outfielder atts.
dKeeper = 1;
}
//my clever way of assigning a player position to the players.
int def = dTackle + dMarking;
int mid = mCrossing + mPlaymaking;
int fwd = fShooting + fShotPower;
if (dKeeper > 1)
{
playPos = "Goal Keeper";
}
else if (def >= fwd && def >= mid)
{
playPos = "Defender";
}
else if (mid >= fwd && mid >= def)
{
playPos = "Midfielder";
}
else if (fwd >= mid && fwd >= def)
{
playPos = "Striker";
}
else
{
//in a rare case (if ever) the logic doesnt add ut, im spawning a star player. because. im not too got at this.
playPos = "Utility Legend";
dTackle = rAG.Next(16, 21);
dMarking = rAG.Next(16, 21);
mPlaymaking = rAG.Next(16, 21);
mCrossing = rAG.Next(16, 21);
fShooting = rAG.Next(16, 21);
fShotPower = rAG.Next(16, 21);
aDribbling = rAG.Next(16, 21);
aHeading = rAG.Next(16, 21);
aPassing = rAG.Next(16, 21);
}
}
//Generates a first name
public string firstNameGenerator()
{
string returnfirstName;
string[] firstnames;
firstnames = new string[60] { "60 different strings of first names... took them out for you, becasue it looked bad in the editor." };
Random rF = new Random(Guid.NewGuid().GetHashCode());
returnfirstName = firstnames[rF.Next(0, 40)];
return returnfirstName;
}
//generates a last name
public string lastNameGenerator()
{
string returnlastName;
string[] lastnames;
lastnames = new string[60] { "60 different strings of lastnames........." };
Random rL = new Random(Guid.NewGuid().GetHashCode());
returnlastName = lastnames[rL.Next(0, 40)];
returnlastName = lastnames[rL.Next(0, 40)];
return returnlastName;
}
}
And now for my other code, you know - the part where you put everything together.
namespace FormManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
player p = new player();
p.autoGenPlayer();
textBox10.Text = p.firstName + " " + p.lastName;
textBox11.Text = p.age.ToString();
textBox1.Text = p.aPassing.ToString();
textBox2.Text = p.aDribbling.ToString();
textBox3.Text = p.aHeading.ToString();
textBox4.Text = p.dTackle.ToString();
textBox5.Text = p.dMarking.ToString();
textBox6.Text = p.fShooting.ToString();
textBox7.Text = p.fShotPower.ToString();
textBox8.Text = p.mPlaymaking.ToString();
textBox9.Text = p.mCrossing.ToString();
textBox12.Text = p.dKeeper.ToString();
textBox13.Text = p.playPos;
}
private void button2_Click(object sender, EventArgs e)
{
//number of objects to generate
int numberOfPlayersToGenerate = 10;
string[] savePlayers = new string[numberOfPlayersToGenerate];
//Generate many objects
for (int i = 0; i < numberOfPlayersToGenerate; i++)
{
player play = new player();
play.autoGenPlayer();
}
}
}
}
So its basically the for loop here that i want to use to store all the generated variables. the autoGenPlayer() method is generating alot of different values of both int and string, and i want to store them all so i can make a nice looking table of some sort to display it.
I would love any ideas on this, its making me crazy at the moment.