Teh Simz

This is a discussion on Teh Simz within the Game Programming forums, part of the General Programming Boards category; I am currently working on a text based sim game. Win32 Console App to be exact. Here is the code ...

  1. #1
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38

    Cool Teh Simz

    I am currently working on a text based sim game. Win32 Console App to be exact. Here is the code I have so far (written in 1 hour so it's really just a skeleton or structure for what is to come):

    Code:
    #include <iostream.h>
    #include <string.h>
    int health;
    int sleep;
    int hunger;
    int money;
    char char_name[50];
    void newgame();
    int loadgame();
    void game_initialize();
    int exercise();
    int sleeping();
    int eat();
    int work();
    void stats();
    int main()
    {
    	int input;
    	cout<<"1. New Game"<<endl;
    	cout<<"2. Load Game"<<endl;
    	cout<<"3. Exit"<<endl;
    	cin>>input;
    	switch(input)
    	{
    	case 1: newgame();
    		break;
    	case 2: loadgame();
    		break;
    	case 3: return 0;
    		break;
    	}
    }
    void newgame()
    {
    	cout<<"Please input your Simz's name:"<<endl;
    	cin>>char_name;
    	health=50;
    	sleep=50;
    	hunger=50;
    	money=50;
    	game_initialize();
    }
    int loadgame()
    {
    	return 0;
    }
    void game_initialize()
    {
    	int choice;
    	cout<<"Please select an action:"<<endl;
    	cout<<"1. Exercise"<<endl;
    	cout<<"2. Sleep"<<endl;
    	cout<<"3. Eat"<<endl;
    	cout<<"4. Work"<<endl;
    	cout<<"5. View Stats"<<endl;
    	cout<<"6. Exit to Main Menu"<<endl;
    	cin>>choice;
    	switch(choice)
    	{
    	case 1: exercise();
    		break;
    	case 2: sleeping();
    		break;
    	case 3: eat();
    		break;
    	case 4: work();
    		break;
    	case 5: stats();
    		break;
    	case 6: main();
    		break;
    	}
    }
    int exercise()
    {
    	int minutes;
    	cout<<"How many minutes would you like to exercise for?"<<endl;
    	cout<<"(1 minute = +5 health, -2 sleep, -2 hunger, -1 dollars)"<<endl;
    	cin>>minutes;
    	health=health+(5*minutes);
    	sleep=sleep-(2*minutes);
    	hunger=hunger-(2*minutes);
    	money=money-minutes;
    	cout<<"You gained "<<5*minutes<<" health points, you lost "<<2*minutes<<" sleep points, you lost "<<2*minutes<<" hunger points, and you lost "<<minutes<<" dollars."<<endl;
    	if(health<0)
    	{
    		cout<<"You have died from sickness.";
    		return 0;
    	}
    	else if(sleep<0)
    	{
    		cout<<"You have died from sleep deprivation.";
    		return 0;
    	}
    	else if(hunger<0)
    	{
    		cout<<"You have died from starvation.";
    		return 0;
    	}
    	else if(money<0)
    	{
    		cout<<"You went broke!";
    		return 0;
    	}
    	game_initialize();
    }
    int sleeping()
    {
    	int hours;
    	cout<<"For how many hours would you like to sleep?"<<endl;
    	cout<<"(1 hour = +1 health, +5 sleep, -2 hunger)"<<endl;
    	cin>>hours;
    	health=health+hours;
    	sleep=sleep+(5*hours);
    	hunger=hunger-(2*hours);
    	cout<<"You gained "<<hours<<" health points, you gained "<<5*hours<<" sleep points, and lost"<<2*hours<<" hunger points."<<endl;
    	if(health<0)
    	{
    		cout<<"You have died from sickness.";
    		return 0;
    	}
    	else if(sleep<0)
    	{
    		cout<<"You have died from sleep deprivation.";
    		return 0;
    	}
    	else if(hunger<0)
    	{
    		cout<<"You have died from starvation.";
    		return 0;
    	}
    	else if(money<0)
    	{
    		cout<<"You went broke!";
    		return 0;
    	}
    	game_initialize();
    }
    int eat()
    {
    	int lbs;
    	cout<<"How many lbs. of food do you want to eat?"<<endl;
    	cout<<"(1 lb. = +1 health, -1 sleep, +5 hunger, -10 dollars)"<<endl;
    	cin>>lbs;
    	health=health+lbs;
    	sleep=sleep-lbs;
    	hunger=hunger+(5*lbs);
    	money=money-(10*lbs);
    	cout<<"You gained "<<lbs<<" health points, you lost "<<lbs<<" sleep points, you gained "<<5*lbs<<" hunger points, and you lost "<<10*lbs<<" dollars."<<endl;
    	if(health<0)
    	{
    		cout<<"You have died from sickness.";
    		return 0;
    	}
    	else if(sleep<0)
    	{
    		cout<<"You have died from sleep deprivation.";
    		return 0;
    	}
    	else if(hunger<0)
    	{
    		cout<<"You have died from starvation.";
    		return 0;
    	}
    	else if(money<0)
    	{
    		cout<<"You went broke!";
    		return 0;
    	}
    	game_initialize();
    }
    int work()
    {
    	int work_hours;
    	cout<<"How many hours would you like to work?"<<endl;
    	cout<<"(1 hour = -2 health, -2 sleep, -2 hunger, +10 dollars)"<<endl;
    	cin>>work_hours;
    	health=health-(2*work_hours);
    	sleep=sleep-(2*work_hours);
    	hunger=hunger-(2*work_hours);
    	money=money+(10*work_hours);
    	cout<<"You lost "<<2*work_hours<<" health points, you lost "<<2*work_hours<<" sleep points, you lost "<<2*work_hours<<" hunger points, and you gained "<<10*work_hours<<" dollars."<<endl;
    	if(health<0)
    	{
    		cout<<"You have died from sickness.";
    		return 0;
    	}
    	else if(sleep<0)
    	{
    		cout<<"You have died from sleep deprivation.";
    		return 0;
    	}
    	else if(hunger<0)
    	{
    		cout<<"You have died from starvation.";
    		return 0;
    	}
    	else if(money<0)
    	{
    		cout<<"You went broke!";
    		return 0;
    	}
    	game_initialize();
    }
    void stats()
    {
    	cout<<char_name<<" STATS:"<<endl;
    	cout<<"Health: "<<health<<endl;
    	cout<<"Sleep: "<<sleep<<endl;
    	cout<<"Hunger: "<<hunger<<endl;
    	cout<<"Dollars: "<<money<<endl;
    	game_initialize();
    }
    I am planing on upgrading the program to have job classes, job upgradablility, personal item inventory, housing, friend NPC's, and go knows what else. Stay tuned! If you have any comments or suggestions please post em!

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    DUDE, why the hell are you posting this here?...it belongs on the game board.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    Sorry!!! I thought it was okay because I saw the Game Developers United thread on this forum, and I thought when it said projects it meant any kind of project. I didn't mean to.
    Last edited by CodeNinja; 07-14-2004 at 09:45 PM.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    This forum is mainly for recruiting people for a project, not to get help a particular program.

  5. #5
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    well, i'm not asking for help, and I did ask for comments and suggestions, so it could have been worse...

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Comments and suggestions fall under help

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Moved from Recruitment board.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I think I see a stack overflow on the horizon.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Just a matter of preference but I don't like it when functions are declared like so:

    int foo();

    I much prefer:

    int foo(void);

    And while it is not required....it just looks better to me.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    I know this is over a week old but cool game! it's addictave lol, I just compiled it to see it, basic but fun

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You'll probably want to take the game_initialize call out of all of your character manipulation functions and put a loop in game_initialize(), similiar to this:

    Code:
    void game_initialize()
    {
    	int choice = 0;
    	while (choice != 6)
    	{
    		cout<<"Please select an action:"<<endl;
    		cout<<"1. Exercise"<<endl;
    		cout<<"2. Sleep"<<endl;
    		cout<<"3. Eat"<<endl;
    		cout<<"4. Work"<<endl;
    		cout<<"5. View Stats"<<endl;
    		cout<<"6. Exit to Main Menu"<<endl;
    		cin>>choice;
    		switch(choice)
    		{
    		case 1: exercise();
    			break;
    		case 2: sleeping();
    			break;
    		case 3: eat();
    			break;
    		case 4: work();
    			break;
    		case 5: stats();
    			break;
    		case 6: break;
    		}
    	}
    }
    You'll probably want to do something similar in main(). adrianxw is right; play this game too long and you'll get a stack overflow.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45

    :o

    Simple, but nonetheless fun. Tryin to touch up a bit on it

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    lol i like how all of the actions are mostly positive...until you get to work..haha you even lose health.

    ...so true..so realistic


  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    i get this in BCB6
    Code:
    [C++ Error] Unit1.cpp(70): E2120 Cannot call 'main' from within the program
    Code:
    switch(choice)
     	{
     	case 1: exercise();
     		break;
     	case 2: sleeping();
     		break;
     	case 3: eat();
     		break;
     	case 4: work();
     		break;
     	case 5: stats();
     		break;
     	case 6: main(); //<---here
     		break;
     	}

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Quote Originally Posted by sand_man
    i get this in BCB6
    Code:
    [C++ Error] Unit1.cpp(70): E2120 Cannot call 'main' from within the program
    Code:
    switch(choice)
     	{
     	case 1: exercise();
     		break;
     	case 2: sleeping();
     		break;
     	case 3: eat();
     		break;
     	case 4: work();
     		break;
     	case 5: stats();
     		break;
     	case 6: main(); //<---here
     		break;
     	}
    C++ does not allow main to be called recursively. If you want the code to compile and run then you have the option of replacing calls to cin and cout with printf and scanf (not to mention replacing iostream.h with stdio.h) and compiling as C, or you will need to modify the structure of the program so it does not try to call main recursively. Both options consist of sweeping changes to the program, so I leave the decision to you. As a final option you can use an older C++ compiler that allows both pre-standard headers and recursive calls to main.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. oh teh noes; I's be goin' away for a while!
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 08-02-2006, 05:50 PM
  2. they aren't teh
    By neandrake in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 11-21-2004, 09:42 AM
  3. I R not teh l337
    By TravisS in forum Tech Board
    Replies: 6
    Last Post: 03-21-2003, 05:29 PM
  4. Teh Book
    By NewBoy in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21