首页 > 编程语言 >游戏类C程序设计

游戏类C程序设计

时间:2023-02-11 20:33:28浏览次数:46  
标签:ch cout int 游戏类 maze 程序设计 include mx

1 迷宫游戏

1.1 MAZE1.0

#include <iostream>
using namespace std;
int main()
{
    int x=1,y=2,mx=10,my=9;
    char ch;
    cout<<"ready!\n";
    while(true)
    {
        cout<<"draw!\n";
        cin>>ch;
        switch(ch)
        {
            case 'L':
            case 'l':
                y--;
                cout<<"turn left:"<<x<<" "<<y<<endl;
                break;
            case 'R':
            case 'r':
                y++;
                cout<<"turn right:"<<x<<" "<<y<<endl;
                break;
            case 'U':
            case 'u':
                x--;
                cout<<"turn up:"<<x<<" "<<y<<endl;
                break;
            case 'D':
            case 'd':
                x++;
                cout<<"turn down:"<<x<<" "<<y<<endl;
                break;
            default:
                cout<<"press error:"<<x<<" "<<y<<endl;
        }
        if(mx==x && my==y) break;
    }
    cout<<"success!";
    return 0;
}

1.2 MAZE2.0

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define N 20
int x=1,y=1;
void Ready(int maze[][N])
{
    int i,j,x,y;
    for(i=0;i<N;i++)
        for(j=0;j<N;j++)
            maze[i][j]=1; //1����ǽ
    srand((unsigned)time(NULL));//����������ӷ���������ʱ����������
    for(int i=0;i<=N*N;i++)
    {
        x=rand()%(N-2)+1;
        y=rand()%(N-2)+1;
        maze[x][y]=0;  //0����·
    }
    maze[1][1]=2;//2������վ����λ��
    maze[N-2][N-2]=0;
    maze[0][1]=0;//���
    maze[N-1][N-2]=0;//����
}
void Draw(int maze[][N])
{
    int x,y;
    system("cls");//����
    cout<<endl;
    for(x=0;x<N;x++)
    {
        for(y=0;y<N;y++)
        {
            if(maze[x][y]==0){cout<<"  ";continue;}//ͨ·
            if(maze[x][y]==1){cout<<"��";continue;}//ǽ
            if(maze[x][y]==2){cout<<"��";continue;}//��ǰվ��λ��
        }
        cout<<endl;
    }
}
void JudgeGo(int maze[][N],char ch)
{
    switch(ch)
    {
        case 'L':
        case 'l':
            if(maze[x][y-1]==0) y--;
            break;
        case 'R':
        case 'r':
            if(maze[x][y+1]==0) y++;
            break;
        case 'U':
        case 'u':
            if(maze[x-1][y]==0) x--;
            break;
        case 'D':
        case 'd':
            if(maze[x+1][y]==0) x++;
            break;
    }
    maze[x][y]=2;
}
int main()
{
    int maze[N][N],mx=N-1,my=N-2;
    char ch;
    Ready(maze);
    while(1)
    {
        Draw(maze);
        cin>>ch;
        JudgeGo(maze,ch);
        if(mx==x && my==y) break;
    }
    cout<<"success!";
    return 0;
}

1.3 MAZE3.0

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define N 20
struct Node
{
    int x;
    int y;
    int direction;
}M[N*N];
int step=0;
int x=1,y=1;
void Ready(int maze[][N])
{
    int i,j,x,y;
    for(i=0;i<N;i++)
        for(j=0;j<N;j++)
            maze[i][j]=1; //1����ǽ
    srand((unsigned)time(NULL));//����������ӷ���������ʱ����������
    for(int i=0;i<=N*N;i++)
    {
        x=rand()%(N-2)+1;
        y=rand()%(N-2)+1;
        maze[x][y]=0;  //0����·
    }
    maze[1][1]=2;//2������վ����λ��
    maze[N-2][N-2]=0;
    maze[0][1]=4;//���
    maze[N-1][N-2]=0;//����
    M[step].x=0;
    M[step].y=1;
    M[step].direction=4;
}
void Draw(int (*p)[N])
{
    int x,y;
    system("cls");//����
    cout<<endl;
    for(x=0;x<N;x++)
    {
        for(y=0;y<N;y++)
        {
            if(*(*(p+x)+y)==0){cout<<"  ";continue;}//ͨ·
            if(*(*(p+x)+y)==1){cout<<"��";continue;}//ǽ
            if(*(*(p+x)+y)==2){cout<<"��";continue;}//��ǰվ��λ��
            if(*(*(p+x)+y)==3){cout<<"��";continue;}//������
            if(*(*(p+x)+y)==4){cout<<"��";continue;}//������
            if(*(*(p+x)+y)==5){cout<<"��";continue;}//������
            if(*(*(p+x)+y)==6){cout<<"��";continue;}//������
        }
        cout<<endl;
    }
}
void JudgeGo(int maze[][N],char ch)
{
    switch(ch)
    {
        case 'L':
        case 'l':
            if(maze[x][y-1]==0)
            {
                maze[x][y]=5;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                y--;
            }
            else if(maze[x][y-1]==6)  //��
            {
                maze[x][y]=0;
                step--;
                y--;
            }
            else if(maze[x][y-1]==3||maze[x][y-1]==4){  // ����
                maze[x][y]=0;
                y--;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
            }
            break;
        case 'R':
        case 'r':
            if(maze[x][y+1]==0)
            {
                maze[x][y]=6;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                y++;
            }
            else if(maze[x][y+1]==5)  //��
            {
                maze[x][y]=0;
                step--;
                y++;
            }
            else if(maze[x][y+1]==3||maze[x][y+1]==4){  // ����
                maze[x][y]=0;
                y++;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
            }
            break;
        case 'U':
        case 'u':
            if(maze[x-1][y]==0)
            {
                maze[x][y]=3;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                x--;
            }
            else if(maze[x-1][y]==4)  //��
            {
                maze[x][y]=0;
                step--;
                x--;
            }
            else if(maze[x-1][y]==5||maze[x-1][y]==6){  // ����
                maze[x][y]=0;
                x--;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
            }
            break;
        case 'D':
        case 'd':
            if(maze[x+1][y]==0)
            {
                maze[x][y]=4;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                x++;
            }
            else if(maze[x+1][y]==3)  //��
            {
                maze[x][y]=0;
                step--;
                x++;
            }
            else if(maze[x+1][y]==5||maze[x+1][y]==6){  // ����
                maze[x][y]=0;
                x++;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
            }
            break;
    }
    maze[x][y]=2;
}
int main()
{
    int maze[N][N];
    int mx=N-1,my=N-2;
    char ch;
    Ready(maze);
    while(1)
    {
        Draw(maze);
        cin>>ch;
        JudgeGo(maze,ch);
        if(mx==x && my==y) break;
    }
    cout<<"success!"<<endl;
    return 0;
}

2 贪吃蛇

2.1 SNAKE1.0

#include <iostream>
using namespace std;
int main()
{
    int x=1,y=2,mx=10,my=9;
    char ch;
    char flag;
    cout<<"ready!\n";
    while(true)
    {
        cout<<"draw!\n";
        cin>>ch;
        switch(ch)
        {
            case 'L':
            case 'l':
                if(flag=='R') continue;
                y--;
                cout<<"turn left:"<<x<<" "<<y<<endl;
                flag = 'L';
                break;
            case 'R':
            case 'r':
                if(flag=='L') continue;
                y++;
                cout<<"turn right:"<<x<<" "<<y<<endl;
                flag = 'R';
                break;
            case 'U':
            case 'u':
                if(flag=='D') continue;
                x--;
                cout<<"turn up:"<<x<<" "<<y<<endl;
                flag = 'U';
                break;
            case 'D':
            case 'd':
                if(flag=='U') continue;
                x++;
                cout<<"turn down:"<<x<<" "<<y<<endl;
                flag = 'D';
                break;
            default:
                cout<<"press error:"<<x<<" "<<y<<endl;
        }
        if(mx==x && my==y) break;
    }
    cout<<"success!";
    return 0;
}

2.2 SNAKE2.0

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define N 20
int x=N/2,y=N/2;
void Ready(int maze[][N], int mx[])
{
    int i,j;
    for(i=0;i<N;i++){
        for(j=0;j<N;j++){
            if(i==0||j==0||i==N-1||j==N-1){
                maze[i][j]=1; //1代表墙
            }
            else{
                maze[i][j]=0; //0代表路
            }
        }
    }
    maze[N/2][N/2]=2;//2代表蛇的位置
    srand((unsigned)time(NULL));//随机函数种子发生器(以时间做参数)
    mx[0]=rand()%(N-2)+1;
    mx[1]=rand()%(N-2)+1;
    maze[mx[0]][mx[1]]=3;//3代表食物的位置
}
void Draw(int maze[][N])
{
    int x,y;
    system("cls");//清屏
    cout<<endl;
    for(x=0;x<N;x++)
    {
        for(y=0;y<N;y++)
        {
            if(maze[x][y]==0){cout<<"  ";continue;}//通路
            if(maze[x][y]==1){cout<<"■";continue;}//墙
            if(maze[x][y]==2){cout<<"※";continue;}//当前蛇的位置
            if(maze[x][y]==3){cout<<"○";continue;}//食物位置 
        }
        cout<<endl;
    }
}
void JudgeGo(int maze[][N],char ch, char flag[])
{
    switch(ch)
    {
        case 'L':
        case 'l':
            if(flag[0]=='R') break;
            if(maze[x][y-1]==0||maze[x][y-1]==3){
                maze[x][y]=0;
                y--;
                flag[0] = 'L';
            }
            break;
        case 'R':
        case 'r':
            if(flag[0]=='L') break;
            if(maze[x][y+1]==0||maze[x][y+1]==3){
                maze[x][y]=0;
                y++;
                flag[0] = 'R';
            }
            break;
        case 'U':
        case 'u':
            if(flag[0]=='D') break;
            if(maze[x-1][y]==0||maze[x-1][y]==3){
                maze[x][y]=0;
                x--;
                flag[0] = 'U';
            }
            break;
        case 'D':
        case 'd':
            if(flag[0]=='U') break;
            if(maze[x+1][y]==0||maze[x+1][y]==3){
                maze[x][y]=0;
                x++;
                flag[0] = 'D';
            }
            break;
    }
    maze[x][y]=2;
}
int main()
{
    int maze[N][N],mx[2];
    char ch;char flag[1]={'T'};
    Ready(maze, mx);
    while(1)
    {
        Draw(maze);
        cin>>ch;
        JudgeGo(maze,ch,flag);
        if(mx[0]==x && mx[1]==y) break;
    }
    cout<<"success!";
    return 0;
}

2.3 SNAKE3.0

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define N 20
struct Node
{
    int x;
    int y;
    int direction;
}M[N*N];
int step=0;
int x=N/2,y=N/2;
void Ready(int maze[][N], int mx[])
{
    int i,j;
    for(i=0;i<N;i++){
        for(j=0;j<N;j++){
            if(i==0||j==0||i==N-1||j==N-1){
                maze[i][j]=1; //1代表墙
            }
            else{
                maze[i][j]=0; //0代表路
            }
        }
    }
    maze[N/2][N/2+1]=5;
    maze[N/2][N/2]=2;//2代表蛇的位置
    srand((unsigned)time(NULL));//随机函数种子发生器(以时间做参数)
    mx[0]=rand()%(N-2)+1;
    mx[1]=rand()%(N-2)+1;
    maze[mx[0]][mx[1]]=10;//10代表食物的位置
    M[step].x=N/2;
    M[step].y=N/2+1;
    M[step].direction=5;
}
void Draw(int (*p)[N])
{
    int x,y;
    system("cls");//清屏
    cout<<endl;
    for(x=0;x<N;x++)
    {
        for(y=0;y<N;y++)
        {
            if(*(*(p+x)+y)==0){cout<<"  ";continue;}//通路
            if(*(*(p+x)+y)==1){cout<<"■";continue;}//墙
            if(*(*(p+x)+y)==2){cout<<"※";continue;}//当前蛇的位置
            if(*(*(p+x)+y)==10){cout<<"○";continue;}//食物位置 
            if(*(*(p+x)+y)==3){cout<<"↑";continue;}//向上走
            if(*(*(p+x)+y)==4){cout<<"↓";continue;}//向下走
            if(*(*(p+x)+y)==5){cout<<"←";continue;}//向左走
            if(*(*(p+x)+y)==6){cout<<"→";continue;}//向右走
        }
        cout<<endl;
    }
}
void JudgeGo(int maze[][N],char ch, char flag[])
{
    switch(ch)
    {
        case 'L':
        case 'l':
            if(flag[0]=='R') break;
            if(maze[x][y-1]==0||maze[x][y-1]==10){
                maze[x][y]=5;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                y--;
                flag[0] = 'L';
            }
            else if(maze[x][y-1]==3||maze[x][y-1]==4){  // 上下
                maze[x][y]=0;
                y--;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
                flag[0] = 'L';
            }
            break;
        case 'R':
        case 'r':
            if(flag[0]=='L') break;
            if(maze[x][y+1]==0||maze[x][y+1]==10){
                maze[x][y]=6;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                y++;
                flag[0] = 'R';
            }
            else if(maze[x][y+1]==3||maze[x][y+1]==4){  // 上下
                maze[x][y]=0;
                y++;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
                flag[0] = 'R';
            }
            break;
        case 'U':
        case 'u':
            if(flag[0]=='D') break;
            if(maze[x-1][y]==0||maze[x-1][y]==10){
                maze[x][y]=3;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                x--;
                flag[0] = 'U';
            }
            else if(maze[x-1][y]==5||maze[x-1][y]==6){  // 左右
                maze[x][y]=0;
                x--;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
                flag[0] = 'U';
            }
            break;
        case 'D':
        case 'd':
            if(flag[0]=='U') break;
            if(maze[x+1][y]==0||maze[x+1][y]==10){
                maze[x][y]=4;
                step++;
                M[step].x=x;
                M[step].y=y;
                M[step].direction=maze[x][y];
                x++;
                flag[0] = 'D';
            }
            else if(maze[x+1][y]==5||maze[x+1][y]==6){  // 左右
                maze[x][y]=0;
                x++;
                while(1){
                    maze[M[step].x][M[step].y]=0;
                    step--;
                    if(M[step].x==x&&M[step].y==y){step--;break;}
                }
                flag[0] = 'D';
            }
            break;
    }
    maze[x][y]=2;
}
int main()
{
    int maze[N][N],mx[2];
    char ch;char flag[1]={'T'};
    Ready(maze, mx);
    while(1)
    {
        Draw(maze);
        cin>>ch;
        JudgeGo(maze,ch,flag);
        if(mx[0]==x && mx[1]==y) break;
    }
    cout<<"success!";
    return 0;
}

标签:ch,cout,int,游戏类,maze,程序设计,include,mx
From: https://www.cnblogs.com/yymqdu/p/17112505.html

相关文章