issues with functions

I cant figure out how to make the if statements into functions such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	if (move == 'u') {
					grid[y][x] = ' ';
					y--;

					if (grid[y][x] == '#') {
						cout << " The trap got you. GAME OVER" << endl;
						gameon = true;
					}
					if (grid[y][x] == 'E') {
						cout << "You found the exit. YOU WIN" << endl;
						gameon = true;
					}
					grid[y][x] = player;
				

i want to make the code smaller by using functions.

this is the entire project im doing..

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
153
154
155
156
157
158
159
160
#include <iostream>
#include <cmath>



using namespace std;



int main() {
	
	
	
		char move;
		bool gameon = false;
		bool validinput;
		char grid[10][10] = { { '*','*','*','*','*','*','*','*','*','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
		{ '*','*','*','*','*','*','*','*','*','*' } };


		char player = 'P', trap = '#', goal = 'E';
		int x = 1, y = 1;
		grid[y][x] = player;
		grid[1][2] = trap;
		grid[1][6] = trap;
		grid[2][6] = trap;
		grid[2][4] = trap;
		grid[4][1] = trap;
		grid[4][2] = trap;
		grid[4][5] = trap;
		grid[4][6] = trap;
		grid[5][6] = trap;
		grid[5][1] = trap;
		grid[5][2] = trap;
		grid[5][3] = trap;
		grid[6][1] = trap;
		grid[6][2] = trap;
		grid[6][5] = trap;
		grid[7][2] = trap;
		grid[8][8] = goal;
		grid[7][8] = trap;





		do {







			for (int row = 0; row < 10; row++) {

				for (int column = 0; column < 10; column++) {
					cout << grid[row][column];
				}
				cout << endl;
			}



			do {

				validinput = true;

				cout << "move" << endl;


				cin >> move;

				if (move == 'r') {
					grid[y][x] = ' ';//Removes From grid location
					x++;// Shifts it to the right by 1

					if (grid[y][x] == '#') {//Checks if there is a trap in the new location you chose
						cout << "The trap got you. GAME OVER!!" << endl;
						gameon = true;
					}
					if (grid[y][x] == 'E') {//Checks if there is a goal in the new location you chose
						cout << "You found the exit. YOU WIN" << endl;
						gameon = true;
					}
					grid[y][x] = player;
				}



				if (move == 'd') {
					grid[y][x] = ' ';
					y++;// Shifts it down by 1

					if (grid[y][x] == '#') {
						cout << "The trap got you. GAME OVER." << endl;
						gameon = true;
					}
					if (grid[y][x] == 'E') {
						cout << " You found the exit. YOU WIN" << endl;
						gameon = true;
					}
					grid[y][x] = player;
				}



				if (move == 'l') {
					grid[y][x] = ' ';
					x--;// Shifts it left by 1

					if (grid[y][x] == '#') {
						cout << " The trap got you. GAME OVER" << endl;
						gameon = true;
					}
					if (grid[y][x] == 'E') {
						cout << "You found the exit. YOU WIN" << endl;
						gameon = true;
					}
					grid[y][x] = player;
				}



				if (move == 'u') {
					grid[y][x] = ' ';
					y--;

					if (grid[y][x] == '#') {
						cout << " The trap got you. GAME OVER" << endl;
						gameon = true;
					}
					if (grid[y][x] == 'E') {
						cout << "You found the exit. YOU WIN" << endl;
						gameon = true;
					}
					grid[y][x] = player;
				}
			} while (!validinput);

		} while (!gameon);
	
	

	system("pause");
	return 0;
}




Last edited on
Some can be easily made into boolean functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool is_Trap(const char & cell)
{
   if(cell=='#') 
   { std::cout<<" The trap got you. GAME OVER\n" ;
      return true;
   }
   else return false;
}

bool is_Exit(const char & cell)
{
   if(cell=='E') 
   { std::cout<<"You found the exit. YOU WIN\n";
     return true;
   }
   else return false;
}


Then just combine those functions to make others with wider usage

1
2
3
4
5
void move_u(int & y, bool & game_state)
{
   --y;
   if(is_Exit() || is_Trap()) game_state=true;
}
Topic archived. No new replies allowed.