Loops for game: help?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Loops for game: help?

  1. #1
    Join Date
    Sep 2010
    Posts
    9

    Question Loops for game: help?

    Hello, today I was working on a basic text 'game' (if you can call it a game at this point) to practice some basic techniques with loops, strings, arrays, etc., and what I would like to happen is that every time the user puts in input, it activates something (like 'move north' moves the player north (duh) or 'take note' picks up a note off the ground and prints the text to the console). I know that there should be some way to do this with loops, but at my skill level I'm not exactly a pro with loops, so if possible could somebody tell me which loop would be best for my explained situation and why? Thanks in advance guys.

    (PS I tried a 'while' loop and it didn't exactly work out too well, will post code with the while loop if someone asks to see it)

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: Loops for game: help?

    I think your outline would be something like this.
    Code:
    // initialization
    while (!GameOver())
    {
        // get user input
        // handle user input
        // update anything that needs updating
    }
    Why didn't your loop "work out"?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Sep 2010
    Posts
    9

    Re: Loops for game: help?

    Well, I finished the program with absolutely everything inside of a 'while' loop, and that ended up prompting me to enter in the string that I needed, but after I entered anything in, the program wouldn't output any text or change any variables, just prompting me to enter in the string again, then closing.

    The second time, I only put the prompting for the user to enter a string inside the while loop and put everything outside, but now if I try to open the program it closes immediately after

    Both times the compiler did not warn me about any problems with the code or anything.

    (PS: I didnt know that this section was for visual C++ only, I will post in the normal C++ area next time.)

    (PSS: How do you put the code into one of those boxes? Sorry I'm new to these forums :S)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,092

    Re: Loops for game: help?

    Post the code if you want more help

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Loops for game: help?

    Quote Originally Posted by momrocker View Post
    Well, I finished the program with absolutely everything inside of a 'while' loop, and that ended up prompting me to enter in the string that I needed, but after I entered anything in, the program wouldn't output any text or change any variables, just prompting me to enter in the string again, then closing.

    The second time, I only put the prompting for the user to enter a string inside the while loop and put everything outside, but now if I try to open the program it closes immediately after
    You shouldn't be guessing about what goes in the loop; just decide how you want the control flow to work and structure the code accordingly. If you're having trouble figuring out what's going wrong with your planned control flow, then set a debugger breakpoint, and step through the execution a line at a time.

    Both times the compiler did not warn me about any problems with the code or anything.
    The compiler is only able to warn or error for problems detected by static code analysis. Even then, it isn't doing a comprehensive analysis, just flagging a few issues it has been programmed to notice. It cannot deduce what you are trying to do and tell you whether or not it will work the way you've written it.

    (PSS: How do you put the code into one of those boxes? Sorry I'm new to these forums :S)
    [code]Code here[/code]. I used a particular trick to make that show up in plain text, but normally if you write that it will do this:
    Code:
    Code here

  6. #6
    Join Date
    Sep 2010
    Posts
    9

    Re: Loops for game: help?

    Quote Originally Posted by Lindley View Post
    You shouldn't be guessing about what goes in the loop; just decide how you want the control flow to work and structure the code accordingly.
    I said I wasnt exactly an expert with loops (aka a lighter way of saying i basically know nothing :P)
    anyways heres my code, its a bit umm.... well just look and see lol. I could have probably put all of the stuff in main in the 'while' loop in a separate function to clear things up, and i know as soon as i post this i will be able to hear every single expert C++ programmer do a /facepalm, but here it goes...
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main() {
    //declaring all the variables (theres ALOT)
    int battery = 0; //changes to 1 when acquired
    int remote = 0; //ditto
    int tv = 0; //changes to 1 when turned on
    int remote_battery = 0; //changes to 1 when you have both the remote and the battery
    int x = 0; //location in the 5x5 room. goes from -2 to 2: higher or lower shows error
    int y = 0; //same as above, but vertical
    int win_game = 0; //changes to 1 when the player wins the game. -1 means error, and 0 means your still playing.
    int g_object = 0; //ghostly object
    int key1 = 0; //if 1, will allow chest to be opened
    int chest = 0; //1 if open, 0 if closed, -1 is error.
    int	key2 = 0; //opens door
    int cabinet = 0; //1 when empty, -1 is error
    int lose_game = 0; //1 if lost
    int electric_water = 0; //1 when activated
    int teleporter = 0; //1 when working
    int error = 0; //if -1, will close application
    char input_function[100]; //recieves input to determine where you go or what you want to do
    int z = 13;
    int door = 0;
    int note = 0;
    int note_event = 0;
    
    cout<<"Hello. Welcome to Teh Room of Mystery!\n"; //Outputting text
    cout<<"You're currently standing at coordinates ("<< x <<","<< y <<")!\n"; //shows coordinates to user
    cout<<"What do you want to do?\n"; //keep in mind this line doesnt ask them for input, cin.getline does
    cout<<"Type 'Help' if this is your first time\n";
    while ( win_game == 0 ) {
    	cout<<"You're currently standing at coordinates ("<< x <<","<< y <<")!\n";
    	cin.getline ( input_function, 100 ); //asks for the user to input the function they want
    	cin.ignore();
    	if ( error == 1 )
    		return 1;
    	if ( strcmp ( input_function, "west" ) == 0 && x > -2 ) {
    		cout<<"You move west.\n";
    		x -= 1; }
    	if ( strcmp ( input_function, "east" ) == 0 && x < 2 ) {
    		cout<<"You move east.\n";
    		x += 1; }
    	if ( strcmp ( input_function, "north" ) == 0 && y < 2 ) {
    		cout<<"You move north.\n";
    		y += 1; }
    	if ( strcmp ( input_function, "north" ) == 0 && z == 3 ) {
    		cout<<"You move through the doorway into the room.\n";
    		z = 26; }
    	if ( strcmp ( input_function, "south" ) == 0 && y > -2 ) {
    		cout<<"You move south.\n";
    		y -= 1; }
    	if ( x == -2 && y == 2 ) //checking coordinates and assigning number for every room...
    		z = 1;
    	if ( x == -1 && y == 2 ) //this would have been easier using arrays, but...
    		z = 2;
    	if ( x == 0 && y == 2 ) //i cant exactly say i 100% understand arrays and pointers...
    		z = 3;
    	if ( x == 1 && y == 2 ) //so, im just using this...
    		z = 4;
    	if ( x == 2 && y == 2 ) //deal with it.
    		z = 5;
    	if ( x == -2 && y == 1 )
    		z = 6;
    	if ( x == -1 && y == 1 )
    		z = 7;
    	if ( x == 0 && y == 1 )
    		z = 8;
    	if ( x == 1 && y == 1 )
    		z = 9;
    	if ( x == 2 && y == 1 )
    		z = 10;
    	if ( x == -2 && y == 0 )
    		z = 11;
    	if ( x == -1 && y == 0 )
    		z = 12;
    	if ( x == 0 && y == 0 )
    		z = 13;
    	if ( x == 1 && y == 0 )
    		z = 14;
    	if ( x == 2 && y == 0 )
    		z = 15;
    	if ( x == -2 && y == -1 )
    		z = 16;
    	if ( x == -1 && y == -1 )
    		z = 17;
    	if ( x == 0 && y == -1 )
    		z = 18;
    	if ( x == 1 && y == -1)
    		z = 19;
    	if ( x == 2 && y == -1 )
    		z = 20;
    	if ( x == -2 && y == -2 )
    		z = 21;
    	if ( x == -1 && y == -2 )
    		z = 22;
    	if ( x == 0 && y == -2 )
    		z = 23;
    	if ( x == -1 && y == -2 )
    		z = 24;
    	if ( x == -2 && y == -2 )
    		z = 25;
    	if ( strcmp ( input_function, "look" ) == 0 || strcmp ( input_function, "examine" ) == 0 || strcmp ( input_function, "search" ) == 0 ) {
    		switch ( z ) { //if one of the three things above is entered, starts this switch case
    			case 1: //if at coordinates (-2, 2), this will happen
    				cout<<"You are at the NW corner of the room.\n";
    				cout<<"You see a TV here.\n";
    				if ( remote == 0 ) {
    					cout<<"You see a remote here.\n";
    					cin.getline ( input_function, 100 );
    					cin.ignore();
    					if ( strcmp ( input_function, "take remote" ) == 0 ) {
    						cout<<"You put the remote in your backpack.\n";
    						remote = 1; }
    				}
    				if ( remote == 1 && battery == 1 ) {
    					cout<<"You put the battery in the remote.\n";
    					cout<<"You recieve: remote (with battery).\n";
    					remote = 0;
    					battery = 0;
    					remote_battery = 1; }
    				break;
    			case 2://so on and so forth
    				cout<<"You see a TV set in the area to the west.\n"; 
    				cout<<"There is a door on the wall in the clearing to the east.\n";
    				break;
    			case 3:
    				cout<<"There is a doorway in the wall.\n";
    				if ( door == 0 )
    					cout<<"The door is closed.\n";
    				if ( door == 1 )
    					cout<<"The door is open.\n";
    				cin.getline ( input_function, 100 );
    				cin.ignore();
    				if ( key2 == 0 && strcmp ( input_function, "open door" ) == 0 && door == 0 ) {
    					cout<<"The door is locked.\n"; }
    				if ( key2 == 1 && strcmp ( input_function, "open door" ) == 0 && door == 0 ) {
    					cout<<"You place the key in the door. The key breaks off.\n";
    					cout<<"Surprisingly, the door swings wide open.\n";
    					door = 1;
    					key2 = 0;
    				}
    				if ( key2 == 1 && door == 1 ) {
    					cout<<"error";
    					cin.get();
    					return 1; }
    				break;
    			case 4:
    				cout<<"To the west, you can see a door.\n";
    				if ( note == 0 && note_event == 0 ) {
    					cout<<"There is a note on the floor. Pick it up? (Yes or No)\n";
    					cin.getline ( input_function, 100 ); //asks for yes or no answer						
    					if ( strcmp ( input_function, "yes") == 0 ) {
    						note_event = 1;
    						note = 1;
    						cout<<"From note:\n\n";
    						cout<<"'Good morning. I hope you had a good rest.'\n";
    						cout<<"'Todays the big day. Ready?'\n";
    						cout<<"'We have big hopes for you, kid. Dont let us down.'\n\n";
    						cout<<"'PS: The code to your trunk is 3-5-1-6-7-8. Write that down'\n\n\n";
    						cout<<"The note ends there.\n"; }
    					if ( strcmp ( input_function, "no") == 0 ) { 
    						cout<<"You decide to ignore the note.\n";
    						cout<<"As you walk away, you quickly glance at the note for a second and see some numbers.\n";
    						cout<<"They read: '3-5-1-6-7-8'. You should probably write those down."; }
    				break;
    			default: //NOTE: Theres supposed to be 25 of these cases, but i stopped here so that I could test the program
    				cout<<"this is default\n";
    				break;
    			}
    		}
    	}
    }
    cin.get();
    return 0; //returns 0 to show completion
    }
    I put alot of pointless comments in here as i tried to explain to myself how it all works, so you could probably ignore those.

    NOTE: ok, i found out that i had '=' instead of '==' for the section which checks for error. but now i have the problem that i have to hit the enter button an ungodly amount of times for text to appear, and some input for the string dont work, like south.

  7. #7
    Join Date
    Sep 2010
    Posts
    9

    Cool Re: Loops for game: help?

    Ok, through about 20 minutes of looking through the program's source code, I eventually changed a few parts that were messed up (such as '=' instead of '==' or extra 'cin.ignore();'s where they dont need to be) and now it works like a charm. If anyone cares enough they can use the code I left up. Oh, and if there are any admins on this forum that could close this thread that would be greatly appreciated.

    Thanks for all the help guys!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center