Ahojte. Mohol by mi niekto pomoct s tymto kodom (potrebujem tam dopracovat funkciu putBeeper) a to nasledovne:
Karel sa nachádza v ľavom hornom rohu.
>.....
......
......
......
......
......
Situácia po postavenà štvorca:
......
.****.
.****.
.****.
.****.
.....>
Koncová situácia:
Karel sa opäť nachádza v ľavom hornom rohu.
>.....
....*.
...*..
..*...
.*....
......
Samotný kód:
#define MAX_HEIGHT 20
#define MAX_WIDTH 20
#define EMPTY 0
#define WALL 1
void draw(void);
void movek(void);
void turnLeft(void);
void turnOn(void);
int frontIcClear(void);
int frontIsBlocked(void);
int width=6;
int height=6;
int karel_x=0, karel_y=0, karel_direction=180;
int world1[6][6] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
};
int world[MAX_HEIGHT][MAX_WIDTH];
void turnOn(void){
int i, j;
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
world[j]=world1[j];
}
}
}
void draw(void){
int i, j;
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
if(karel_x==i && karel_y==j)
{
if(karel_direction==0) printf("\t <");
if(karel_direction==90) printf("\t ^");
if(karel_direction==180) printf("\t >");
if(karel_direction==270) printf("\t v");
}
else
{
if(world[j]==EMPTY) printf("\t.");
if(world[j]==WALL) printf("\t#");
}
}
printf("\n\n\n");
}
sleep(1000);
system("cls");
}
void movek(void)
{
if(karel_direction==0)
{
if(frontIsClear())
{
karel_x=karel_x;
karel_y=karel_y-1;
}
else printf("access denied\n");
}
else if(karel_direction==90)
{
if(frontIsClear())
{
karel_x=karel_x-1;
karel_y=karel_y;
}
else printf("access denied\n");
}
else if(karel_direction==180)
{
if(frontIsClear())
{
karel_x=karel_x;
karel_y=karel_y+1;
}
else printf("access denied\n");
}
else if(karel_direction==270)
{
if(frontIsClear())
{
karel_x=karel_x+1;
karel_y=karel_y;
}
else printf("access denied\n");
}
}
void turnLeft(void)
{
if(karel_direction==0)
{
karel_direction=270;
}
else if(karel_direction==90)
{
karel_direction=0;
}
else if(karel_direction==180)
{
karel_direction=90;
}
else if(karel_direction==270)
{
karel_direction=180;
}
draw();
}
int frontIsClear(void){
if(karel_direction==90)
{
if(karel_x==0)
{
return 0;
}
else
{
if(world[karel_x-1][karel_y]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==0)
{
if(karel_y==0)
{
return 0;
}
else
{
if(world[karel_x][karel_y-1]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==180)
{
if(karel_y==width)
{
return 0;
}
else
{
if(world[karel_x][karel_y+1]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==270)
{
if(karel_x==height)
{
return 0;
}
else
{
if(world[karel_x+1][karel_y]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
}
int frontIsBlocked(void){
if(frontIsClear()) return 0;
else return 1;
}
Karel sa nachádza v ľavom hornom rohu.
>.....
......
......
......
......
......
Situácia po postavenà štvorca:
......
.****.
.****.
.****.
.****.
.....>
Koncová situácia:
Karel sa opäť nachádza v ľavom hornom rohu.
>.....
....*.
...*..
..*...
.*....
......
Samotný kód:
#define MAX_HEIGHT 20
#define MAX_WIDTH 20
#define EMPTY 0
#define WALL 1
void draw(void);
void movek(void);
void turnLeft(void);
void turnOn(void);
int frontIcClear(void);
int frontIsBlocked(void);
int width=6;
int height=6;
int karel_x=0, karel_y=0, karel_direction=180;
int world1[6][6] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
};
int world[MAX_HEIGHT][MAX_WIDTH];
void turnOn(void){
int i, j;
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
world[j]=world1[j];
}
}
}
void draw(void){
int i, j;
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
if(karel_x==i && karel_y==j)
{
if(karel_direction==0) printf("\t <");
if(karel_direction==90) printf("\t ^");
if(karel_direction==180) printf("\t >");
if(karel_direction==270) printf("\t v");
}
else
{
if(world[j]==EMPTY) printf("\t.");
if(world[j]==WALL) printf("\t#");
}
}
printf("\n\n\n");
}
sleep(1000);
system("cls");
}
void movek(void)
{
if(karel_direction==0)
{
if(frontIsClear())
{
karel_x=karel_x;
karel_y=karel_y-1;
}
else printf("access denied\n");
}
else if(karel_direction==90)
{
if(frontIsClear())
{
karel_x=karel_x-1;
karel_y=karel_y;
}
else printf("access denied\n");
}
else if(karel_direction==180)
{
if(frontIsClear())
{
karel_x=karel_x;
karel_y=karel_y+1;
}
else printf("access denied\n");
}
else if(karel_direction==270)
{
if(frontIsClear())
{
karel_x=karel_x+1;
karel_y=karel_y;
}
else printf("access denied\n");
}
}
void turnLeft(void)
{
if(karel_direction==0)
{
karel_direction=270;
}
else if(karel_direction==90)
{
karel_direction=0;
}
else if(karel_direction==180)
{
karel_direction=90;
}
else if(karel_direction==270)
{
karel_direction=180;
}
draw();
}
int frontIsClear(void){
if(karel_direction==90)
{
if(karel_x==0)
{
return 0;
}
else
{
if(world[karel_x-1][karel_y]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==0)
{
if(karel_y==0)
{
return 0;
}
else
{
if(world[karel_x][karel_y-1]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==180)
{
if(karel_y==width)
{
return 0;
}
else
{
if(world[karel_x][karel_y+1]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
else if(karel_direction==270)
{
if(karel_x==height)
{
return 0;
}
else
{
if(world[karel_x+1][karel_y]==WALL)
{
return 0;
}
else
{
return 1;
}
}
}
}
int frontIsBlocked(void){
if(frontIsClear()) return 0;
else return 1;
}