Page 1 of 1

It's a text-based game written in 'C' language. The user is marked with "#". If the player finds "G", the game will end.

Posted: Sat Nov 27, 2021 2:26 pm
by answerhappygod
It's a text-based game written in 'C' language.
The user is marked with "#". If the player finds "G", the
game will end.
Monster "M" interferes with finding "G" in the middle.
The user moves up, down, left and right using the keys 'w', 's',
'a', and 'd'.
When I run my code, Monsters 1 and 2 sometimes move one space or
several spaces.
Please modify the code so that it moves only one space at a
time.
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<conio.h>


#include<Windows.h>


#define SIZE 10
int arr[SIZE][SIZE];


int p1 = 0, p2 = 0;

// Player's starting position.
int m1 = 3, m2 = 3, M1 = 5, M2 = 5;
// Monster's starting position.
int g1 = 9, g2 = 9;

// Gold position.
void set_arr() {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE;
j++) {
arr[j]
= 0;
}
}
arr[p1][p2] = 1;

// Determine the output by connecting it to
the print() function.
arr[g1][g2] = 100;
arr[m1][m2] = 2;
arr[M1][M2] = 2;
}
void print() {

// A function that outputs on
the screen.
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE;
j++) {

if
(arr[j] == 1) printf("#");
else if
(arr[j] == 2) printf("M");
else if
(arr[j] == 100) printf("G");
else
printf(".");
}
printf("\n");
}
}
void moveMon() {
srand((unsigned)time(NULL));
while (1) {
int move1 = rand() % 5;
int move2 = rand() % 5;
switch (move1) {

// Move Monster 1.
case 0: if (m1 > 0)
// up

m1--;
break;
case 1:if (m1 < SIZE -
1) // down

m1++;
break;
case 2: if (m2 > 0)
//
left

m2--;
break;
case 3:if (m2 < SIZE -
1) // right

m2++;
break;
case 4: break;
}
switch (move2) {

// Move Monster 2.
case 0: if (M1 > 0)
// up

M1--;
break;
case 1:if (M1 < SIZE -
1) // down

M1++;
break;
case 2: if (M2 > 0)
//
left

M2--;
break;
case 3:if (M2 < SIZE -
1) // right

M2++;
break;
case 4: break;
}
if (m1 != p1 && M1
!= p1 && m2 != p2 && M2 != p2 && m1 != M1
&& m2 != M2)

break;
else if ((m1 == p1 &&
m2 == p2) || (M1 == p1 && M2 == p2))

break;
}
}
int main() {
char ch;
set_arr();


print();
while (1) {




if (p1 == g1 && p2 ==
g2) {

// If the player
finds gold, it's over. Wait for 3 seconds.

printf("\nCLEAR!\n"); Sleep(3000); break;
}
else if ((m1 == p1 &&
m2 == p2) || (M1 == p1 && M2 == p2)) {
// If the player meets a monster, it's
over. Wait for 3 seconds.

printf("\nFAIL!\n"); Sleep(3000); break;
}
ch = _getch();
switch (ch) {



// Player, move up, down, left and right.
case 'w':
//up
if (p1 ==
0 || p1 == m1 || p1 == M1) break;

arr[p1][p2] = 0;

p1--;
break;
case 's':
//down
if (p1 ==
9 || p1 == m1 || p1 == M1) break;

arr[p1][p2] = 0;

p1++;
break;
case 'a':
//left
if (p2 ==
0 || p2 == m2 || p2 == M2) break;

arr[p1][p2] = 0;

p2--;
break;
case 'd':
//right
if (p2 ==
9 || p2 == m2 || p2 == M2) break;

arr[p1][p2] = 0;

p2++;

break;
}
arr[p1][p2] = 1;
// Player, show a
new location.
arr[m1][m2] = 0;
// Delete the
previous location mark of Monster.
arr[M1][M2] = 0;
arr[g1][g2] = 0;
moveMon();
//
Move a monster.
arr[m1][m2] = 2;
// Show a monster's
new location.
arr[M1][M2] = 2;
arr[g1][g2] = 100;
system("cls");
// Erase
the previous screen.
print();
//
Print it out again on the screen.
}
return 0;
}