using arrays/loops

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

rowlandsfc

Guest
im creating the game hammurabi and im stuck ive got the base game all running with the codes for inputs and everything working but im at the stage where i now need to create the results after 10 years and im having trouble storing data after each year for example i need to store the the amount of people starved for every year and then work out the average and display, my code so far is as follows


// 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 starved = 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");
}
else
{
plague = 0;

}




// working out the amount of people starved
if (int.Parse(txtFeeding.Text) < (22 * population / 2))
{
MessageBox.Show("You starved " + population);

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

}

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

if (rats == 5)
{
ratsAte = bushels * 10 / 100;
lblRats.Text = "Rats ate " + ratsAte + " bushels.";

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

}

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

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

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

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


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

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

bushels = bushels + bushelsHarvested - ratsAte - int.Parse(txtFeeding.Text) - int.Parse(txtSeed.Text);




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

// changing the lables to show the new information remaining labels
lblBushels.Text = "You now have " + bushels;
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 + ", " + starved + " people starved.";
lblRemaining.Text = bushels + " Remaining";



}

private void txtBuySell_Leave(object sender, EventArgs e)
{
// 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;
}

bushels = bushels + bushelsTrading;

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

lblAcres.Text = "The city now owns " + acres + " acres.";

lblRemaining.Text = bushels + " Bushels Remaining";
}

private void txtFeeding_Leave(object sender, EventArgs e)
{
// warning user they dont have enough bushels
if (int.Parse(txtFeeding.Text) > bushels)
{
MessageBox.Show("You do not have enough bushels in store");
return;
}

lblRemaining.Text = (bushels - int.Parse(txtFeeding.Text)) + " Bushels Remaining";
}

private void txtSeed_Leave(object sender, EventArgs e)
{
// 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;
}
if (int.Parse(txtSeed.Text) > acres)
{
MessageBox.Show("you can only plant a max of 1 bushel per acre");
return;
}


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

}

Continue reading...
 

Similar threads

Back
Top