C++乌龟画图

  • 输入命令并进行绘图

题目:Turtle Graphics

The Logo language, which is popular among elementary school children,made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the roomunder the control of a C++ program. The turtle holds a pen in one of two positions, up or down.While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad as well.Use a 20-by-20 array floor that is initialized to false. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your program must process are shown in Fig.

简单翻译过来,大意是一个拿着笔的乌龟,笔放下时,画出移动的路径;当笔向上时,乌龟自由地四处移动,不画任何东西。记录乌龟的操作命令和创建一个画板(使用一个20*20的数组),并随时记录海龟的当前位置,以及笔是否处于上升或下降状态。假设乌龟总是用它的笔从(0, 0)开始。

命令表

例如:

假设乌龟在靠近地板中心的某个地方。下面的命令将绘制和打印12*12的方形路径并且最后笔尖向上。

输入

2
5,12
3
5,12
3
5,12
3
5,12
1
6
9

输出


代码块

#include <iostream>
using namespace std;
const int COMMANDS = 100, SIZE = 40;  //这里SIZE为展示画板大小,由于原本行列间隔不一样,为美观我将行间隔
int turnRight(int);                //一个空位,COMMANDS为命令输入上限,为后面创建命令数组使用
int turnLeft(int);
void getCommands(int[][2]);
void movePen(int, int[][SIZE], int, int);
void printArray(const int[][SIZE]);int main()
{int floor[SIZE][SIZE] = { 0 };int commands = 0;static int direction = 0;  //记录方向int commandArray[COMMANDS][2] = { 0 };  //储存命令int count = 0;int distance = 0;bool penDown = false;getCommands(commandArray);commands = commandArray[count][0];while (commands!=9){//cout << "commands:" << commands << endl;//测试用switch (commands){case 1:penDown = false;break;case 2:penDown = true;break;case 3:direction = turnRight(direction);break;case 4:direction = turnLeft(direction);break;case 5:distance = commandArray[count][1];movePen(penDown, floor, direction, distance);break;case 6:cout << "\nThe drawing is:\n\n";printArray(floor);break;default:break;}commands = commandArray[++count][0];  //从数组中取出命令}return 0;
}void getCommands(int commands[][2])
{int tempCommand, i;cout << "Enter command(9 to end input): ";cin >> tempCommand;for ( i = 0; tempCommand!=9&&i<COMMANDS; i++){commands[i][0] = tempCommand;if (tempCommand==5){cin.ignore();  //忽略5后面的字符','cin >> commands[i][1];  //将移动距离保存在commands[i][1]中。}cout << "Enter command(9 to end input): ";cin >> tempCommand;}commands[i][0] = 9;
}int turnRight(int dir)
{return ++dir > 3 ? 0 : dir;  //原点为0,右转4次回到原点
}
int turnLeft(int dir)
{return --dir < 0 ? 3 : dir;
}
void movePen(int down, int a[][SIZE], int dir, int dis)
{static int xPosition = 0, yPosition = 0;  //设置原点int xTemp, yTemp, i;xTemp = xPosition;yTemp = yPosition;//cout << "Direction:" << dir << " Pen:" << down << endl;//测试用 switch (dir){case 0:for ( i = 0; i < dis*2&&(yTemp+i)<SIZE; i+=2){if (down){a[xTemp][yTemp + i] = 1;}else if (down==0){a[xTemp][yTemp + i] = 0;}xPosition = xTemp;yPosition = yTemp+i;}break;case 1:for (i = 0; i < dis && (xTemp + i)<SIZE; ++i){if (down){a[xTemp+i][yTemp] = 1;}else if (down == 0){a[xTemp + i][yTemp] = 0;}xPosition = xTemp + i;yPosition = yTemp ;}break;case 2:for (i = 0;  i < dis * 2 && (yTemp - i)>=0; i += 2) //加2是为了让行和列的间隔一样,单纯美观{if (down){a[xTemp][yTemp-i] = 1;}else if (down == 0){a[xTemp][yTemp-i] = 0;}xPosition = xTemp;yPosition = yTemp - i;}break;case 3:for (i = 0; i < dis && (xTemp - i)>=0; ++i){if (down){a[xTemp - i][yTemp] = 1;}else if (down == 0){a[xTemp - i][yTemp] = 0;}xPosition = xTemp - i;yPosition = yTemp;}break;}
}
void printArray(const int a[][SIZE])
{for (int i = 0; i < SIZE; ++i){for (int j = 0; j < SIZE; ++j){if (a[i][j] == 0) cout << " ";else cout << "*";}cout << endl;}
}

本人只是一个大一小白,如果你发现了错误或者有什么好的建议的话请在下方提出,谢谢!

٩꒰▽ ꒱۶⁼³₌₃ 学习去咯

使用C++编写乌龟画图程序相关推荐

  1. 用JAVA制作一个画图程序

    在日常生活中画图软件给我们带来了许多便利,往大了说可以用它来制作图标,往小了说也可以当做一个涂鸦板消磨时间. 比如:制作个图标 用来画画: 这个画图程序界面如上,这个程序的功能如下: 1.可更改的画笔 ...

  2. python画图程序有图-python画图程序

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 今天做的是用python实现画图,画出你想要的图案,只要你的想象力足够丰富,以及 ...

  3. 【python基础】python中的乌龟画图

    自学了python,看到python中有个turtle模块,乌龟画图,使用这个模块做了随机画五角星的的小程序 #导入turtle模块 import turtle,random t = turtle # ...

  4. 蒙特卡罗MCNP学习汇总(一)-----MCNP简介及编写第一个程序

    目录 简介: 什么是MC模拟 介绍 应用 运行 编写第一个程序 格式 程序 讲解 现象 简介: 什么是MC模拟 一种通过随机抽样解决数学问题的一种数值计算方法. MC方法解决的主要数学问题 -数值积分 ...

  5. 用Python实现win10画图程序拖动鼠标画各种形状多边形功能

    win10画图程序在工具栏中有许多按钮,按钮显示的图形就是各种不同形状的多边形,见下图.单击选中某按钮,就能拖动鼠标画这种形状多边形.本文介绍用Python实现win10画图程序这个功能的方法. 所谓 ...

  6. win10画图程序拖动鼠标画图形功能用python实现的思路

    win10画图程序本质上是一个位图处理程序.其最主要的功能是画各种图形,包括线.矩形.椭圆(圆)和各种多边形等,并将各种图形保存到位图中.线.矩形.椭圆(圆)和各种多边形都可以用一个矩形定位,这点在前 ...

  7. 基于C++的画图程序

    (实验说明:本程序是在linux系统下编写编译的,没有Windows可执行文件,所以只能给出源码,但是经测试好像源码在windows系统下opencv库有一些问题,不能正常编译,所以只能在linux下 ...

  8. java画笔覆盖在界面_Java实现画图程序和重绘

    上次聊了一下事件监听机制,今天就来聊一下怎么实现一个画图程序并且实现重绘. 一.实现画图程序 1.实现一个画图程序所需的API类? JFrame窗体容器组件类 JPanel 面板元素组件类 JButt ...

  9. 87岁的老奶奶喜欢用windows画图程序作画,而且画得还贼好!

    来自:安说|搜集有趣的事 信息化时代,谁还不会点新技能呢? 你可能认为老年人很难跟上时代的步伐,比如说你的奶奶不会用智能手机,不会发微信,不会使用手机支付等等.但是事实是,他们可能并不是不会,而是不想 ...

最新文章

  1. mysql新建表96k_innodb表 手工导入导出
  2. 解决Linux安装过程中不能安装Grub的问题
  3. Kubernetes初步学习
  4. python3.6+Appium实现手机微信自动回复
  5. 如何安装TreeView控件
  6. tx关于机器人的律师函_酷q、晨风等第三方机器人被封杀停运,余者纷纷跑路!...
  7. 通过ip如何免费反查域名?
  8. 弹簧振子串联matlab,关于串联弹簧振子的研究
  9. 两步实现安卓手机秒变网络摄像头
  10. vue项目兼容IE浏览器,判断IE11以下,则提示浏览器版本过低,更新浏览器页面
  11. FFmpeg 使用命令整理 – 提取音频或视频、提取图片、格式转换等
  12. 关于Fusion on Apple Silicon的谨慎猜测
  13. Android代码安装apk程序
  14. gcd中group实现并发任务全部完成后
  15. SQLServer数据库镜像配置
  16. antv/G2 v4使用遇坑之旅
  17. 不要轻易放过一个30几岁的程序员
  18. android城市万花筒,诺基亚720评测
  19. 数学建模-相关性分析(Matlab)
  20. 竣达技术 | 8路IO模块模拟量采集 通讯信号检测 继电器输出控制 可接入漏水和声光

热门文章

  1. 神雕侠侣手游服务器维修,《神雕侠侣》2021年3月25日更新维护新服开启公告
  2. UVA11491 奖品的价值 Erasing and Winning
  3. Replays For SC2 LOG
  4. 谷歌搜索留痕外推霸屏
  5. wxWidgets体系结构
  6. win7文件名不能使用哪些符号
  7. 合肥工业大学期中期末试卷在哪找?
  8. “任务管理器”中“进程”各个选项卡的含义?
  9. java jdk 7u79 64位_jdk-7u79-windows-x64.exe下载
  10. nubia基于android深度定制的ui,FiT2.0突破新维度 nubia UI 4.0系统体验