1
你好
我正在解決課本練習來解決右手原則迷宮
我使用開關的情況下處理它
switch (face)
{
case face_EAST:
{
if(map[x][y + 1] == '.' && map[x + 1][y] == '#')
{
y = y + 1;
gotoxy(x, y);
face = face_EAST;
}
else if (map[x - 1][y] == '.' && map[x][y + 1] == '#')
{
x = x - 1;
gotoxy(x, y);
face = face_NORTH;
}
else if (map[x + 1][y] == '.' && map[x + 1][y - 1] == '#')
{
x = x + 1;
gotoxy(x, y);
face = face_SOUTH;
}
else
face = face_SOUTH;
break;
}
case face_SOUTH :
{
if(map[x + 1][y] == '.' && map[x][y - 1] == '#')
{
x = x + 1;
gotoxy(x, y);
face = face_SOUTH;
}
else if(map[x][y - 1] == '.' && map[x - 1][y - 1] == '#')
{
y = y - 1;
gotoxy(x, y);
face = face_WEST;
}
else if(map[x][y + 1] == '.' && map[x + 1][y] == '#')
{
y = y + 1;
gotoxy(x, y);
face = face_EAST;
}
else
face = face_WEST;
break;
}
case face_WEST:
{
if(map[x][y - 1] == '.' && map[x - 1][y] == '#')
{
y = y - 1;
gotoxy(x, y);
face = face_WEST;
}
else if(map[x - 1][y] == '.' && map[x - 1][y + 1] == '#')
{
x = x - 1;
gotoxy(x, y);
face = face_NORTH;
}
else if(map[x + 1][y] == '.' && map[x][y - 1] == '#')
{
x = x + 1;
gotoxy(x, y);
face = face_SOUTH;
}
else if(map[x][y + 1] == '.' && map[x + 1][y] == '#')
{
y = y + 1;
gotoxy(x, y);
face = face_EAST;
}
else if(map[x - 1][y] == '.' && map[x - 1][y + 1] == '#')
{
x = x - 1;
gotoxy(x, y);
face = face_NORTH;
}
else
face = face_NORTH;
break;
}
case face_NORTH:
{
if(map[x - 1][y] == '.' && map[x][y + 1] == '#')
{
x = x - 1;
gotoxy(x, y);
face = face_NORTH;
}
else if(map[x][y - 1] == '.' && map[x - 1][y] == '#')
{
y = y - 1;
gotoxy(x, y);
face = face_WEST;
}
else if(map[x][y + 1] == '.' && map[x + 1][y + 1] == '#')
{
y = y + 1;
gotoxy(x, y);
face = face_EAST;
}
else
face = face_EAST;
break;
}
}
我想知道是否有任何更容易的方法來使右手打印ciple工作?
我雖然要改變座標系,但不能實現它
當左手也以同樣的方式工作時,稱之爲右手原則是否正確? – Potatoswatter
我認爲你可以使用表格而不是代碼 – perreal
@perreal我該如何使用表格來做到這一點?我不知道 – good5dog5