Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming Language Task: Introduce movement into a 2D maze game using keys ('u', 'd', 'l', 'r') for movements up, down, left, right respectively. The

C Programming Language

Task:

Introduce movement into a 2D maze game using keys ('u', 'd', 'l', 'r') for movements up, down, left, right respectively.

The program will run using the following format:

./check_move

 is a 16-character string describing state of the maze and  is a single character (u, d, l, or r) representing the proposed move.

Current Code:

#include
#include

int main(int argc, char* argv[]) {
char* s = argv[1];

int playerpos = 0;
for (int i=0; i<16; i = i + 1) {
if (s[i] == '3') {
playerpos = i;
}
}

// look at right case (r)
if (playerpos%4 == 3 || s[playerpos + 1] == '1') {
// player is at the right border or player is running into an obstacle
printf("Player cannot move");
return 0;

} else if (s[playerpos + 1] == '0') {
// player is to the left of a blank space
printf("Player can move");
} else if (s[playerpos + 1] == '2') {
// player is to the left of an apple
printf("Player can move Got Apple!");
} else if (s[playerpos + 1] == '4') {
// player is to the left of the goal space
printf("Player can move Goal Reached!");
}

// look at left case (l)
if (playerpos%4 == 0 || s[playerpos - 1] == '1') {
// player is at the left border or player is running into an obstacle
printf("Player cannot move");
return 0;

} else if (s[playerpos - 1] == '0') {
// player is to the right of a blank space
printf("Player can move");
} else if (s[playerpos - 1] == '2') {
// player is to the right of an apple
printf("Player can move Got Apple!");
} else if (s[playerpos - 1] == '4') {
// player is to the right of the goal space
printf("Player can move Goal Reached!");
}

// look at up case (u)
if (s[playerpos - 4] == '1') {
//player is below an obstacle
printf("Player cannot move");
return 0;

} else if (s[playerpos - 4] == '0') {
// player is below a blank space
printf("Player can move");
} else if (s[playerpos - 4] == '2') {
// player is below an apple
printf("Player can move Got Apple!");
} else if (s[playerpos - 4] == '4') {
// player is below goal space
printf("Player can move Goal Reached!");
}

// look at down case (d)
if (s[playerpos + 4] == '1') {
// player is above an obstacle
printf("Player cannot move");
return 0;

} else if (s[playerpos + 4] == '0') {
// player is above a blank space
printf("Player can move");
} else if (s[playerpos + 4] == '2') {
// player is above an apple
printf("Player can move Got Apple!");
} else if (s[playerpos + 4] == '4') {
// player is above goal space
printf("Player can move Goal Reached!");
}

return 0;
}

Desired Output Examples:

./ check_move 3000100011102040 d Player cannot move ./ check_move 3000100011102040 r Player can move . / check_move 000010301 

 

./check_move 3000100011102040 d Player cannot move ./check_move 3000100011102040 r Player can move ./check_move 0000103011202040 d Player can move Got Apple! ./check_move 0000100011302040 d Player cannot move ./check_move 0000100011300040 d Player can move Goal Reached!

Step by Step Solution

3.48 Rating (155 Votes )

There are 3 Steps involved in it

Step: 1

C C implementation of the above approach include define MAX 5 using namespace std void getallpathint matrixMAXMAX int nint rowint colvector ansstring cur ifrown or coln or row 0 or col 0 or matrixrowc... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Discovering Advanced Algebra An Investigative Approach

Authors: Jerald Murdock, Ellen Kamischke, Eric Kamischke

1st edition

1559539844, 978-1604400069, 1604400064, 978-1559539845

More Books

Students also viewed these Mathematics questions

Question

Define data speculation.

Answered: 1 week ago