C中的蛇游戏关于switch语句

时间:2014-12-26 01:18:49

标签: c

这是一个简单的游戏,只是为了输入一个角色,移动蛇并每次打印桌子。 主函数中的switch语句在没有默认值的情况下解决4个案例''d''w'但是,当程序运行时,如果输入了一个字符,则程序仍然会做出反应并输出桌子(此外,桌子输出两次)。为什么? 此外,它还让我感到困惑的是,如果输入是在蛇的情况下蛇不能正确移动。

忘记在以前的情况下添加'break'之后:)但问题仍然让我困惑。现在如果我默认使用'break',程序仍会打印表两次,如果我使用'continue'默认情况下,程序不会对错误的输入作出反应。 但是蛇仍然无法正确移动。:(

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void output(void);
void gameover(void);
char map[12][12] = 
    {"************",
    "*XXXXH     *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
    char ch;
    srand(time(NULL));
    put_money();
    output();
    origin=0;
    while(game) {
        scanf("%c",&ch);
        switch(ch) {
            case 'w':
                snakeMove(1,1);
                break;
            case 's':
                snakeMove(2,1);
                break;
            case 'a':
                snakeMove(3,1);
                break;
            case 'd':
                snakeMove(4,1);
                break;
            default:
                continue;
        }
        if(eaten==1) {
            put_money();
            eaten=0;
        }
        output();
    }
    gameover();
    return 0;
}
void snakeMove(int direction, int distance) {
    int i,move=1;
    switch(direction) {
        case 1:
            if(snakeY[3]==snakeY[4]+distance)
                move=0;
            else
                snakeY[4]=snakeY[4]+distance;
            break;
        case 2:
            if(snakeY[3]==snakeY[4]-distance)
                move=0;
            else
                snakeY[4]=snakeY[4]-distance;
            break;
        case 3:
            if(snakeX[3]==snakeX[4]-distance)
                move=0;
            else
                snakeX[4]=snakeX[4]-distance;
            break;
        case 4:
            if(snakeX[3]==snakeX[4]+distance)
                move=0;
            else
                snakeX[4]=snakeX[4]+distance;
    }
    if (move==1) {
        for(i=3;i>=0;i--){
            snakeX[i]=snakeX[i+1];
            snakeY[i]=snakeY[i+1];
        }
    }
}
void output(void) {
    int x, y,i;
    if(origin==0) {
        for(x=1;x<11;x++) {
            for(y=1;y<11;y++) {
                if(map[x][y]!='$')
                    map[x][y]=' ';
                }
        }
        if(map[snakeX[4]][snakeY[4]]=='$')
            eaten=1;
        for(i=0;i<4;i++)
            map[snakeX[i]][snakeY[i]]='X';
        map[snakeX[4]][snakeY[4]]='H';
        if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
            game=0;
        for(i=0;i<4;i++) {
            if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
                game=0;
        }
    }
    for(x=0;x<12;x++) {
        for(y=0;y<12;y++)
            printf("%c",map[x][y]);
        printf("\n");
    }
}
void gameover(void) {
    printf("Game Over!!!\n");
    exit(0);
}
void put_money(void) {
    int foodx,foody,done=0;
    while(done==0) {
        foodx=rand()%12;
        foody=rand()%12;
        if(map[foodx][foody]==' ') {
            map[foodx][foody]='$';
            done=1;
        }
    }


}

1 个答案:

答案 0 :(得分:1)

有趣。你的switch语句很好。这是一个带有更改注释的工作代码,后跟一个完整的代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_snake(void);
void output(void);
void gameover(void);
char map[12][12] = 
    {"************",
    "*XXXXH     *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;

int main() {
        char ch;
        srand(time(NULL));
        put_money();
        output();
        origin=0;
        while(game) {

正如Cool guy所说,scanf中的空间有效。这是因为当您按Enter键时,换行符将被计为字符。

                scanf(" %c",&ch);
                switch(ch) {
                case 'w':
                        snakeMove(1,1);
                        break;
                case 's':
                        snakeMove(2,1);
                        break;
                case 'a':
                        snakeMove(3,1);
                        break;
                case 'd':
                        snakeMove(4,1);
                        break;
                default:
                        continue;
                }
                if(eaten==1) {
                        put_money();
                        eaten=0;
                }
                output();
        }
        gameover();
        return 0;
}

此更新功能已添加,因为您需要在更新磁头之前更新蛇。否则,当您尝试将其发回时,您将丢失原始头部。

void update_body()
{
        int i;
        for(i=0;i<4;i++){
                snakeX[i]=snakeX[i+1];
                snakeY[i]=snakeY[i+1];
        }

}
void snakeMove(int direction, int distance) {
        int move=1;

        switch(direction) {
        case 1:

第三个问题是在图形中,原点(0,0)位于左上角而不是左下角。这意味着上升,你必须减去并下降你必须添加。

                if(snakeY[3]==snakeY[4]-distance)
                        move=0;
                else{
                        update_body();
                        snakeY[4]=snakeY[4]-distance;
                }
                break;
        case 2:
                if(snakeY[3]==snakeY[4]+distance)
                        move=0;
                else{
                        update_body();
                        snakeY[4]=snakeY[4]+distance;
                }
                break;
        case 3:
                if(snakeX[3]==snakeX[4]-distance)
                        move=0;
                else{
                        update_body();
                        snakeX[4]=snakeX[4]-distance;
                }
                break;
        case 4:
                if(snakeX[3]==snakeX[4]+distance)
                        move=0;
                else{
                        update_body();
                        snakeX[4]=snakeX[4]+distance;
                }
        }

}
void output(void) {
        int x, y,i;
        if(origin==0) {
                for(x=1;x<11;x++) {
                        for(y=1;y<11;y++) {
                                if(map[x][y]!='$')
                                        map[x][y]=' ';
                        }
                }

最后一个问题是地图是颠倒的。这意味着X和Y坐标分别为第1和第2,它们应该是第2和第1。

                if(map[snakeY[4]][snakeX[4]]=='$')
                        eaten=1;
                for(i=0;i<4;i++)
                        map[snakeY[i]][snakeX[i]]='X';
                map[snakeY[4]][snakeX[4]]='H';
                if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
                        game=0;
                for(i=0;i<4;i++) {
                        if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
                                game=0;
                }
        }
        for(x=0;x<12;x++) {
                for(y=0;y<12;y++)
                        printf("%c",map[x][y]);
                printf("\n");
        }
}
void gameover(void) {
        printf("Game Over!!!\n");
        exit(0);
}
void put_money(void) {
        int foodx,foody,done=0;
        while(done==0) {
                foodx=rand()%12;
                foody=rand()%12;
                if(map[foodx][foody]==' ') {
                        map[foodx][foody]='$';
                        done=1;
                }
        }
}

这是整个代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_body();
void output(void);
void gameover(void);
char map[12][12] =
{"************",
 "*XXXXH     *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "*          *",
 "************"};

int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
        char ch;
        srand(time(NULL));
        put_money();
        output();
        origin=0;
        while(game) {
                scanf(" %c",&ch);
                switch(ch) {
                case 'w':
                        snakeMove(1,1);
                        break;
                case 's':
                        snakeMove(2,1);
                        break;
                case 'a':
                        snakeMove(3,1);
                        break;
                case 'd':
                        snakeMove(4,1);
                        break;
                default:
                        continue;
                }
                if(eaten==1) {
                        put_money();
                        eaten=0;
                }
                output();
        }
        gameover();
        return 0;
}
void update_body()
{
        int i;
        for(i=0;i<4;i++){
                snakeX[i]=snakeX[i+1];
                snakeY[i]=snakeY[i+1];
        }

}
void snakeMove(int direction, int distance) {
        int move=1;
        switch(direction) {
        case 1:
                if(snakeY[3]==snakeY[4]-distance)
                        move=0;
                else{
                        update_body();
                        snakeY[4]=snakeY[4]-distance;
                }
                break;
        case 2:
                if(snakeY[3]==snakeY[4]+distance)
                        move=0;
                else{
                        update_body();
                        snakeY[4]=snakeY[4]+distance;
                }
                break;
        case 3:
                if(snakeX[3]==snakeX[4]-distance)
                        move=0;
                else{
                        update_body();
                        snakeX[4]=snakeX[4]-distance;
                }
                break;
        case 4:
                if(snakeX[3]==snakeX[4]+distance)
                        move=0;
                else{
                        update_body();
                        snakeX[4]=snakeX[4]+distance;
                }
        }

}
void output(void) {
        int x, y,i;
        if(origin==0) {
                for(x=1;x<11;x++) {
                        for(y=1;y<11;y++) {
                                if(map[x][y]!='$')
                                        map[x][y]=' ';
                        }
                }
                if(map[snakeY[4]][snakeX[4]]=='$')
                        eaten=1;
                for(i=0;i<4;i++)
                        map[snakeY[i]][snakeX[i]]='X';
                map[snakeY[4]][snakeX[4]]='H';
                if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
                        game=0;
                for(i=0;i<4;i++) {
                        if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
                                game=0;
                }
        }
        for(x=0;x<12;x++) {
                for(y=0;y<12;y++)
                        printf("%c",map[x][y]);
                printf("\n");
        }
}
void gameover(void) {
        printf("Game Over!!!\n");
        exit(0);
}
void put_money(void) {
        int foodx,foody,done=0;
        while(done==0) {
                foodx=rand()%12;
                foody=rand()%12;
                if(map[foodx][foody]==' ') {
                        map[foodx][foody]='$';
                        done=1;
                }
        }
}