• 1327403062(张端风)-实验2


    小乌龟题目


    题目描述

    1.创建turtle和beach类对象,使turtle能在beach上自由移动。
    2.可以记录小乌龟的行走轨迹。

    流程图

    1.设置小乌龟坐标,设置沙滩大小
    2.方向键控制小乌龟移动
    3.记录小乌龟移动轨迹

    算法与函数

    1.用方向键字节码对小乌龟进行实时移动
    2.用清屏和刷新重新绘图
    3.用二维数组记录小乌龟行走痕迹

    代码

    /*
    	作业:小乌龟作业
             运行环境:Windows
           注:代码中运用windows.h头文件中的清屏函数,所以只能在windows平台运行。
    	                          作者:张端风
                                        	2014.11.06
    */
    #include<iostream>
    #include<iomanip>
    #include<string.h>
    #include<windows.h>
    #include<conio.h>
    using namespace std;
    
    int judge, tail[100][100];
    class Turtle
    {
    public:
    	int x, y, height,width;
    	Turtle(int a, int b, int c, int d)
    	{
    		x = a;
    		y = b;
    		height = d;
    		width = c;
    	}
    	void up()
    	{
    		if(y>1)
    			y--;
    	}
    	void down()
    	{
    		if(y < height)
    			y++;
    	}
    	void left()
    	{
    		if(x>1)
    			x--;
    	}
    	void right()
    	{
    		if(x<width)
    			x++;
    	}
    };
    class Panel
    {
    public:
    	int height, width;
    	Panel( int x, int y)
    	{
    		height = y;
    		width = x;
    	}
    };
    void move(Turtle *t,char c)
    {
            switch(c){
            case 72: (*t).up();break;     // 向上
            case 80: (*t).down();break;      // 向下
            case 75: (*t).left();break;      // 向左
            case 77: (*t).right();           // 向右
            }
    }
    void refresh(Panel p, Turtle t)
    {
    	int x,y,width,height;
    	x = t.x; y = t.y;
    	width = p.width; height = p.height;
    	tail[y][x] = 1;
    	for(int i = 0; i <= 2 * width +1; i++)
    		cout << '=';
    	cout << endl;
    	for(int i = 1; i <= height; i++)
    	{
    		cout << '=';
    		for(int j = 1; j <=width; j++)
    			if( judge && tail[i][j] == 1 && (i!=y || j !=x))
    				cout << " .";
    			else if (i == y && j == x )
    				cout << " H";
    			else
    				cout << "  ";
    		cout << '=' <<  endl;
    	}
    	for(int i = 0; i <= 2*width+1; i++)
    		cout << '=';
    	cout << endl ;
    	cout << "使用说明 :"<<endl;
    	if(judge)
    		cout << "1.乌龟行走留下痕迹。"<<endl;
    	else
    		cout << "1.乌龟行走不留下痕迹。"<<endl;
    	cout << "2.H代表小乌龟,请使用方向键控制小乌龟";
    }
    int main()
    {
    	int height = 20, width = 30;
    	memset(tail,0,sizeof(tail));
    	char c;
    	/*cout<< "Pleasse enter the width and height of panel:"<<endl;
    	cin >> width >> height;*/
    	Panel p(width,height);
    	Turtle t(3,3,width,height);
    	cout << "是否留下小乌龟行走痕迹? 1:留 0:不留 " << endl;
    	cin>>judge;
    	system("cls");
    	refresh(p,t);
    	while(1)
    	{
    		getch();c = getch();
    		move(&t,c);
    		system("cls");
    		refresh(p,t);
    	}
    }
    
  • 相关阅读:
    UI
    OC之block
    web前端开发学习
    OC面向对象下之文件
    UIButton
    视图
    frame和bounds
    UIView
    UIWindow
    Hello friends!
  • 原文地址:https://www.cnblogs.com/cyno/p/4078351.html
Copyright © 2020-2023  润新知