Jump to content

Noob C++ help again!

KushKomputers

So i am working on a project for my begenner c++ class and i can figure out what is going on. I have a basic game going where a character moves around the screen and the collects health and hit points.

I can run through and select the game and then run through it but once I loop back around again to play again the coordinates for the game are not resetting so the game ends right away once the game starts again. 

 

to play the game use the arrow keys and - will take away 10,  * will add 10, and the only way as of right now to loose it to get score to 0.

 

here is the code

 #include <iostream>#include "windows.h"#include <string> using namespace std;   char map [21] [37] ={    "#==================================#",    "|^                                 |",    "|                    *             |",    "|                                  |",    "|                                  |",    "|                 *         -      |",    "|                                  |",    "|        -           *             |",    "|                                  |",    "|                                  |",    "|                                  |",    "|                                  |",    "|                                  |",    "|                                  |",    "|                      -           |",    "|                                  |",    "|                                  |",    "|          $                       |",    "|                                  |",    "#==================================#"  };char map2 [21] [37] ={    "#==================================#",    "|^                                 |",    "|                    *             |",    "|                                  |",    "|                                  |",    "|                 *         -      |",    "|                                  |",    "|        -           *             |",    "|                                  |",    "|                                  |",    "|                  ?               |",    "|                                  |",    "|                                  |",    "|                                  |",    "|                      -           |",    "|                                  |",    "|                                  |",    "|          $                       |",    "|                                  |",    "#==================================#"  }; bool gameRunning = true;bool levelRunning = true;//placementint x = 1;int y = 1; int level; //scoreint a = 10;string game; int main(){    while(gameRunning == true)    {        system("cls");         levelRunning = true;        int x = 1;        int y = 1;          cout << "Welcome to my game!\n";        cout << "Please select a level.\n\n";         cout << "1. Level 1\n" <<                "2. Level 2\n";         system("pause>null");         cin >> level;         if(level == 1)        {             while(levelRunning == true)            {                 system("cls");                 for(int display = 0; display < 21; display++)                    {                        cout << map[display] << endl;                    }                cout << "Score =";                cout << a <<endl;                 system("pause>null");              //Getting User Input for Movement                if(GetAsyncKeyState(VK_DOWN))                {                    //Updating coordinates                    int y2 = y + 1;                     //What happens when moved                    switch (map[y2][x])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            a=a-10;                            break;                     }                 }                 if(GetAsyncKeyState(VK_UP))                {                    int y2 = y - 1;                     switch (map[y2][x])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(GetAsyncKeyState(VK_RIGHT))                {                    int x2 = x + 1;                     switch (map [y][x2])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(GetAsyncKeyState(VK_LEFT))                {                    int x2 = x - 1;                     switch (map [y][x2])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(a <=0)                {                    system("cls");                    cout << "#==================================#\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|            GAME  OVER            |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "#==================================#\n";                    cout << "\n";                    cout << "Score = ";                    cout << a <<endl;                     levelRunning = false;                    system("pause");                  }            }        }         if(level == 2)        {            while(levelRunning == true)            {                 system("cls");                 for(int display = 0; display < 21; display++)                    {                        cout << map2[display] << endl;                    }                cout << "Score =";                cout << a <<endl;                 system("pause>null");              //Getting User Input for Movement                if(GetAsyncKeyState(VK_DOWN))                {                    //Updating coordinates                    int y2 = y + 1;                     //What happens when moved                    switch (map[y2][x])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            y++;                            map [y] [x] ='^';                            a=a-10;                            break;                     }                 }                 if(GetAsyncKeyState(VK_UP))                {                    int y2 = y - 1;                     switch (map[y2][x])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            y--;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(GetAsyncKeyState(VK_RIGHT))                {                    int x2 = x + 1;                     switch (map [y][x2])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            x++;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(GetAsyncKeyState(VK_LEFT))                {                    int x2 = x - 1;                     switch (map [y][x2])                    {                        //Normal Movement                        case ' ':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            break;                        //Adding Points                        case '*':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            a=a+10;                            break;                        //Subtracting Points                        case '-':                            map [y] [x] = ' ';                            x--;                            map [y] [x] ='^';                            a=a-10;                            break;                    }                 }                 if(a <=0)                {                    system("cls");                    cout << "#==================================#\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|            GAME  OVER            |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "|                                  |\n";                    cout << "#==================================#\n";                    cout << "\n";                    cout << "Score = ";                    cout << a <<endl;                     levelRunning = false;                    system("pause");                  }            }        }        system("pause>null");     }    return 0;}
Edited by alpenwasser
code tags

AMD FX-8350 OC to 4.5ghz      Evga gtx 660       SilverStone FT03      Samsung 840 128gb  

Link to comment
Share on other sites

Link to post
Share on other sites

That's a lot of code! Please use code brackets 

like this.

 I'll take a closer look at the code soon.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

a) use code

and b) you seem to be deep into game-logic coding, but none of the basics like functions.

Put it in a code block so its readable and we can figure something out.

Link to comment
Share on other sites

Link to post
Share on other sites

Write in Block format so we can help you. I can't even......

Link to comment
Share on other sites

Link to post
Share on other sites

Your issue is that you do not reset the arrays map or map2 when the game "resets" in your highest level loop.

Create a const version of each of the maps and a working copy that is generated from the constant one when you chose a level.

And as the others mentioned, I highly recommend that you start using functions. Stuffing 400 lines of code into main is difficult to read. 
Put the "maps" in a separate header file and include them.
Have your main loop call some kind of level function, to tidy the whole thing up. 

Edit: take advantage of c++'s object oriented nature and make a "map" object.
If you don't know what that is, don't worry about it and put it on your list of things to read about it. 

Link to comment
Share on other sites

Link to post
Share on other sites

I believe I have found your problem, but I will have to give a lecture sorry.  I will be honest, I am impressed your detail of code and not having a single function *but that is not a good thing*...functions help to organize code, and prevent having retype the exact same thing over and over again.

 

You can have your game code, but at least make a function that generates each game for you to play.  If you haven't put it in because you don't know how to, then I would seriously suggest that being your next thing you learn, as it is the most important thing missing at the moment.

 

Anyways a few extra things to talk about before actually getting to the problem *these should not be overlooked though as they are problems, just you haven't hit them yet*.  As joncppl said, you are manipulating the map file, I disagree that it is your current issue (All I can see it doing is making you unable to collect things...ie it won't spawn the next time your level appears, but you haven't seen it yet because it keeps resetting).  Also your level 2 is using the level 1's array (which is probably due to the copy pasting, another reason for functions)

The final issue non-related that I want to bring up is the following, you don't run any checks on accessing the map.  So if you forget an | in your map you will be able to get a memory violation.

 

So now to where the real problem is occurring.  The problem that you currently are facing is that the variable a is never reset, and it is the exit for your level.  You set a to 10 outside of any of the for loops

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

the game could probably be about 1/4 of the length if you used C++ features

you should decide wether to use C or C++: you're currently using C style with almost only (and few) C features

Link to comment
Share on other sites

Link to post
Share on other sites

I believe I have found your problem, but I will have to give a lecture sorry.  I will be honest, I am impressed your detail of code and not having a single function *but that is not a good thing*...functions help to organize code, and prevent having retype the exact same thing over and over again.

 

You can have your game code, but at least make a function that generates each game for you to play.  If you haven't put it in because you don't know how to, then I would seriously suggest that being your next thing you learn, as it is the most important thing missing at the moment.

 

Anyways a few extra things to talk about before actually getting to the problem *these should not be overlooked though as they are problems, just you haven't hit them yet*.  As joncppl said, you are manipulating the map file, I disagree that it is your current issue (All I can see it doing is making you unable to collect things...ie it won't spawn the next time your level appears, but you haven't seen it yet because it keeps resetting).  Also your level 2 is using the level 1's array (which is probably due to the copy pasting, another reason for functions)

The final issue non-related that I want to bring up is the following, you don't run any checks on accessing the map.  So if you forget an | in your map you will be able to get a memory violation.

 

So now to where the real problem is occurring.  The problem that you currently are facing is that the variable a is never reset, and it is the exit for your level.  You set a to 10 outside of any of the for loops

Lectures are always the best type of help..

And OP you should rename variable 'a' to something meaningful like "score".

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you guys so much, I can always find help here!

AMD FX-8350 OC to 4.5ghz      Evga gtx 660       SilverStone FT03      Samsung 840 128gb  

Link to comment
Share on other sites

Link to post
Share on other sites

[spoiler][code]Your code here[/code][/spoiler]

Please put you code in spoiler tags. I will make the page easier to read.. 

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×