Hammurabi Victory

Write your question here.

Basically I need help adding in a victory (and loss when bushels run out) condition. Where do I add the necessary loops? (For the victory too)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include <iostream>		
#include <Windows.h>	  
#include <cstdlib>		
#include <time.h>		

using namespace std;


class Hammurabi
{
public:
	
	void displayMessage(int year, int starved, int immigrants, int population, int land, int harvest, int rats, int storage, int trade)
	{
		cout << "Hammurabi: I beg to report to you that in Year " << year << endl << endl;
		cout << starved << " people starved;" << endl;
		cout << immigrants << " immigrants came to the city" << endl;
		cout << "The city population is " << population << endl;
		cout << "The city now owns " << land << " acres" << endl;
		cout << "You harvested " << harvest << " bushels per acre;" << endl;
		cout << "Rats ate " << rats << " bushels;" << endl;
		cout << "You now have " << storage << " bushels in storage;" << endl;
		cout << "Land is trading at " << trade << " bushels per acre" << endl;
		cout << endl;
	}//end function displayMessage
};//end class Hammurabi

//function main begins program execution
int main()
{

		//variables to store the values
		int year = 0;
	int starved = 0;							//people who starved, population loss
	const int immigrants = 5;					//people who came to the city, population gain
	int population = 100;
	int land = 1000;							//amount of land, acres owned by the city
	const int harvest = 3;						//amount of bushels harvested per acre planted
	const int rats = 10;						//amount of bushels destroyed by rats
	int storage = 2500;							//amount of bushels in storage
	int trade = 15;								//price land is trading, how many bushels per acre

	while (year <= 9 && population > 0) {
		year = year + 1;

		srand((unsigned)time(NULL));
		//trade = 15 + (rand() % 5) + 1;

		Hammurabi myHammurabi; //create a Hammurabi object named my Hammurabi

		//call my Hammurabi displayMessage function 
		//and pass values as an argument
		myHammurabi.displayMessage(year, starved, immigrants, population, land, harvest, rats, storage, trade);

		int buy;									//amount of acres to buy
		int sell;									//amount of acres to sell
		int food;									//amount of bushels to feed the population
		int plant;									//amount of acres to plant with bushels

		cout << "How many acres of land do you want to buy? " << endl;			//amount of bushels to to trade for land				
		cin >> buy;
		land += buy;							//assignment by sum and difference, (land = land + buy)
		storage -= buy * trade;

		cout << "How many acres of land do you want to sell? " << endl;
		cin >> sell;
		land -= sell;
		storage += sell * trade;
		cout << "How many bushels do you want to feed to the people? (each needs 20) " << endl;
		cin >> food;
		storage -= food;
		cout << "How many acres do you want to plant with seed? (each acre takes one bushel) " << endl;
		cin >> plant;
		storage -= plant;

		cout << endl;

		population += immigrants;
		storage -= rats;
		storage = storage + (harvest * plant);

		
	}//end main

	system("pause");
	return 0;
}
The logical place to put your checks for win/lose is at line 82.

A few other comments:

Line 47: NEVER call srand() inside a loop. srand() sets the RNG back to the starting value if called within the same second. You should call srand() only ONCE at the beginning of the program (line 43).

Line 53: You comment is incorrect. Your object is called myHammurabi.

Line 14: The arguments to displayMessage are really attributes of your game. You should consider making them member variables of your class.

Lines 60-75: You have no checks for valid input. i.e one can sell more land than one has.

Line 50: You create a new Hammurabi instance each time through the loop. Right now, that not a problem since your Hammurabi object has no state information. If you want your Hammurabi instance to represent the state of your game, then you should move it to line 43.

> one can sell more land than one has.
as in real life
Thanks Abstraction, I made the necessary corrections. But I'm still trying to figure out what kind of loop r statement to use for the victory, only just picking C++ back up again after taking a bit of a break from it. Could I maybe have a hint? haha thanks again.
Last edited on
I'm still trying to figure out what kind of loop to use

No need for a loop. You just need a few simple if statements. The key is you want to break out of the loop if you have a win or loss conditions. You haven't stated what those conditions are, so I can only guess.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// After line 82
  if (population <= 0)
  {  cout << "Your civilization has died out" << endl;
      break;  // Break out of the main loop
  }
  if (storage <= 0)
  {  cout << "You have no food to feed your people" << endl;
      cout << "Your people revolt and kill you" << endl;
      break;  // break out of the main loop
  }
// After line 84
  if (year == 9 && population > 0)
  {  cout << "Your civilization has survived" << endl;
  }

Last edited on
OH OF COURSE IT WOULD BE AN IF STATEMENT! Thanks man, you're a life saver haha. I really appreciate the help. Though one final thing, I don't think any people starve.
Last edited on
Topic archived. No new replies allowed.