学以致用——Java源码——海龟绘图小程序2018版(Turtles Graphics)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/85283893

程序功能:

支持用户在画板上按以下绘图指令绘制轨迹(类似于小海龟在画板上按东、南、西、北方向行走时形成的任意轨迹):
1:抬起画笔
2:落下画笔
3:右转
4:左转
5:画笔向前移动n步(第一次移动时默认为向右移动,n为自定义)
6:绘制图形
7:擦除图形
9:退出程序

参考文章:

简单的海龟绘图语言(Design a Turtle Graphics language which can be used to draw "L" graphics),

https://blog.csdn.net/hpdlzu80100/article/details/2312204

运行效果示例:

辅助工具:

Excel中使用条件格式化,可以快速设计要使用海龟绘图语言(本程序)绘制的图形,并可帮助我们输入指令(前进方向,前进步数)。

运行过程示例:

********欢迎使用海龟绘图语言********

 

请按照以下说明输入绘图指令:

1:抬起画笔

2:落下画笔

3:右转

4:左转

5:画笔向前移动n步(第一次移动时默认为向右移动,n为自定义)

6:绘制图形

7:擦除图形

9:退出程序

 

指令(当前位置:【11】,当前朝向:东):5

请输入移动步数:6

已向前移动6步,当前位置:【17】,当前朝向:东

指令(当前位置:【1,7】,当前朝向:东):3

朝向已更改为:南

指令(当前位置:【1,7】,当前朝向:南):5

请输入移动步数:1

已向前移动1步,当前位置:[2,7],当前朝向:南

指令(当前位置:【2,7】,当前朝向:南):2

画笔已落下。

指令(当前位置:【27】,当前朝向:南):4

朝向已更改为:东

指令(当前位置:【27】,当前朝向:东):5

请输入移动步数:5

已向前移动5步,当前位置:【212】,当前朝向:东

指令(当前位置:【212】,当前朝向:东):3

朝向已更改为:南

指令(当前位置:【212】,当前朝向:南):5

请输入移动步数:3

已向前移动3步,当前位置:[5,12],当前朝向:南

指令(当前位置:【5,12】,当前朝向:南):4

朝向已更改为:东

指令(当前位置:【5,12】,当前朝向:东):5

请输入移动步数:4

已向前移动4步,当前位置:【516】,当前朝向:东

指令(当前位置:【516】,当前朝向:东):3

朝向已更改为:南

指令(当前位置:【516】,当前朝向:南):5

请输入移动步数:6

已向前移动6步,当前位置:[11,16],当前朝向:南

指令(当前位置:【1116】,当前朝向:南):3

朝向已更改为:西

指令(当前位置:【1116】,当前朝向:西):5

请输入移动步数:13

已向前移动13步,当前位置:[11,3],当前朝向:西

指令(当前位置:【113】,当前朝向:西):3

朝向已更改为:北

指令(当前位置:【113】,当前朝向:北):5

请输入移动步数:6

已向前移动6步,当前位置:[5,3],当前朝向:北

指令(当前位置:【5,3】,当前朝向:北):3

朝向已更改为:东

指令(当前位置:【5,3】,当前朝向:东):5

请输入移动步数:4

已向前移动4步,当前位置:【57】,当前朝向:东

指令(当前位置:【5,7】,当前朝向:东):3

朝向已更改为:南

指令(当前位置:【57】,当前朝向:南):3

朝向已更改为:西

指令(当前位置:【57】,当前朝向:西):3

朝向已更改为:北

指令(当前位置:【57】,当前朝向:北):5

请输入移动步数:3

已向前移动3步,当前位置:[2,7],当前朝向:北

指令(当前位置:【27】,当前朝向:北):6

代码如下:

import java.util.Scanner;

//JHTP Exercise 7.10: Sales Commissions
//by [email protected]
/*7.21 (Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical
 *  turtle that walks around the room under the control of a Java application. 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, and 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.
Use a 20-by-20 array floor that’s initialized to zeros. 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 application must process are shown in Fig. 7.29.

Command -> Meaning
1 ->  Pen up
2  -> Pen down
3 ->  Turn right
4  -> Turn left
5,10 ->  Move forward 10 spaces (replace 10 for a different number of spaces)
6  -> Display the 20-by-20 array
9 ->  End of data (sentinel)

Suppose that the turtle is somewhere near the center of the floor. The following “program” would draw
 and display a 12-by-12 square, leaving the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 
command (display the array) is given, wherever there’s a 1 in the array, display an asterisk or any 
character you choose. Wherever there’s a 0, display a blank.
Write an application to implement the turtle graphics capabilities discussed here. Write several turtle
 graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle 
 graphics language.*/

public class TurtleGraphics  {
	
	//private static int commands[]={1,5,2,5,3,5,4,5,3,5,5,3,5,5,5,3,5,5,3,5,4,5,1,6,9}; //绘制“凸”字形
	//private static int commands[]={2,5,12,3,5,12,3,5,12,3,5,12,1,6,9}; //绘制“凸”字形
	private static int pace=5;
	private static int[][] positions = new int[20][20];
	private static int row,column;
	private static boolean penLocation;	//画笔位置(抬起或落下,默认为抬起)
	private static String currentDirection = "东";	//初始朝向为东
	private static boolean moveLeft,moveRight,moveDownwards,moveUpwards; 	//画笔移动方向(东、南、西、北)
	private static int instruction;	//用户输入的单条指令
	private static Scanner input = new Scanner(System.in);
 
 public static void main(String[] args)
 {   
     row=0;
     column=0;
     moveRight=true;
     
     System.out.println("********欢迎使用海龟绘图语言********");   
     System.out.printf("%n请按照以下说明输入绘图指令:%n"
     		+ "1:抬起画笔%n"
     		+ "2:落下画笔%n"
     		+ "3:右转%n"
     		+ "4:左转%n"
     		+ "5:画笔向前移动n步(第一次移动时默认为向右移动,n为自定义)%n"
     		+ "6:绘制图形%n"
     		+ "7:擦除图形%n"
     		+ "9:退出程序%n%n");
     do {
	 System.out.printf("指令(当前位置:【%d,%d】,当前朝向:%s):",row+1, column+1, currentDirection);
	 instruction =input.nextInt();
	 if (instruction == 5) {
		 System.out.printf("请输入移动步数:");
	 pace =input.nextInt();
	 turtleBehaviour(instruction);	//如果指令是5,先设置步长,再执行前移动作
	 continue;
	 }
	 else turtleBehaviour(instruction);
		} while (instruction != 9);  


 }
 
 public static void turtleBehaviour(int argument)
 {
     switch (argument)
     {
     case 1:	//抬起画笔
         penLocation=false;
         System.out.println("画笔已抬起。");
         break;
     case 2:	//落下画笔
         penLocation=true;
         System.out.println("画笔已落下。");
         break;
     case 3:// 右转
         if (moveRight)  //如果当前移动方向是右移,则右转向后移动方向变为向下移动
         {
             moveDownwards=true;	//向下移动为true
             moveLeft=moveRight=moveUpwards=false;	//其他移动方向为false
             currentDirection="南";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         else if(moveLeft)	//如果当前移动方向是左移,则右转向后移动方向变为向上移动
         {
             moveUpwards=true;
             moveLeft=moveRight=moveDownwards=false;
             currentDirection="北";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         else if(moveUpwards)	//如果当前移动方向是上移,则右转向后移动方向变为向右移动
         {
            moveRight=true;
             moveLeft=moveUpwards=moveDownwards=false;
             currentDirection="东";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         else if(moveDownwards)	//如果当前移动方向是下移,则右转向后移动方向变为向左移动
         {
             moveLeft=true;
             moveUpwards=moveRight=moveDownwards=false;
             currentDirection="西";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         
         break;
     case 4:// 左转
         if (moveRight)
         {
             moveUpwards=true;
             moveLeft=moveRight=moveDownwards=false;
             currentDirection="北";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         else if(moveLeft)
         {
             moveDownwards=true;
             moveLeft=moveRight=moveUpwards=false;
             currentDirection="南";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
             
         }
         else if(moveUpwards)
       {
             moveLeft=true;
             moveRight=moveUpwards=moveDownwards=false;
             currentDirection="西";
             System.out.printf("朝向已更改为:%s%n",currentDirection);;
         }
         else if(moveDownwards)
         {
             moveRight=true;
             moveUpwards=moveLeft=moveDownwards=false;
             currentDirection="东";
             System.out.printf("朝向已更改为:%s%n",currentDirection);
         }
         
         break;
     case 5:  //向前移动
         if (moveRight)
             {
             currentDirection="东";
             if (penLocation)
                 for (int j=column;j<column+pace;j++)
                     positions[row][j]=1;
             column+=pace;
             System.out.printf("已向前移动%d步,当前位置:【%d,%d】,当前朝向:东%n",pace, row+1, column+1);
                          }
         else if(moveLeft)
        {currentDirection="西";;
             if (penLocation)
                 for (int j=column;j>column-pace;j--)
                     positions[row][j]=1;
             column-=pace;
             System.out.printf("已向前移动%d步,当前位置:[%d,%d],当前朝向:西%n",pace, row+1, column+1);
         }
         else if(moveDownwards)
         {currentDirection="南";;
             if (penLocation)
              for (int i=row;i<row+pace;i++)	//已重写
                     positions[i][column]=1;
                 row+=pace;
                 System.out.printf("已向前移动%d步,当前位置:[%d,%d],当前朝向:南%n",pace, row+1, column+1);
                 }
         else if(moveUpwards)
        {currentDirection="北";;
             if (penLocation)
                 for (int i=row;i>row-pace;i--)
                     positions[i][column]=1;
                 row-=pace;
                 System.out.printf("已向前移动%d步,当前位置:[%d,%d],当前朝向:北%n",pace, row+1, column+1);
         }
         break;
     case 6://print turtle graphics,打印图形
         for (int i=0;i<20;i++)
             {
             for (int j=0;j<20;j++)
             {
             if (positions[i][j]==0)
                 System.out.print("-");
             else
            	 System.out.print("@");
             }
             System.out.print("\n");
             }
         System.out.print("\n\n");
         break;
     case 7://clear turtle graphics,擦除当前图形
         for (int i=0;i<20;i++)	//重置数组
             for(int j=0;j<20;j++)
                 positions[i][j]=0;
         turtleBehaviour(6);  //绘制空白画板
         row = column = 0; //重置画笔位置为画板左上角
         moveRight = true; //重置移动方向
         System.out.println("图形已擦除。");
         break;
     case 9://terminate the program
    	 System.out.println("绘制完成!");
         break;
     
     }

        
     }

 }

彩蛋:

如果要输出以下图形,

 

对应的指令为:

指令 步长
5 3
3  
5 2
4  
2  
5 5
3  
5 1
3  
1  
5 1
2  
4  
5 1
3  
1  
5 1
4  
2  
5 1
3  
1  
5 1
4  
2  
5 1
3  
1  
5 1
4  
2  
5 1
3  
1  
5 1
4  
2  
5 1
3  
1  
5 1
4  
2  
5 1

可见,带拐弯的图形输出指令比较冗长,比较耗时。 可见,这个海龟还有待进化!

猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/85283893