该任务是创建一个伪龟图形程序。我已经陷入了困境。乌龟应该从左上角开始,笔向上(图中为T)。我无法弄清楚如何将T更改为小写t以指示用户何时想要使用penDown()
。即使在开始时,如果我使用display()
,程序将正确绘制一个填充空白区域的50x50画布,但不会绘制角落中的初始T..
我应该使用填充0的50x50阵列。数组中的1表示绘制的图块。我用2和3表示笔是上升还是下降。
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
void penUp(int input);
void penDown(void);
int turnRight(void);
int turnLeft (void);
int draw (void);
void display (void);
int end (void);
char turtle, tile;
static int canvas[50][50];
char drawing[50][50];
int i, j, input, a, b;
int main(int argc, char** argv){
for(i = 0; i < 50; i++){
for(j = 0; j < 50; j++){
canvas[i][j] = 0;
}
}
for(i = 0; i < 50; i++){
for(j = 0; j < 50; j++){
drawing[i][j] = ' ';
}
}
canvas[0][0] = 2;
input = 1;
printf("Controls: \n");
printf("1 for pen up (capital T means pen is up) \n");
printf("2 for pen down (lowercase t means pen is down) \n");
printf("3 for turn right \n");
printf("4 for turn left \n");
printf("5 for drawing, followed by an int value for how many tiles to draw \n");
printf("6 to display drawing \n");
printf("9 to end program \n");
do{
printf("Enter command: ");
scanf("%d", &input);
if(input == 2){
penDown();
}
else if(input == 6){
display();
}
else if(input == 9){
}
}while(input != 0);
return 0;
}
void penDown(void){
for(i = 0; i < 50; i++){
for(j = 0; j < 50; j++){
if(canvas[i][j] == 2){
canvas[i][j] == 3;
break;
}
}
}
}
void display(void){
for(i = 0; i < 50; i++){
for(j = 0; j < 50; j++){
if(canvas[i][j] == 2){
drawing[i][j] == 'T';
}
else if(canvas[i][j] == 3){
drawing[i][j] == 't';
}
}
}
for(a = 0; a < 50; a++){
for(b = 0; b < 50; b++){
printf("%c", drawing[a][b]);
}
printf("\n");
}
}
答案 0 :(得分:1)
也许以下内容更接近您尝试实现的目标。我把它简化了一下;添加符号以替换数字;增加了对龟的当前X和Y位置的感觉,而不是搜索板;删除了多余的drawing
表示;添加了case
语句来替换广泛的if else
语句:
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 50
#define HEIGHT 50
enum TILE_TYPES { EMPTY, FILLED, UP, DOWN };
void penUp(int x, int y);
void penDown(int x, int y);
int turnRight(void);
int turnLeft(void);
int draw(void);
void display(void);
int end(void);
int canvas[WIDTH][HEIGHT];
int main(int argc, char** argv) {
for (int j = 0; j < HEIGHT; j++) {
for (int i = 0; i < WIDTH; i++) {
canvas[i][j] = EMPTY;
}
}
int x = 0, y = 0; // update these as turtle moves
canvas[x][y] = UP;
int input = -1;
printf("Controls:\n");
printf("1 for pen up (capital T means pen is up)\n");
printf("2 for pen down (lowercase t means pen is down)\n");
printf("3 for turn right\n");
printf("4 for turn left\n");
printf("5 for drawing, followed by an int value for how many tiles to draw\n");
printf("6 to display drawing\n");
printf("9 to end program\n");
do {
printf("Enter command: ");
scanf("%d", &input);
switch (input) {
case 1:
penUp(x, y);
break;
case 2:
penDown(x, y);
break;
case 6:
display();
break;
case 9:
exit(0);
}
} while (input != 0);
return 0;
}
void penDown(int x, int y){
if (canvas[x][y] == UP) {
canvas[x][y] = DOWN;
}
}
void penUp(int x, int y){
if (canvas[x][y] == DOWN) {
canvas[x][y] = UP;
}
}
void display(void) {
for (int j = 0; j < HEIGHT; j++) {
for (int i = 0; i < WIDTH; i++) {
switch (canvas[i][j]) {
case EMPTY:
printf("%c", ' ');
break;
case FILLED:
printf("%c", 'X');
break;
case UP:
printf("%c", 'T');
break;
case DOWN:
printf("%c", 't');
break;
}
}
printf("\n");
}
}
您现在应该可以输入:6,2,6,1,6,9
并查看笔状态的变化。