Hi all,
I have two piece of code, but i am not sure where did it go work, it just didn't work out the way it want to be.Can anyone help me to troubleshoot.
This is one part of my code.
Code:
#include<stdio.h>
#include<stdlib.h>
#include "navigation.h"
#include "robot.h"
// Usage: ./readMaze <mazefile.txt> <movelimit>
int main(int argc, char **argv){
FILE *f;
int w,h;
int i,j;
int x,y;
char **maze;
char *buf;
int moves=0;
int maxMoves=0;
int won=0;
char *name=robot_name();
maxMoves = atoi(argv[2]);
f=fopen(argv[1],"r");
fscanf(f,"%dx%d\n",&w,&h);
maze=(char**)malloc(sizeof(char)*h);
for (i=0;i<h;i++){
maze[i]=(char*)malloc(sizeof(char)*w);
}
buf = (char*) malloc(sizeof(char)*w);
for (i=0;i<h;i++){
fgets(buf,w,f);
for (j=0;j<w;j++){
if (buf[j]=='X'){
y=j;
x=i;
maze[i][j]='.';
}else{
maze[i][j]=buf[j];
}
}
}
fclose(f);
for (i=0;i<h;i++){
for (j=0;j<w;j++){
fprintf(stderr,"%c",maze[i][j]);
}
fprintf(stderr," - that was line %d\n",i);
}
fprintf(stderr,"$ done reading map\n");
start_at(x,y);
// assume the maze is surrounded by walls
//so we're always 1 away from the edge
while (moves < maxMoves && (!won)){
Wallinfo w;
enum Dir a;
// Determine what's blocked
fprintf(stderr,"$ -----------------------\n");
fprintf(stderr,"$ (%s) at (%d,%d)\n",name,x,y);
if (maze[y-1][x]=='#') w.N = 1;
else w.N = 0;
if (maze[y+1][x]=='#') w.S = 1;
else w.S = 0;
if (maze[y][x+1]=='#') w.E = 1;
else w.E = 0;
if (maze[y][x-1]=='#') w.W = 1;
else w.W = 0;
a = robot_move(&w);
moves++;
if (a==N){
if(maze[y-1][x]=='#'){
fprintf(stderr,"$ North is blocked!\n");
continue;
} else {
y--;
fprintf(stderr, "$ (%s) moved N\n",name);
if (maze[y][x]=='E') won=1;
continue;
}
}
if (a==S){
if (maze[y+1][x]=='#'){
fprintf(stderr,"$ South is blocked!\n");
continue;
} else {
y++;
fprintf(stderr,"$ (%s) moved S\n",name);
if (maze[y][x]=='E') won=1;
continue;
}
}
if (a==E){
if(maze[y][x+1]=='#'){
fprintf(stderr,"$ East is blocked!\n");
continue;
} else {
x++;
fprintf(stderr,"$ (%s) moved E\n",name);
if (maze[y][x]=='E') won=1;
continue;
}
}
if (a==W){
if(maze[y][x-1]=='#'){
fprintf(stderr,"$ West is blocked!\n");
continue;
} else {
x--;
fprintf(stderr,"$ (%s) moved W\n",name);
if (maze[y][x]=='E') won=1;
continue;
}
}
}
if (won)
fprintf(stderr,"$ (%s) won in %d moves!\n",name,moves);
else
fprintf(stderr,"$ (%s) didn't win!\n",name);
return 0;
}
This is another part
Code:
#include "navigation.h"
#include "robot.h"
int found_wall=1;
enum Dir hand_on_wall;
char *my_name = "Wallbot 0.1";
char *robot_name() { return my_name; }
void start_at(int x,int y){
return;
}
enum Dir robot_move(Wallinfo *w){
int decided=0;
enum Dir result;
if (!found_wall){
if (w->N){
hand_on_wall=N;
} else {
return N;
}
}
// Walk clockwise - If hand is on north wall, should try to go north first
// (eg. corners)
while (!decided){
if (hand_on_wall==N){
if (!(w->N)){
decided=1;
hand_on_wall=W;
result=N;
} else
if (!(w->W)){
decided=1;
result=E;
} else {
hand_on_wall=E;
}
} else
if (hand_on_wall==E){
if (!(w->E)){
decided=1;
hand_on_wall=N;
result=E;
} else
if (!(w->S)){
decided=1;
result=S;
} else {
hand_on_wall=S;
}
} else
if (hand_on_wall==S){
if (!(w->S)){
decided=1;
hand_on_wall=E;
result=S;
} else
if (!(w->W)){
decided=1;
result=W;
} else {
hand_on_wall=W;
}
} else
if (hand_on_wall==W){
if (!(w->W)){
decided=1;
hand_on_wall=S;
result=W;
} else
if (!(w->N)){
decided=1;
result=N;
} else {
hand_on_wall=N;
}
}
}
return result;
}
Thanks in advance.