Help with C++ Game?

Hey, I'm making a somewhat RPG game for fun, I'm in a 10th grade CS class so don't make fun of it's horrendous code, and I am using elements that we have not learned yet in the class (Functions), so yeah, can anyone help me with this? For some reason when I choose a "move", it doesn't want to increment or decrement my x or y coordinate. (You'll see if you compile the code, respond with any questions and I'll try to respond.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <stdlib.h> 
#include <time.h>  

//uses the std for the following
using std::cout;
using std::cin;
using std::endl;

//declares character enemyOne to be used
char enemyOne()
{
     //pulls a random number from 1-10 for the x and y coordinates.
	int enemyOneX = rand() % 10 + 1;
	int enemyOneY = rand() % 10 + 1;
	//declares the enemyOne x and y coordinates.
	int enemyOneXCoord = enemyOneX;
	int enemyOneYCoord = enemyOneY;
	 
}
//declares character enemyTwo to be used.
char enemyTwo()
{
     //pulls a random number from 1-10 for the x and y coordinates.
	int enemyTwoX = rand() % 10 + 1;
	int enemyTwoY = rand() % 10 + 1;
	//declares the enemyTwo x and y coordinates.
	int enemyTwoXCoord = enemyTwoX;
	int enemyTwoYCoord = enemyTwoY;
	 
}
//declares character enemyThree to be used
char enemyThree()
{
     //pulls a random number from 1-10 for the x and y coordinates.
	int enemyThreeX = rand() % 10 + 1;
	int enemyThreeY = rand() % 10 + 1;
	 //declares the enemyTwo x and y coordinates.
	int enemyThreeXCoord = enemyThreeX;
	int enemyThreeYCoord = enemyThreeY;
	 
}
//begins main function
int main()
{
    //declares srand statement so rand can be used.
	srand (time(NULL));
	//declares the character Hero. (May not be needed)
	char Hero;
	//sets integer resume to one
	int resume=1;
	//below is the title screen.
	cout<<"              xxxxxxxxx    xxxxxxxxx    xx        xx   xxxxxx  "<<endl;
	cout<<"              x            x       x    xx        xx   x       "<<endl;
	cout<<"              x            xxxxxxxxx    xx        xx   xxxxxx  "<<endl;
	cout<<"              x            x       x      x      x     x       "<<endl;
	cout<<"              xxxxxxxxx    x       x       xxxxxx      xxxxxx  "<<endl;
	
	cout<<"\n";
	cout<<"\n";
	
	cout<<"       xxxxxxxxx    xxxxxxx      xxxxxxxxxxx     x   x   x    x         "<<endl;
	cout<<"       x            x      x     xx       xx     x   x   x    x         "<<endl;
	cout<<"       x            xxxxxxxxx    xxxxxxxxxxx     x   x   x    x         "<<endl;
	cout<<"       x            x       x    xx       xx     x   x   x    x         "<<endl;
	cout<<"       xxxxxxxxx    x       x    xx       xx     xxxxxxxxx    xxxxxxxxx "<<endl;  
	
	cout<<"\n";
	cout<<"\n";
	
	//user enters 1 to start the game, Q at any time to quit.
    cout<<"		Press 1 To Play.		Press Q to quit.			  "<<endl;
	cin>>resume;
	while(resume == 1)
	//basically, this while loop controls the game.
	{
		int move;
		int xCoord = 0;          //declares the x and y position for char Hero.
		int yCoord = 0;
		int spawn;               /*declares spawn so user can spawn the enemies and stuff, 
                                 *basically, this is what controls the players position.
                                 *and also controls where everything is spawned*/
                                 
		cout<<"Press 2 to spawn your enemies, treasure, and yourself!\n";
		cin>>spawn;
		
		/*this while loop is for controlling a turn, every time the player moves,
		*it counts as a turn. AI is supposed to move during this time too, but
		*the AI has not been added yet.
		*/
		
		while(spawn = 2)
		{
            //if statements are not breaking if one statement is false?
			int turn=0;
			while(turn < 20)
			{
			 cout<<"Turn:"<<turn<<"\n";
			 cout<<"You are a caver who is exploring the ruins! Your coords are: X:"<<xCoord<<", Y: "<<yCoord<<"\n";
             cout<<"To move, choose 8 for Up, 7 for Left, 5 for Down, and 9 For Right.\n";
			 cin>>move;
			 
			 //this if statement is for if a player enters an invalid turn direction.
			 if(move != 8 || 7 || 5 || 9)
			 {
				cout<<"You must enter a valid move. (8, 7, 5, or 9.)\n";
			 }
                     //declares the move steps.
			         else if(move = 8)
			         {
                         yCoord++;
                     }
			         else if(move = 7)
			         {
				         xCoord--;
                     }
			         else if(move = 5)
			         {
				         yCoord--;
			         }
			         else if(move = 9)
			         {
				         xCoord++;
                     }
                     
                      /*redeclares the if statement for x OR y coords being negative. may or may not 
                      *be needed.*/
                      
                      if(xCoord < 0 || yCoord < 0)
                      {
                         cout<<"You cannot move there. That is off the board.\n";
                      }
             //ends the while loop "turn"
             }
             //increments turn by 1 every time a "turn" ends.
		     turn++;
          //ends turn while loop
	      }
		
		
	system("PAUSE");
	return 0;
	//ends game while loop
  }
  //ends program
}

//no syntax errors
//only errors that occur during the while loop and if statements
//to do: ADD ENEMY AI!!!!!!!!!!!
//to do: ADD TREASURE AND GOAL
//to do: add battle "arenas and attacks and HP and more" 
Anyone? Anyone at all?
In a few places you're using the assignment operator = where I think you intend to use the equlaity operator ==.

Also -
if(move != 8 || 7 || 5 || 9)

you need to put the full comparison in each condition you check here.
e.g move != 8 || move != 7 etc


Might be more, but that's what I see right off the bat.
Last edited on
Well this didnt really fix anything, but thanks for the tip...
Well, that did need to be fixed :) What issues do you have now?
How about incrememting your integer turn only if one of your move statements is correct?

This code looks quite messy to me (maybe cauz im looking at my phone). Ill compile it tommorow evening if this still isnt solved: as tip: check out switchstatements ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (move)
{
    case '8':
        yCoord +=1;
        turn +=1;
        break;

    case  '7':
        xCoord -= 1;
        turn += 1;
        break;

//...

    default:
        std::cout << "please enter a valid number! your turn count has not increased...\n";
        break; 


Your functions at the top of the program do nothing at all. You are consistently using '=' instead of '==', which will give you weird results. You also need to reconsider your while loops - you have a lot that will be stuck forever there.
Alright, here is what I have now:
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
	

cout<<"Turn:"<<turn<<"\n";
			 cout<<"You are a caver who is exploring the ruins! Your coords are: X:"<<xCoord<<", Y: "<<yCoord<<"\n";
             cout<<"To move, choose 8 for Up, 7 for Left, 5 for Down, and 9 For Right.\n";
			 cin>>move;
			 
			 //this if for the switch statement, takes in cases for direction movement
			 switch(move)
			 {
			    case '8':
				yCoord+=1;
				turn+=1;
				break;
			    case '7':
			    	xCoord+=1;
			    	turn+=1;
			    	break;
			    case '5':
			    	yCoord+=1;
			    	turn+=1;
			    	break;
			    case '9':
				xCoord+=1;
				turn+=1;
				break;
			    default:
					cout<<"That is not a valid move."	;
					break;
			 }


That's what I have now, but the code still isn't adding or decreasing my x or y by one?
Last edited on
Also where do I place this if statement so if the player makes a move that puts one of their coordinates below zero, it tells them they cant and forbids them to move.
1
2
3
4
                     if(xCoord < 0 || yCoord < 0)
                      {
                         cout<<"You cannot move there. That is off the board.\n";
                      }
You need to move turn inside the while loop.
As it is;
1
2
3
4
5
6
7
8
turn = 0;
while (turn < 20) {
//stuff
turn is still 0
reaches end of loop
sees turn is still less than 20
keeps looping
}

ie. until (turn >= 20) the computer won't go past that last bracket at the end of the while loop.
//stuff after the loop doesn't affect the loop
Also;
1
2
3
4
5
6
7
8
9
char enemyOne()
{
     //pulls a random number from 1-10 for the x and y coordinates.
	int enemyOneX = rand() % 10 + 1;
	int enemyOneY = rand() % 10 + 1;
	//declares the enemyOne x and y coordinates.
	int enemyOneXCoord = enemyOneX;
	int enemyOneYCoord = enemyOneY;
}

This declares a function enemyOne() that takes no arguements and returns a character (ie. letter, such as 'a').
What I think you intend to do is create a struct for "enemy", then I would declare 3 "enemy", and create a function that assigns a random coordinate for the enemy (just to make it a bit tidier).
1
2
3
4
struct enemy_t { //creates type enemy_t which is composed of an x and y coord
                   int XCoord;
                   int Ycoord;
                   };

 
enemy_t enemyOne, enemyTwo, enemyThree; //declares enemies with type enemy_t 

1
2
3
4
5
6
enemy_t Random_Coord () {
enemy_t enemy;
enemy.XCoord =    //code for random number into the XCoord component of enemy
enemy.YCoord =    //code again for YCoord component
return enemy;
}

 
enemyOne = Random_Coord();                     


You might want to read up about these first, especially to see if there's a more efficient way, the code i gave are just examples, but I'm used to C, was looking into trying C++ and ended up here. I learned quite a bit just checking the commands still exist in C++.

Good luck o//
Last edited on
Yeah that really just confused me even more, is there any way you can dumb that down for me? lol
shirokumi was trying to explain you an easy way to add enemies : structures and classes. Basically, a class is a new datatype (like integer or character) where you can declare members and methods as public, private or protected. A structure is basically a class where all variables/functions are public. I personally never use structures. Classes are one reason why C++ is an OOP-language (object oriented programming) and it will give you greate benefits later on ;) For now forget it and Focus on learning the Basic data types: int, char, float, void

Here an Explanation on classes (it may only confuse you): http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm


As I said, Ill try to get this code working in the evening, when i dont have an IDE.

Greets HalfNOob
The Problem appears to be, that the program ignores your std::cin Statements in the while loops... I added some Output at this Point

1
2
3
4
5
 if (move == 8)
{
    yCoord +=1;
    std::cout << "yCoord incremented...\n"; //<-- to check if it goes until here
}


Whatsoever: there was no Output saying that the yCoord was incremented. This tells me that the program didnt reach this point. so the Problem lies within how you use std::cin. Gather some Information on how to use std::cin in a while Loop and this should be solved (you maybe Need a do-while() Loop)

Hope this helps... HalfNOob
Back with this topic, anyway using that if statement DOES move the character in the correct position, but... what if, the player moves in a position that is illegal according to the game? (x=-1 or y=-1) What would I need to do? Basically if the player moves outside the coordinate plane (20x20) they need to be told that thats incorrect, and to not count that as a turn. A break statement will not work for me for some reason...
Can anyone help?
Topic archived. No new replies allowed.