Question: I ' m writing a call function in a C + + program and most of it is done. Its function is to complete the
Im writing a call function in a C program and most of it is done. Its function is to complete the movement of the position of the letters in a D map matrix.
Id like to add a feature that will detect if other letters are in the way of moving letters in different directions. How can this be done?
For example, if the letter A moves three squares to the east, then the contents of the three adjacent squares on the east side of A must all be instead of other letters, otherwise return instructions that failed to move.
StatusMoveAction updateMapForMoveActionchar mapMAXROWSMAXCOLS int mapRows, int mapCols,
char letter, DirectionType direction, int moveSteps
Step : Locate the position of the letter
int currentRow currentCol ;
for int row ; row mapRows; row
for int col ; col mapCols; col
if maprowcol letter
currentRow row;
currentCol col;
break;
if currentRow break; Exit outer loop when letter is found
If the letter is not found, return invalid move
if currentRow currentCol
return STATUSACTIONMOVENOTIMPLMENTED;
Step : Calculate new position based on direction and steps
int newRow currentRow, newCol currentCol;
switch direction
case DIRECTIONNORTH:
newRow newRow moveSteps;
break;
case DIRECTIONSOUTH:
newRow newRow moveSteps;
break;
case DIRECTIONWEST:
newCol newCol moveSteps;
break;
case DIRECTIONEAST:
newCol newCol moveSteps;
break;
Step : Validate the new position
Check if the new position is within the map boundaries
if newRow newRow mapRows newCol newCol mapCols
return STATUSACTIONMOVEOUTSIDEBOUNDARY;
Check if the new position is not occupied by another letter
if mapnewRownewCol
return STATUSACTIONMOVEHITANOTHERALONGPATH;
Step : Update the map
mapcurrentRowcurrentCol; Clear the old position
mapnewRownewCol letter; Move letter to the new position
Step : Return success status
return STATUSACTIONMOVESUCCESS;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
