1. 程式人生 > >使用C++編寫烏龜畫圖程式

使用C++編寫烏龜畫圖程式

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; } }

本人只是一個大一小白,如果你發現了錯誤或者有什麼好的建議的話請在下方提出,謝謝!

٩꒰▽ ꒱۶⁼³₌₃ 學習去咯