forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventureGame2.c
229 lines (190 loc) · 6.86 KB
/
AdventureGame2.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# include <stdio.h>// defines scanf() and printf()
# include <math.h>//has math functions; exp(x,y)
# include <stdlib.h>//defines rand()
# include <string.h>//
# include <time.h>//defines time()
int n = 10;
char arr[10][10];
int iPlayerPosition=0, jPlayerPosition=0;
//Functions
int movePlayer(int playersMove, char arr[][10], int iPosition, int jPosition);
int moveCheck(int playersMove, char arr[][10], int iPosition, int jPosition);
int wallCheck(int playersMove, char arr[][10], int iPosition, int jPosition);
int moveCheck2(int playersMove, char arr[][10], int iPosition, int jPosition);
int main(void)
{
int seed = time(NULL);
srand(seed);// seeding random
int playersMove;
int i,j,k;
int iRandom=rand()%n + 0 , jRandom =rand()%n + 0; // iRandom and jRandom range from 0 to 9;
int TRUE;
/////////////////////******************************* Setting up the grid *********//////////////////////////////////////////////
// placing 0's into the array just to fill the gird and to know which are empty , so any array with a 0 is considered empty
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
arr[i][j] = '0';
}//*/
arr[0][0] = 'P';// starting P "the player" at the top left of the grid / board
arr[n-1][n-1] = 'T';// Placing the treasure on the bottom left of the board
for(k=0; k < n*(.5); k++)// the percentage of 'X' on the grid is 5%
{
if(arr[iRandom][jRandom] == '0')
{
arr[iRandom][jRandom] = 'X';
//printf("arr[%d][%d] = %c\n", iRandom, jRandom, arr[iRandom][jRandom]);
iRandom = rand()%n + 0; // iRandom ranges from 0 to 9
jRandom = rand()%n + 0;// jRandom ranges from 0 to 9
}
//*/
}
for(k=0; k < n*(.10); k++)// the percentage of '*' on the grid is 10%
{
if(arr[iRandom][jRandom] == '0')
{
arr[iRandom][jRandom] = '*';
iRandom = rand()%n + 0; // iRandom ranges from 0 to 9
jRandom = rand()%n + 0;// jRandom ranges from 0 to 9
}
}
//FILLING UP THE REMAINDING SPOTS
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(arr[i][j] == '0')
arr[i][j] = '.';
}//*/
///////////////////////////////////////////////////////////////////END SETTING UP THE BOARD//////////////////////////////////////////
char c;
do{
printf("Welcome to the Adventure Game 8=up,2=down,4=left,6=right\n");
printf("X-BLOCK, *-DIE, T-TREASURE, P-PERSON\n");
printf("Object of the game is to get to the treasure\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%c ", arr[i][j] );
}
printf("\n");
}
printf("Enter your move: ");
scanf("%d", &playersMove);
//printf("players move is %c\n", playersMove);
TRUE = movePlayer(playersMove, arr, iPlayerPosition, jPlayerPosition);
system("cls");
}while(TRUE != -1 &&TRUE != 2);
system("pause");
}
///////////////////////////////////////////////////////////FUNCTIONS//////////////////////////////////////////////////////////////
int movePlayer(int playersMove, char arr[][n], int iPosition, int jPosition)
{
int tempi = iPosition;
int tempj = jPosition;
int TRUE;
// printf("players move is %c\n", playersMove);
if(playersMove == 8)
{//printf("move up\n");
iPlayerPosition -= 1;
}
else if(playersMove ==2 )
{//printf("move down\n");
iPlayerPosition += 1;
}
else if(playersMove == 6)
{//printf("move right\n");
jPlayerPosition += 1;
}
else if(playersMove == 4)
{//printf("move left\n");
jPlayerPosition -= 1;
}
TRUE = moveCheck(playersMove, arr, iPlayerPosition, jPlayerPosition);
if( TRUE == 1 && wallCheck(playersMove, arr, iPlayerPosition, jPlayerPosition) == 1)
{
//swap arrays
char temp = arr[tempi][tempj];
arr[tempi][tempj] =arr[iPlayerPosition][jPlayerPosition];
arr[iPlayerPosition][jPlayerPosition] = temp;
return TRUE;
}
else
{
iPlayerPosition = iPosition;
jPlayerPosition = jPosition;
return TRUE;
}
}
int moveCheck(int playersMove, char arr[][10], int iPosition, int jPosition)
{
if(arr[iPosition][jPosition] == 'X')
{
printf("Can't move there\n");
return 0;
}
if(arr[iPosition][jPosition] == '*')
{
printf("You Lose, Thank You for Playing\n");
//system("pause");
return -1;
}
if(arr[iPosition][jPosition] == 'T')
{
printf("Congratulations you win !\n");
system("pause");
return 2;
}
return 1;
}
int wallCheck(int playersMove, char arr[][10], int iPosition, int jPosition)
{
//Handling the corners
if(iPosition<0 && jPosition<0 && (playersMove== 8 || playersMove==4))
{
printf("Can't move there\n");
return 0;
}
else if(iPosition<0 && jPosition>9 && (playersMove== 8 || playersMove==6))
{
printf("Can't move there\n");
return 0;
}
else if(iPosition>9 && jPosition<0 && (playersMove== 2 || playersMove==4))
{
printf("Can't move there\n");
return 0;
}
else if(iPosition>9 && jPosition<0 && (playersMove== 2 || playersMove==6))
{
printf("Can't move there\n");
return 0;
}
else// handling the ith rows and jth columns
{
if(iPosition < 0 && playersMove == 8)
{printf("Can't move there\n");
return 0;}
if(iPosition > 9 && playersMove == 2)
{printf("Can't move there\n");
return 0;}
if(jPosition < 0 && playersMove == 4)
{printf("Can't move there\n");
return 0;}
if(jPosition > 9 && playersMove == 6)
{printf("Can't move there\n");
return 0;}
}
// printf("iPosition = %d , jPosition = %d\n", iPosition, jPosition);
//system("pause");
return 1;
}
int moveCheck2(int playersMove, char arr[][10], int iPosition, int jPosition)
{
if(arr[iPosition][jPosition] == '*')
{
printf("You Lose, Thank You for Playing\n");
return 0;
}
return 1;
}