正如标题所说我正在创建一个乌龟图形程序,它将从文件(01_A.txt)中获取命令并使用它们在40x40区域内移动乌龟,绘制' *&# 39;笔落下时在地上。到目前为止我的程序我相信是从文件接收输入,但我没有输出。任何帮助都会很棒!
/* PROGRAM: turtle_A.c
AUTHOR: Taylor Havart-Labrecque
DATE:27/02/16
TOPIC:
PURPOSE: Simple Turtle Graphic
LEVEL OF DIFFICULTY: HARD
CHALLENGES:
HOURS SPENT:
NOTES:
*/
/**************************************************************************/
/* Declare include files
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/**************************************************************************/
/* Defines
**************************************************************************/
#define TRUE 1
#define FALSE 0
#define MAX 100
#define SIZE 40
#define RIGHT 0
#define DOWN 1
#define LEFT 2
#define UP 3
#define TOTAL_DIR 3
/**************************************************************************
Function Prototypes
**************************************************************************/
int readCommands( char c[] );
int execCommands( char c[], int n );
int turnRight( int d );
int turnLeft( int d );
void moveTurtle( int down, int a[][ SIZE ], int dir, int dist);
void printFloor( int a[][ SIZE ], char c);
int x = 0, y = 0;
/**************************************************************************
Main
Read commands from stdin or file
Execute the commands
**************************************************************************/
int main( void ) {
/* Array for commands, set to the maximum amount. */
char commands[MAX] = {0};
/* Number of commands, used to decrement the switch statement*/
int ncmds;
ncmds = readCommands(commands);
execCommands(commands, ncmds);
return 0;
}
/**************************************************************************
readCommands
Read one command at a time and store it in the commands arrays
Reads until the EOF ( CTL-D ) or the MAX maximum number of commands is
reached
ARGUMENTS:
commands: An empty array of chars to store the commands read
commands is modified in this function
RETURN:
number of commands read
**************************************************************************/
int readCommands( char commands[]) {
int i=0;
int counter = 0;
char command;
FILE* file = fopen("01_A.txt", "r");
fscanf(file,"%c", &command);
while(!feof(file)){
fscanf(file,"%c", &command);
for (i = 0; i < MAX; i++){
counter++;
commands[i]= command;
}
}
return counter+1;
}
/**************************************************************************
execCommands
execute one command at a time
ARGUMENTS:
commands: an array containing valid turtle commands
ncmds : number of commands stored in commands
0 <= ncmds < MAX
**************************************************************************/
int execCommands( char commands[], int ncmds ) {
int i = 0;
char cmd;
int dir = 0;
int floor[ SIZE ][ SIZE ] = { { 0 } };
int penDown = FALSE;
cmd = commands[i];
while ( ncmds-- ) {
switch ( cmd ) {
case 'U':
penDown = FALSE;
break;
case 'D':
penDown = TRUE;
break;
case 'R':
dir = turnRight( dir );
break;
case 'L':
dir = turnLeft( dir );
break;
case 'F':
moveTurtle( penDown, floor, dir, 1 );
break;
case 'P':
printFloor(floor, '*');
break;
default:
break;
}
cmd = commands[i++];
}
return 0;
}
/**************************************************************************
turnRight
Turn the turtle direction to the right ( 90 degrees counterclockwise )
**************************************************************************/
int turnRight( int d ) {
if (d == RIGHT){
d=DOWN;
}
else if(d == DOWN){
d=LEFT;
}
else if(d == LEFT){
d = UP;
}
else if(d == UP){
d = RIGHT;
}
else
printf("Can not turn right!(Boundary Reached)");
return d;
}
/**************************************************************************
turnLeft
Turn the turtle direction to the left ( 90 degrees counterclockwise )
*************************************************************************/
int turnLeft( int d ) {
if (d == RIGHT){
d = UP;
}
else if(d == DOWN){
d = LEFT;
}
else if(d == LEFT){
d = UP;
}
else if(d == UP){
d = RIGHT;
}
else
printf("Can not turn left!(Boundary reached)");
return d;
}
/**************************************************************************
movePen
move the turtle a distance dis in the direction dir from current pos
if the pen is
down, place a 1 in floor position
ARGUMENTS:
a : floor
down: 1 if pen is down
dir : direction turtle is facing
dist: How far to walk
*************************************************************************/
void moveTurtle( int down, int a[][ SIZE ], int dir, int dist) {
int i;
if (down == TRUE && dir == RIGHT){
for (i = 0; i < dist; i++)
{
a[x][y+i]=1;
}
y+=(dist);
}
else if(down == TRUE && dir == LEFT){
for (i = 0; i < dist; i++){
a[x][y-i] = 1;
}
y-=(dist);
}
else if (down == TRUE && dir == UP){
for (i = 0; i < dist; i++){
a[x-i][y] = 1;
}
x-=(dist);
}
else if(down == TRUE && dir == DOWN){
for (i = 0; i < dist; i++){
a[x+i][y] = 1;
}
x-=(dist);
}
else if (down == FALSE && dir == RIGHT){
y+=(dist+1);
}
else if (down == FALSE && dir == LEFT){
y-=(dist-1);
}
else if (down == FALSE && dir == UP){
x-=(dist-1);
}
else{
x+=(dist-1);
}
return;
}
/**************************************************************************
printFloor
Print the floor
ARGUMENTS:
a : floor
c : char to use to print the floor
*************************************************************************/
void printFloor( int a[][ SIZE ], char c){
int i,j;
for (i = 0; i < SIZE; i++){
printf("\n");
for (j = 0; j < SIZE; j++){
if (a[i][j] == 1){
printf("%c", c);
}
else{
printf(" ");
}
}
}
}
答案 0 :(得分:0)
查看while
中的execCommands()
循环。您使用第一个命令两次,而不是最后一个。您的代码(仅显示相关部分)是:
int i = 0;
...
cmd = commands[i];
while ( ncmds-- ) {
switch ( cmd ) {
...
}
cmd = commands[i++];
}
ncmds
是命令的数量(函数参数),它减少计数读取的命令数。您使用i
从0到ncmds
进行计数。但请查看修改i
的位置。第一次到达cmd = commands[i++]
时,它会在cmd
中存储与初始化cmd = commands[i]
相同的值。所以这个第一个命令使用了两次,你总共只使用ncmds
,所以排除了最后一个。如果P
是输入文件的最后一个命令(我可能想在结尾打印结果),它将不会被执行。
您只需将i++
替换为++i
即可。但是for
循环可能更具可读性:
int i;
...
for ( i=0; i<ncmds; i++ ) {
switch ( commands[i] ) {
...
}
}
请注意,您甚至不需要cmd
变量,因为您不会在任何switch
个案例中引用它。
你确定你甚至正确地阅读了这些命令吗?你在readCommands()
中做了类似的事情:
fscanf(file,"%c", &command);
while(!feof(file)){
fscanf(file,"%c", &command);
for (i = 0; i < MAX; i++){
counter++;
commands[i]= command;
}
}
您将一个char
读入command
,然后在循环开始时,您将另一个char
读入command
,而不对第一个做任何事情。我甚至不确定for (i = 0; i < MAX; i++)
是什么意思。看起来它只是用相同的命令填充commands
缓冲区中的每个空格,然后用下一个命令重复这个等等。这不是你想要的。您可能想在此重新考虑您的策略。