海龟作图(头歌java训练题)

任务要求

设想有一只机械海龟,它在JAVA语言的控制下在屋里四处爬行。海龟拿了一支笔,这只笔或朝上或朝下,当笔朝下时,海龟用笔画下自己的轨迹,当笔朝上时,海龟在移动过程中什么也不画。 使用一个nxn的数组表示屋里n行n列的点,坐标(1,1)表示坐上角,(n,n)表示右下角。数组元素初始化为0,表示海龟没有画下任何内容。从一个装有命令的数组中读取各种命令。不管是笔朝上还是笔朝下,都要跟踪海龟的当前位置。假定海龟总是从地板的左上角,坐标为(1,1),初始方向向右出发,并且开始时笔是朝上的(注意:当笔尖朝下的指令发出后,海龟的标记轨迹不包括当前所在位置的单元格)。程序必须处理的一组命令如下:

指令含义
1 笔朝上
2 笔朝下
3 右转弯
4 左转弯
5 10 向前走10格(或其他的格数,不包括当前格)
6 打印nxn的数组
9 数据结束(标记)

输入格式:

程序的输入包括若干行,第一行输入一个正整数n(n>1),规定矩阵的大小为nxn。接下来每行表示一个命令,当命令代号为5时,其后跟着一个整数,二者之间以空格分开。程序以数字9作为结束标记。

输入样例:

11 2 5 10 3 1 5 2 2 3 5 10 6 9

输出格式

程序的输出是一个nxn的点阵,表示程序结束后,乌龟走过的痕迹。其中乌龟未画下轨迹的地方用空格和减号(‘ -’)表示,乌龟画下痕迹的位置用空格和星号(‘ *’)表示。

输出样例

 输出样例1-4

 

Main.java

package turple;
import java.util.Scanner;
//import java.util.*;
import turple.Turtle;

public class Main {
   public static void main(String[] args) {
           /*
               题目要求:1、请尽可能使用面向对象的程序设计思路
                        2、题目已经建好Turtle.java类文件,在左上角 “代码文件” 目录下。
                           请在turtle.java文件中编写相应属性以及方法。
           */
           Turtle tr = new Turtle();
           @SuppressWarnings("resource")
           Scanner sc = new Scanner(System.in);
           tr.n = sc.nextInt();
           tr.pen = 0;  //start 0
           tr.direct = 1;  //start right
           tr.x = 0;
           tr.y = 0;
           int[][] turtle = tr.init();
           String str;
           do
           {
               str = sc.next();
               if(str.charAt(0) == '5')
               {
            	   str = sc.next();
            	   //System.out.println(str);
                   int a = Integer.parseInt(str);
                   if(tr.pen == 1)
                       tr.paint(turtle,a);
                   tr.update(a);
               }
               else
               {
                   int sym = Integer.parseInt(str);
                   switch(sym)
                   {
                       case 1:
                    	   tr.pen = 0;
                    	   break;
                       case 2:
                    	   tr.pen = 1;
                    	   break;
                       case 3:
                    	   tr.rect(1);
                    	   break;
                       case 4:
                    	   tr.rect(-1);
                    	   break;
                       case 6:
                    	   for(int i=0;i<tr.n;i++)
                           {
                               for(int j=0;j<tr.n;j++)
                               {
                                   if(turtle[i][j] == 0)
                                       System.out.print(" -");
                                   else
                                       System.out.print(" *");
                               }
                               System.out.println();
                           }
                    	   break;
                       case 9:break;
                       default:break;
                   }
               }
               //System.out.println(tr.x+" "+tr.y);
           }
           while(str.charAt(0) != '9');
       }
   }

Turtle.java

package turple;
//import java.util.ArrayList;
//import java.math.*;

public class Turtle {
	/*
    注意:本文件作为海归作图的类文件,用于设置Turtle类属性(包括位置坐标、抬笔/放笔、当前方向等)以及相应方法。
     */
    public int n;
    public int direct;   //right 1 down 2 left 3 up 4 
    public int pen;
    public int x;
    public int y;

    public void update(int len)
    {
    	int a;
        if(direct == 1)
        {
        	if(y+len > n-1)
        		a = n-1-y;
        	else
        		a = len;
        	y += a;
        }
        else if(direct == 2)
        {
        	if(x+len > n-1)
        		a = n-1-x;
        	else
        		a = len;
        	x += a;
        }
        else if(direct == 3)
        {
        	if(y-len < 0)
        		a = y;
        	else
        		a = len;
        	y -= a;
        }
        else 
        {
        	if(x-len < 0)
        		a = x;
        	else
        		a = len;
        	x -= a;
        }
    }
    
    public void paint(int[][] arr,int len)
    {
    	int a;
        if(direct == 1)
        {
        	if(y+1 > n-1)
        		a = y;
        	else if(y+len > n-1)
        		a = n;
        	else
        		a = y+len+1;
            for(int i=y+1;i<a;i++)
                arr[x][i] = 1;
        }
        else if(direct == 2)
        {
        	if(x+1 > n-1)
        		a = x;
        	else if(x+len > n-1)
        		a = n;
        	else
        		a = x+len+1;
            for(int i=x+1;i<a;i++)
                arr[i][y] = 1;
        }
        else if(direct == 3)
        {
        	if(y-1 < 0)
        		a = 0;
        	else if(y-len < 0)
        		a = -1;
        	else
        		a = y-len-1;
            for(int i=y-1;i>a;i--)
                arr[x][i] = 1;
        }
        else 
        {
        	if(x-1 < 0)
        		a = 0;
        	else if(x-len < 0)
        		a = -1;
        	else
        		a = x-len-1;
            for(int i=x-1;i>a;i--)
                arr[i][y] = 1;
        }
    }

    public int[][] init()
    {
        int[][] matrix = new int[n][n];
        for(int i=0;i<n;i++)
        	for(int j=0;j<n;j++)
        		matrix[i][j] = 0;
        return matrix;
    }

    public void rect(int no)   //update direction
    {
        if(no == 1 && direct != 4)
            direct += 1;
        else if(no == 1 && direct == 4)
            direct = 1;
        else if(no == -1 && direct != 1)
            direct -= 1;
        else
            direct = 4;
    }

}

  

运行结果

10
2
5 1
6
 - * - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
1
5 1
6
 - * - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
2
5 3
6
 - * - * * * - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
1
5 2
6
 - * - * * * - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
 - - - - - - - - - -
9

  

posted @ 2021-03-16 11:45  梵蒂冈宝石  阅读(555)  评论(0编辑  收藏  举报