using arrays

  • Thread starter Thread starter rowlandsfc
  • Start date Start date
R

rowlandsfc

Guest
hey im writing up the code for my hammurabi game for my college work and im having some issues with using arrays, the game isset to play over 10 years where the year moves forward by 1 every time you click the button which works out different things based on the data entered by the user



private void Form1_Load(object sender, EventArgs e)
{

}
// variables
int year = 1;
int arrivals = 0;
int population = 100;
int acres = 1000;
int harvested = 0;
int rats = 5;
int trading = 18;
int ratsAte = 0;
int bushels = 2800;
int bushelsHarvested = 0;
int bushelsTrading = 0;
int plague = 0;
int died = 0;
double averageStarved = 0;
double averageAcres = 0;
double totalStarved = 0;


private void btnAllocate_Click(object sender, EventArgs e)

{

// moves the game forward a year every time the allocate button is clicked
year = year + 1;

// random amount of people come to the city
Random random = new Random();
arrivals = random.Next(1, 16);

// plague that halfs the population
Random random4 = new Random();
plague = random.Next(1, 21);
if (plague == 1)
{
population = population / 2;
MessageBox.Show("You were hit by a plague, half your population died", "Plague", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
plague = 0;

}

// working out the amount of people starved
if (int.Parse(txtFeeding.Text) < (22 * population / 2))
{
population = population / 2;
MessageBox.Show(population + " You starved Half your population", "Starved", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
if (int.Parse(txtFeeding.Text) >= (22 * population / 2) && int.Parse(txtFeeding.Text) < 20 * population)
{
died = ((20 * population) - (int.Parse(txtFeeding.Text))) / 20;

}
else
{
died = 0;
}


// new population total
population = population + arrivals - plague - died;

// randomised number for trading
Random random3 = new Random();
trading = random.Next(17, 27);



//randomised number of bushels harvested per acre
Random random1 = new Random();
harvested = random.Next(1, 6);

// possibilty of rats
Random random2 = new Random();
rats = random.Next(1, 11);



// calculates the amount of bushels harvested and stores it in a variable

bushelsHarvested = harvested * int.Parse(txtSeed.Text);

// adds the bushels harvested to the users bushels
bushels = bushels + bushelsHarvested;

// works out how much rats have eaten
if (rats == 5)
{
ratsAte = bushels * 10 / 100;
lblRats.Text = "Rats ate " + ratsAte + " bushels.";
MessageBox.Show("Rats ate " + ratsAte + " bushels");

}
else
{
ratsAte = 0;
lblRats.Text = "Rats ate 0 bushels";

}

// takes the amount of bushels that the rats ate away fromthe remaining bushels
bushels = bushels - ratsAte;

// adds the amount died from starvation each year to a variable
averageStarved = died + averageStarved;
totalStarved = averageStarved;

// end of year evaluation
if (year == 11)
{
pnlEvaluation.Visible = true;
pnlResults.Visible = true;
lblResultsTitle.Visible = true;
averageAcres = acres / population;
averageStarved = averageStarved / 10;
lblAverageAcres.Text = "You started with an average of 10 acres per person and ";
lblAverageAcres2.Text = "ended with an average of " + averageAcres + " per person.";
lblAverageStarved.Text = averageStarved + " people starved on average every year, ";
lblAverageStarved2.Text = totalStarved + " in total died during your reign.";

if (averageAcres <= 7 && averageStarved > 0)
{
lblEvaluation.Text = "Due to your reckless missmanagement of resources the people ";
LBLEvaluation2.Text = "find you a horrible and disgusting ruler and want you beheaded.";
}
if (averageAcres >= 13 && averageStarved < 1)
{
lblEvaluation.Text = "You have managed to keep every fed and happy over the years, ";
LBLEvaluation2.Text = "your people are looking forward to your next reign.";
}
if ((averageAcres > 7 && averageAcres < 13 ) && (averageStarved == 0))
{
lblEvaluation.Text = "You have had a very average reign and your people expected more, ";
LBLEvaluation2.Text = "Improvement need or yoou may be overthrown.";
}

return;
}

// resets the textboxes
txtBuySell.Text = "";
txtFeeding.Text = "";
txtSeed.Text = "";

// changing the lables to show the new information remaining labels
lblNewPeople.Text = arrivals + " people came to the city.";
lblHarvest.Text = "You harvested " + harvested + " bushels per acre.";
lblPopulation.Text = "The city population is now " + population;
lblTrading.Text = "land is trading at " + trading + " bushels per acre.";
lblYear.Text = "In Year " + year + ", " + died + " people starved.";
lblBushels.Text = "You now have " + bushels;
lblRemaining.Text = bushels + " Bushels Remaining";



}

private void txtBuySell_Leave(object sender, EventArgs e)
{
// validation, making sure user doesnt leave the textbox empty
if (string.IsNullOrWhiteSpace(txtBuySell.Text) == true)
{
MessageBox.Show("Please enter an amount of acres to sell/buy");
return;
}

// buy/sell calculation
if (int.Parse(txtBuySell.Text) < 0)
{
bushelsTrading = Math.Abs(int.Parse(txtBuySell.Text) * trading);
}
else if (int.Parse(txtBuySell.Text) >= 0)
{
bushelsTrading = -int.Parse(txtBuySell.Text) * trading;
}

// calculating the amount of acres that are left
acres = acres + int.Parse(txtBuySell.Text);

// updating the bushels amount
bushels = bushels + bushelsTrading;

// changing the labels to reflect new information
lblRemaining.Text = bushels + " Bushels Remaining";
lblAcres.Text = "The city now owns " + acres + " acres.";
lblBushels.Text = "You now have " + bushels;

}

private void txtFeeding_Leave(object sender, EventArgs e)
{
// validation, making sure user doesnt leave the textbox empty
if (string.IsNullOrWhiteSpace(txtFeeding.Text) == true)
{
MessageBox.Show("Please enter an amount of bushels to feed your peopl");
return;
}


// warning user they dont have enough bushels
if (int.Parse(txtFeeding.Text) > bushels)
{
MessageBox.Show("You do not have enough bushels in store");
return;
}

bushels = bushels - int.Parse(txtFeeding.Text);
lblRemaining.Text = bushels + " Bushels Remaining";
lblBushels.Text = "You now have " + bushels;

}

private void txtSeed_Leave(object sender, EventArgs e)
{
// validation, making sure user doesnt leave the textbox empty
if (string.IsNullOrWhiteSpace(txtSeed.Text) == true)
{
MessageBox.Show("Please enter an amount of bushels to seed");
return;
}

// tells user that they can not seed more than 10 seed per person
if (int.Parse(txtSeed.Text) > (10 * population))
{
MessageBox.Show("You can only Plant a max of 10 seeds per person");
return;
}

bushels = bushels - int.Parse(txtSeed.Text);

lblRemaining.Text = bushels + " Bushels Remaining";
lblBushels.Text = "You now have " + bushels;



}


thats my code at the moment, what im looking to do is use arrays so that i can store the amount of people that have starved each year(each button click) and be able to call back to them later so that i can work out the averages over a 10 year period

Continue reading...
 
Back
Top