控制面板比较容易,使用while和switch语句就可以解决了,It's too easy.
但是如何控制海龟就成了难点和重点,只要这个一旦解决,所有的问题就将迎刃而解。
首先需要一个函数用于输出当笔在海龟下时的轨迹:
在数组floor[50][50]中,首先将二维数组初始化为0。如果海龟走过,就将走过的数组元素修改为1,当然这不是此函数的功能。此函数只是输出值为1的元素。

Code
1
void print( int a[][ SIZE ], char symbol )
2

{
3
int i;
4
int j;
5
for( i = 0; i < SIZE; i++ )
6
{
7
for ( j = 0; j < SIZE; j++ )
8
{
9
if ( a[ i ][ j ] == 1 )
10
printf( "%c", symbol );
11
else
12
printf( " " );
13
}
14
printf( "\n" );
15
}
16
}
最关键的问题是,如何判断海龟的方向?
海龟在方格中爬行,有自己的方向,我们将其称为“相对方向”。也就是说,海龟先向右走,再向右走,再向右走,再向右走,就回到了原来的位置,这个“向右”是相对海龟自己而言的。然而,我们以方格为参照物,那么海龟的方向就是右>下>左>上,我们称此为“绝对方向”。如果能够确定绝对方向,那么问题就变得很容易了。我很顺利的写下来四个函数(绝对方向):

Code
1
// move right, x add step
2
int moveRight( int a[][ SIZE ], int row, int column, int step, int pen)
3

{
4
int i;
5
for ( i = column; i < column + step; i++ )
6
{
7
a[ row ][ i ] = pen - 1;
8
}
9
10
return column + step;
11
}
12
13
// move left, x subtract step
14
int moveLeft( int a[][ SIZE ], int row, int column, int step, int pen )
15

{
16
int i;
17
for( i = column; i >= column - step; i-- )
18
{
19
a[ row ][ i ] = pen - 1;
20
}
21
return column - step;
22
}
23
24
// move up, y add step
25
int moveUp( int a[][ SIZE ], int row, int column, int step, int pen )
26

{
27
int i;
28
for ( i = row; i >= row - step; i-- )
29
{
30
a[ i ][ column ] = pen - 1;
31
}
32
return row - step;
33
}
34
35
// move down, y add step
36
int moveDown( int a[][ SIZE ], int row, int column, int step, int pen )
37

{
38
int i;
39
for ( i = row; i <= row + step; i++ )
40
{
41
a[ i ][ column ] = pen - 1;
42
}
43
return row + step;
44
}
其中参数row是二维数组的第一维,column是第二维。
用数组体现方格方格如下:
<0, 0> <0, 1> <0, 2> <0, 3> <0, 4> ... <0, 49>
<1, 0> <1, 1> <1, 2> <1, 3> <1, 4> ... <1, 49>
<2, 0> <2, 1> <2, 2> <2, 3> <2, 4> ... <2, 49>
...
<49,0> <49, 1> <49, 2> <49, 3> <49, 4> ... <49, 49>
参数step是向前走的步长。
参数pen是代表笔在海龟上或下。pen=1是代表笔在海龟上,这样pen - 1的值为0,这样就不会产生轨迹;pen=2是代表笔在海龟下,这样pen - 1的值为1,这样数组元素为1,就可以通过print函数输入轨迹了。
返回值是非常关键的,它反映了海龟位置的变化。用于定位海龟位置。
可是题中只有相对位置的左右,没有上下。如何将海龟的相对方向转换为绝对方向呢。知道电灯泡是如何发明出来的么?是一万次实验么?记不得了,反正爱迪生经过了许多试验之后才发明了电灯泡。我为了这个问题也做了许多试验,在本上画了无数的图,终于得出一个结论:
当海龟
向右(相对方向)时:+1
向左(相对方向)时:-1
默认海龟在<0,0>位置上方向向上,经过多次相对方向的组合得出的数字除以4。如果
余数为0,绝对位置向上;
余数为1,绝对位置向右;
余数为2,绝对位置向下;
余数为3,绝对位置向左。
这样就可以通过if语句实现相对位置向绝对位置的转换。主文件的代码如下:

Code
1
#include<stdio.h>
2
#include"623.h"
3
#define SIZE 50
4
5
int main()
6

{
7
int floor[ SIZE ][ SIZE ] =
{ 0 };
8
int row = 0;
9
int column = 0;
10
int cmd = 0;
11
int step;
12
int pen;
13
int direct = 0;
14
15
while ( cmd != 9 )
16
{
17
scanf( "%d", &cmd );
18
switch( cmd )
19
{
20
case 1: pen = 1; break;
21
case 2: pen = 2; break;
22
case 3: direct++; break;
23
case 4: direct--; break;
24
case 5:
{
25
scanf( "%d", &step );
26
if ( direct % 4 == 0 )
27
row = moveUp( floor, row, column, step, pen );
28
else if ( direct % 4 == 1 )
29
column = moveRight( floor, row, column, step, pen );
30
else if ( direct % 4 == 2 )
31
row = moveDown( floor, row, column, step, pen );
32
else if ( direct % 4 == 3 )
33
column = moveLeft( floor, row, column, step, pen );
34
break;
35
}
36
case 6: print( floor, '*' ); break;
37
default: printf( "Error" );
38
}
39
}
40
printf( "\n\nThe End\n" );
41
42
43
column = moveRight( floor, row, column, 12, pen);
44
row = moveDown( floor, row, column, 12, pen);
45
column = moveLeft( floor, row, column, 10, pen);
46
row = moveUp( floor, row, column, 12, pen);
47
48
print( floor, '*' );
49
50
return 0;
51
}
52