C Programming is one of the most popular programming languages globally. Despite its popularity, many students struggle completing assignments in this language. With most students struggling with the simple codes in C, they find it almost impossible to crack the harder ones, such as 2D movement. 2D movement codes are important in robots, and anyone willing to explore the world of robotics must have the required knowledge in this field. There are many reasons why students struggle to find a path using 2D movement in C. Some of them include;
- Compiler error messages – In 2D movement, most students struggle with the compiler error messages. This might look like a small thing, but most students are not familiar with how the formats are strict. They, therefore, face many complaints generated by the compiler.
- Designing the program – With most students being beginners in programming, design is a real issue. They struggle with putting most codes together, which is a real issue with complex tasks such as 2D movement.
- Debugging – Debugging is a very important skill every student needs to master. Debugging gives students a hard time because it is frustrating and tedious.
Below is an assignment sample on 2D movement in C with a well-crafted solution. Please go through it and reach out for any questions or assistance.
Assignment Overview
In this assignment you are expected to come up with a C program that can run a robot through a marsh containing obstacles, bogs, and dry ground. Through this program the robot is expected to run through some weird logic and will not have a vision system. In each step the robot makes, there is a 50% chance that it will step forward one step, 10% chance that it will step backward, 20% chance of stepping to the left, and 20% chance of stepping to the right. The robot should be able to pass safely on the ground. The robot will remain stationary if it attempts to pass through an obstacle, and if it passes across a boggy patch, it will be bogged leading to the abortion of the mission. When bogged, it will emit a distress signal and show its current position.
Through this program, you will be able to read the map of the location of the robot. This map will be divided into squares, with each square corresponding to the steps of the robot. The robot only moves along the squares on the same rows or columns. This robot will not have access to this file and will start the operation at a position that you specify on a specified marsh row.
Through the program, you should be able to stimulate the robot’s movement through the marsh until it meets one of the given instructions. The mission will be successful if the robot reaches the dry ground. Ensure that this robot will not move off the marsh in your program. If the robot attempts to move off the marsh, the program should make it stationary.
Solution
#include
#include
#include
#include
#define MARSH_SIZE 10
#define LINE_SIZE 256
void display_details() {
printf("%-9s: %s\n", "File", "yourEmailId_robot.c");
printf("%-9s: %s\n", "Author", "Your Name");
printf("%-9s: %s\n", "Stud ID", "Your Stud ID");
printf("%-9s: %s\n", "Email ID", "Your Email ID");
printf("%s\n", "This is my own work as defined by the University's Academic Misconduct Policy.");
}char get_step_choice() {
int goodInput = 0;
char line[256];
while(goodInput == 0) {
char c;
printf("Take another step [y|n]? ");
scanf("%s", line);
if (strlen(line) > 1 || (line[0] != 'y' && line[0] != 'n')) {
printf("Please enter either y or n.\n\n");
}
else {
return line[0];
}
}
}
void read_marsh(char marsh[][MARSH_SIZE] ) {
char c;
FILE *f = fopen("marsh.txt", "r");
int i, j;
if (!f) {
printf("Can not open marsh file");
return;
}
for(i = 0; i < MARSH_SIZE; i++) {
for (j = 0; j < MARSH_SIZE; j++) {
fscanf(f, "%c", &c);
marsh[i][j] = c;
}
fscanf(f, "%c", &c);
fscanf(f, "%c", &c);
}
fclose(f);
}
void display_marsh(char marsh[][MARSH_SIZE], int x, int y, int steps) {
int i, j;
printf("-- %d steps taken -- position [%d, %d] --\n\n", steps, x, y);
printf(" ");
for(i = 0; i= MARSH_SIZE) {
return 1;
}
if (marsh[*x+1][*y] == 'o') {
return 0;
}
*x = *x + 1;
return marsh[*x][*y] == 'b' ? -1 : 1;
}
else if (dir == 'b') {
if (*x-1 < 0) {
return 1;
}
if (marsh[*x-1][*y] == 'o') {
return 0;
}
*x = *x - 1;
return marsh[*x][*y] == 'b' ? -1 : 1;
}
else if (dir == 'l') {
if (*y-1 < 0) {
return 1;
}
if (marsh[*x][*y-1] == 'o') {
return 0;
}
*y = *y - 1;
return marsh[*x][*y] == 'b' ? -1 : 1;
}
else {
if (*y+1 >= MARSH_SIZE) {
return 1;
}
if (marsh[*x][*y+1] == 'o') {
return 0;
}
*y = *y + 1;
return marsh[*x][*y] == 'b' ? -1 : 1;
}
}
int main(void) {
char marsh[MARSH_SIZE][MARSH_SIZE];
int x = 0;
int y = 0;
int bogged = 0;
int steps = 0;
char choice = 'y';
char dir = ' ';
int res;
display_details();
printf("\n\n");
printf("Please, enter robot starting position [0-9]: ");
scanf("%d", &y);
printf("\n");
read_marsh(marsh);
display_marsh(marsh, x, y, steps);
printf("\n");
choice = get_step_choice();
while (choice == 'y' && (x < MARSH_SIZE - 1 && bogged == 0)) {
steps++;
dir = get_direction();
res = move_robot(marsh, dir, &x, &y);
display_marsh(marsh, x, y, steps);
if(dir == 'f') {
printf("-- Moving forward\n\n");
}
else if (dir == 'b') {
printf("-- Moving backward\n\n");
}
else if (dir == 'l') {
printf("-- Moving left\n\n");
}
else {
printf("-- Moving right\n\n");
}
if (res == -1) {
printf("-- Help! Stuck in a bog [%d, %d]\n", x, y);
bogged = 1;
}
if (res == 0) {
printf("-- Doh! Hit an obstacle\n");
}
choice = get_step_choice();
}
if (choice == 'n' || bogged == 1) {
printf("-- Mission failed at %d steps - \n", steps);
}
else {
printf("-- Success in %d steps --\n", steps);
}
return 0;
}