貪喫蛇設計與算法

我覺得在這個遊戲中,最難的就是move函數了,打得很艱難,不過最後終於在改了很多次後成功打出來了。如下:

void move(char a)
{
    int j1 = sx[0], j2 = sy[0];
    if (foodnum == 0){
        foodx = rand() % 10 + 1;
        foody = rand() % 10 + 1;
        while (map[foodx][foody] != ' '){
            foodx = rand() % 10 + 1;
            foody = rand() % 10 + 1;
        }
        map[foodx][foody] = '$';
        foodnum = 1;
    }
    switch (a){
    case 'A':case 'a':
        sy[0]--;
        break;
    case 'D':case 'd':
        sy[0]++;
        break;
    case 'W':case 'w':
        sx[0]--;
        break;
    case 'S':case 's':
        sx[0]++;
        break;
    }
    if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){
        zhixing = 0;
        return;
    }
    if (map[sx[0]][sy[0]] == ' '){
        map[sx[len - 1]][sy[len - 1]] = ' ';
        for (i = len - 2; i >= 1; i--){
            sx[i + 1] = sx[i];
            sy[i + 1] = sy[i];
        }
        sx[1] = j1, sy[1] = j2;
        map[sx[0]][sy[0]] = 'H';
        for (i = 1; i <= len - 1; i++){
            map[sx[i]][sy[i]] = 'X';
        }
    }
    if (map[sx[0]][sy[0]] == '$'){
        foodnum = 0;
        len++;
        for (i = len - 2; i >= 1; i--){
            sx[i + 1] = sx[i], sy[i + 1] = sy[i];
        }
        sx[1] = j1, sy[1] = j2;
        map[sx[0]][sy[0]] = 'H';
        for (i = 1; i <= len - 1; i++){
            map[sx[i]][sy[i]] = 'X';
        }
    }
}

不過我的和別人的不太一樣,我是把食物的代碼也放進了move函數中。
下面是我的wheregonext函數(超低配智能):

char wheregonext(void)
{
    int heng, shu;
    heng = abs(sy[0] - foody);
    shu = abs(sx[0] - foodx);
    if (heng >= shu){
        if (sy[0] - foody > 0){
            return 'a';
        }
        else{
            return 'd';
        }
    }
    if (heng < shu){
        if (sx[0] - foodx > 0){
            return 'w';
        }
        else{
            return 's';
        }
    }
}

在寫的時候可以用自頂向下的方法,先寫好總控代碼,再分別寫各個函數,下面是全部代碼:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<Windows.h>
char map[12][13] = {
    "************",
    "*XXXXH     *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "*          *",
    "************"
};
int sx[100] = { 1, 1, 1, 1, 1 }, sy[100] = { 5, 4, 3, 2, 1 };
int foodnum = 0, zhixing = 1, len = 5;
int i, foodx, foody;
void move(char a);
char wheregonext(void);
int main(void)
{
    char  c;
    srand(time(NULL));
    foodx = rand() % 10 + 1;
    foody = rand() % 10 + 1;
    while (map[foodx][foody] != ' '){
        foodx = rand() % 10 + 1;
        foody = rand() % 10 + 1;
    }
    map[foodx][foody] = '$';
    foodnum = 1;
    for (i = 0; i <= 11; i++){
        printf("%s\n", map[i]);
    }
    while (zhixing){
        c = wheregonext();
        move(c);
        system("cls");
        Sleep(100);
        for (i = 0; i <= 11 && zhixing == 1; i++){
            printf("%s\n", map[i]);
        }
    }
    printf("Game over!\n");
    return 0;
}
void move(char a)
{
    int j1 = sx[0], j2 = sy[0];
    if (foodnum == 0){
        foodx = rand() % 10 + 1;
        foody = rand() % 10 + 1;
        while (map[foodx][foody] != ' '){
            foodx = rand() % 10 + 1;
            foody = rand() % 10 + 1;
        }
        map[foodx][foody] = '$';
        foodnum = 1;
    }
    switch (a){
    case 'A':case 'a':
        sy[0]--;
        break;
    case 'D':case 'd':
        sy[0]++;
        break;
    case 'W':case 'w':
        sx[0]--;
        break;
    case 'S':case 's':
        sx[0]++;
        break;
    }
    if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){
        zhixing = 0;
        return;
    }
    if (map[sx[0]][sy[0]] == ' '){
        map[sx[len - 1]][sy[len - 1]] = ' ';
        for (i = len - 2; i >= 1; i--){
            sx[i + 1] = sx[i];
            sy[i + 1] = sy[i];
        }
        sx[1] = j1, sy[1] = j2;
        map[sx[0]][sy[0]] = 'H';
        for (i = 1; i <= len - 1; i++){
            map[sx[i]][sy[i]] = 'X';
        }
    }
    if (map[sx[0]][sy[0]] == '$'){
        foodnum = 0;
        len++;
        for (i = len - 2; i >= 1; i--){
            sx[i + 1] = sx[i], sy[i + 1] = sy[i];
        }
        sx[1] = j1, sy[1] = j2;
        map[sx[0]][sy[0]] = 'H';
        for (i = 1; i <= len - 1; i++){
            map[sx[i]][sy[i]] = 'X';
        }
    }
}
char wheregonext(void)
{
    int heng, shu;
    heng = abs(sy[0] - foody);
    shu = abs(sx[0] - foodx);
    if (heng >= shu){
        if (sy[0] - foody > 0){
            return 'a';
        }
        else{
            return 'd';
        }
    }
    if (heng < shu){
        if (sx[0] - foodx > 0){
            return 'w';
        }
        else{
            return 's';
        }
    }
}

下面是在運行的程序:
這裏寫圖片描述
歡迎大家指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章