c++海龟图

#include <iostream>
#include <string>
using namespace std;
void head();//游戏开始函数
void isorno(int [],int floor[20][20],int,bool&prinf);
void print(int floor[20][20]);//打印20*20的二维数组
int main()
{
    head();
    char a[100]= {'0'};//原命令的数组
    int order[100]= {0};//新命令的数组
    int counter=0;//字符型数组的长度
    int oderL=0;//数值型数组的长度
    string num;//用于接收输入命令
    int floor[20][20]= {0};
    bool prinf;
    while(true)//获取字符型数组
    {
        cin>>num;
        if(num[0] =='9')
        {
            break;
        }
        else
        {
            if(num[0]=='5')
            {
                a[counter]=num[0];
                counter++;
                if(num.size()!=4)
                {
                    string nums = num.substr(num.size()-2,2);
                    a[counter]=nums[0];
                    counter++;
                    a[counter]=nums[1];
                    counter++;
                }
                else
                {
                    string nums = num.substr(num.size()-1,1);
                    a[counter]=nums[0];
                    counter++;
                }
            }
            else
            {
                a[counter]=num[0];
                counter++;
            }


        }


    }
    a[counter] ='9';
    for(int i=0; i<counter+1; i++)//将字符型数组转换为数值型数组
    {
        if((int(a[i])-48)==1)
        {
            order[oderL]=10+(int(a[i+1])-48);
            i++;
            oderL++;
        }
        else
        {
            order[oderL]=int(a[i])-48;
            oderL++;
            //counter2++;
        }


    }
    /*for(int j=0; j<oderL; j++)
    {
        cout << order[j]<<"  ";
    }
    cout<<"\n\n\n\n\n\n";*/
    isorno(order,floor,oderL,prinf);
    print(floor);
    cout<<endl<<endl;
    for(int i=0; i<20; i++)
    {
        for(int j=0; j<20; j++)
        {
            cout<<floor[i][j]<<" ";
        }
        cout << endl;
    }
}
void head()//游戏开始函数
{
    cout<<"_____________________________________________________"<<endl
    <<"命令\t\t"<<"含义"<<endl
    <<"_____________________________________________________"<<endl
    <<"1\t\t"<<"笔尖朝上"<<endl
    <<"2\t\t"<<"笔尖朝下"<<endl
    <<"3\t\t"<<"向右转"<<endl
    <<"4\t\t"<<"向左走"<<endl
    <<"5,10\t\t"<<"向前移动10个格子(或者不是10格)"<<endl
    <<"6\t\t"<<"打印这个20*20的数组"<<endl
    <<"9\t\t"<<"数据结束"<<endl
    <<"_____________________________________________________\n\n"<<endl
    <<"例如打印一个12*12的正方形,结束时笔尖朝上"<<endl
    <<"2\n"<<"5,12\n"<<"3\n"<<"5,12\n"<<"3\n"<<"5,12\n"<<"3\n"<<"5,12\n"<<"1 6 9\n"<<endl
    <<"_____________________________________________________\n\n"<<endl;
}
void isorno(int order[],int floor[20][20],int length,bool&prinf)
{
    string direction ="3";
    int column = 0;
    int rows = 0;
    for(int i=0; i<length; i++)
    {
        switch(order[i])
        {
        case 1:
            prinf = false;//笔尖向上
            break;
        case 2:
            prinf = true;//笔尖向下
            if(direction == "3")
            {
                if(order[i+1]==5)
                {
                    int j;
                    for(j=0; j<order[i+2]; j++)
                    {
                        floor[0][j]=1;


                    }
                    column = j;
                }
            }
            else if(direction =="4")
            {
                if(order[i+1]==5)
                {
                    int p;
                    for(p=0; p<order[i+2]; p++)
                    {
                        floor[p][column]=1;


                    }
                    rows = p;
                }
            }
            break;
        case 3://向右走
            if(direction=="3")
            {
                //cout << "*****************"<<column<<endl;
                if(order[i+1]==5)
                {


                    int k;
                    for(k=rows; k<rows+order[i+2]; k++)
                    {
                        floor[k][column]=1;


                    }
                    rows = k;
                    direction = "4";
                }


            }
            else if(direction=="4")
            {
                if(order[i+1]==5)
                {
                    int m;
                    for(m=column; m>=column-order[i+2]; m--)
                    {
                        floor[rows][m]=1;


                    }
                    column = m+1;
                    direction ="1";
                }
            }
            else if(direction=="1")
            {
                if(order[i+1]==5)
                {
                    int n;
                    for(n=rows; n>=rows-order[i+2]; n--)
                    {
                        floor[n][column]=1;


                    }
                    rows = n;
                    direction="2";
                }
            }
            else
            {
                if(order[i+1]==5)
                {
                    int a;
                    for(a=column; a<column+order[i+2]; a++)
                    {
                        floor[rows][a]=1;


                    }
                    column = a;
                    direction ="1";
                }
            }
            break;
        case 4://向左走
            if(direction=="4")
            {


                if(order[i+1]==5)
                {
                    int q;
                    //cout<<"12313"<<" "<<column<<" "<<order[i+2]<<endl;
                    for(q=column; q<column+order[i+2]; q++)
                    {
                        floor[rows][q]=1;


                    }
                    column = q;
                    //cout<<"12313"<<" "<<column<<endl;
                    direction="3";
                }
            }
            else if(direction=="3")
            {
                if(order[i+1]==5)
                {
                    int b;
                    for(b=rows; b>=rows-order[i+2]; b--)
                    {
                        floor[b][column]=1;


                    }
                    rows = b+1;
                    direction="2";
                }
            }
            else if(direction=="2")
            {
                if(order[i+1]==5)
                {
                    int c;
                    for(c=column; c>=column-order[i+2]; c--)
                    {
                        floor[rows][c]=1;


                    }
                    column = c+1;
                    direction ="1";
                }
            }
            else
            {
                if(order[i+1]==5)
                {
                    int d;
                    for(d=rows; d<rows+order[i+2]; d++)
                    {
                        floor[d][column]=1;


                    }
                    rows = d;
                    direction = "4";
                }
            }
            break;
        /*case 5:
            if(direction == "3")
            {
                int j;
                for(j=0; j<order[i+1]; j++)
                {
                    floor[0][j]=1;
                }
                column = j;


            }
            else if(direction =="4")
            {


                int p;
                for(p=0; p<order[i+1]; p++)
                {
                    floor[p][column]=1;


                }
                rows = p;
            }
            break;*/
        default:
            break;
        }
    }
}
void print(int floor[20][20])//打印20*20的二维数组
{
    char a;
    cout<<"\n\n please enter the sign\n";
    cin>>a;
    cout<<endl<<endl<<endl;
    for(int i=0; i<20; i++)
    {
        for(int j=0; j<20; j++)
        {
            if(floor[i][j]==1)
            {
                cout<<a<<" ";
            }
            else
            {
                cout<<" "<<" ";
            }
            //cout<<floor[i][j]<<" ";
        }
        cout << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41682681/article/details/80917712