【发布时间】:2021-03-08 12:41:03
【问题描述】:
我正在尝试用 C 重新创建 Turtle 图形,但我的程序没有给我想要的东西。我的“乌龟”应该从笔向上并朝东开始,但相反,它从朝南开始。我决定为方向提供编号变量,以便我可以轻松管理它们。所以 3 是东,6 是南,9 是西,12 是北我的 0,0 位置在数组的左上角。为了导航,用户可以使用命令来改变方向、决定何时将笔向上或向下以及何时打印。
当我输入命令 2(下笔)、5,15(移动 15 个空格)、6(打印)和 9(显示结果)时,这就是它给我的:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
而不是这个:
***************
这是我的代码:
#include<stdio.h>
int facing = 3; //start by facing east
int pen = 1; //start by pen up
int copyToArrays(int commandCheck[2][2], const int commands);
int readCommand(int commandCheck[]);
void performCommand(const int command, const int factor, int floor[50][50]);
void draw(const int floor[50][50]);
// y and x coordinates of turtle
int posX =0;
int posY =0;
int main(void)
{
//INSTRUCTIONS
printf("Command Key: \n\n");
printf("1 ---- pen up\n");
printf("2 ---- pen down\n");
printf("3 ---- turn right\n");
printf("4 ---- turn left\n");
printf("5, N ---- move forward N spaces\n");
printf("6 ---- Print 50 by 50 Floor\n");
printf("9 ---- End Data\n\n");
printf("Enter your commands.\n");
int floor[50][50] = {{0}, {0}};// floor of length and width of 50
int commandArrays[100][2] = {{0}, {0}};// arrays for the commands
int numberOfCommands = 0;
numberOfCommands = copyToArrays(commandArrays, 100);// calculate how many commands to process
//for loop to go through the commands and perform them one by one
for(size_t i = 0; i < numberOfCommands; i++)
{
performCommand(commandArrays[i][0], commandArrays[i][1], floor);
}
}
// function to copy commands into arrays and return the number of commands
int copyToArrays(int commandArray[2][2], const int commands)
{
int i;
int commandCheck[2];
for ( i = 0; i <commands && readCommand(commandCheck); i++)
{
commandArray[i][0] = commandCheck[0];
commandArray[i][1] = commandCheck[1];
}
return i;
}
// Function to take input commands from user while checking if it is a move command or not
int readCommand(int commandCheck[])
{
scanf("%d,%d", &commandCheck[0], &commandCheck[1]);
if(commandCheck[0] != 5)
{
commandCheck[1] = 0;
}
if(commandCheck[0] == 9)
{
return 0;
}
else
{
return 1;
}
}
// read command and perform task accordingly
void performCommand(const int command, const int factor, int floor[50][50])
{
int j;
switch(command)
{
case 1: pen = 1;
break;
case 2: pen = 0;
floor[posX][posY] = 1;
break;
case 3: if (facing == 3)
{
facing = 6;
}
else if (facing == 6)
{
facing = 9;
}
else if (facing == 9)
{
facing = 12;
}
else if (facing == 12)
{
facing = 3;
}
break;
case 4: if (facing == 3)
{
facing = 12;
}
else if (facing == 12)
{
facing = 9;
}
else if (facing == 9)
{
facing = 6;
}
else if (facing == 6)
{
facing = 3;
}
break;
case 5: if (facing == 3)
{
for (j = 1; j <= factor; j++)
{
posX++;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if (facing == 6)
{
for(j = 1; j <= factor; j++)
{
posY--;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if (facing == 9)
{
for(j = 1; j <= factor; j++)
{
posX--;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
else if(facing == 12)
{
for(j = 1; j <= factor; j++)
{
posY++;
if(pen == 0)
{
floor[posX][posY] = 1;
}
}
}
break;
case 6: draw(floor);
break;
default:
break;
}
}
//FUntion to draw on the floor
void draw(const int floor[50][50])
{
printf("\n");
for (int i = 0; i < 49; i++)
{
for(int j = 0; j < 49; j++)
{
if(floor[i][j] == 1)
{
printf("%s", "*");
}
else
{
printf(" ");
}
}
puts("");
}
}
【问题讨论】:
-
因为只有“5”命令需要一个额外的参数,我认为你不能按原样使用简单的
scanf("%d,%d",...);。请参阅我最近的回答:stackoverflow.com/questions/64979351/an-array-of-arrays/… 了解读取命令的方法。 -
由于您标记为 C++,请考虑使用
std::vector<std::vector<>>。传递给函数(通过引用)比传递数组更容易。 -
代替
printf("%s", "*");打印1个字符,考虑putchar('*'); -
因为您的
draw以错误的顺序循环i和j。将j循环放在外面,一切正常。
标签: c++ arrays c multidimensional-array graphics