首页 > 代码库 > PlantsVsZombies_v2.0_1

PlantsVsZombies_v2.0_1

第二版,改进很大!!关键是正确了!!下一版,封装结构体。

#include <stdio.h>
#include <string.h>
#include "api.h"
#include "PlantsVsZombies.h"

EndSysInfo sysBaseInfo = {0};

int grassArray[2][10] = {0};//标记草地是否可种植
int zombieLife[2][10] = {3};//标记僵尸的生命值

void main(int argc, char* argv[])
{
    CmdReset(); 
    /* 
    启动Socket服务侦听5555端口(sapi_server_start函数在lib库已实现)。
    lib库已实现从Socket接收到字符串后的命令分发处理;
    */
    api_server_start(argc, argv);
    // 此处不会执行到,注意不要在此处添加代码
}

void CmdReset()
{
    sysBaseInfo.endTime = 0;
    sysBaseInfo.sysSun = 200;
    sysBaseInfo.sysGold = SYS_GOLD_INIT;
    sysBaseInfo.sunflowerNum = 0;
    sysBaseInfo.diedSunflowerNum = 0;
    sysBaseInfo.beanshooterNum = 0;
    sysBaseInfo.diedBeanshooterNum = 0;
    sysBaseInfo.commonZombieNum = 0;
    sysBaseInfo.ironZombieNum = 0;
    sysBaseInfo.diedCommonZombieNum = 0;
    sysBaseInfo.diedIronZombieNum = 0;

    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            grassArray[i][j] = NOTHING;
            zombieLife[i][j] = 3;
        }
    }
    api_defendsys_ret(OP_E_INIT_SUCCESS);
    return;
}

/* 收集阳光*/
void collectSun()
{
    printf("collect sun\n");
    if(sysBaseInfo.sysSun < SYS_SUN_UP_LIMIT_NUM)
    {
        sysBaseInfo.sysSun += sysBaseInfo.sunflowerNum * SUN_SELECTED;
    }

    if(sysBaseInfo.sysSun > SYS_SUN_UP_LIMIT_NUM)
    {
        sysBaseInfo.sysSun = SYS_SUN_UP_LIMIT_NUM;
    }                
}

/* 产生僵尸*/
void generateZombie()
{
    if(sysBaseInfo.endTime%2 == 0)
    {
        grassArray[0][9] = COMMON_ZOMBIE;
        grassArray[1][9] = COMMON_ZOMBIE;
        sysBaseInfo.commonZombieNum += 2;
    }
}

/* 实际攻击僵尸*/
void realAttackZombies(int k, int i)
{
    if(grassArray[k][i] == BEAN_SHOOTER)
    {
        printf("BEAN_SHOOTER+++++++++:%d,%d,%d\n",k,i,grassArray[k][i]);//这东西很耗时的
        for(int j = i + 1; j < 10;j++)
        {
            if(grassArray[k][j] == COMMON_ZOMBIE)
            {
                if(zombieLife[k][j] == 1)
                {
                    sysBaseInfo.commonZombieNum -= 1;
                    sysBaseInfo.diedCommonZombieNum += 1;
                    grassArray[k][j] = NOTHING;
                    printf("zombieLife die=======sysTime======:%d,%d,%d\n",k,j,sysBaseInfo.endTime);//这东西很耗时的
                    zombieLife[k][j] = 3;//重新赋值为初始值
                    return;//确保一个豌豆射手每1h只攻击一次
                }
                zombieLife[k][j] -= 1;//bug_fix:
                return;
            }                    
        }
    }
}

/* 攻击僵尸*/
void attackZombie()
{
    for(int k = 0; k < 2; k++)
    {
        for(int i = 0; i < 9;i++)//豌豆只能在位置10之前
        {
            realAttackZombies(k,i);
        }
    }
}


/* 僵尸移动,移动时它的生命值也要跟着走,这就是两个2维数组的坏处*/
void moveZombie()
{
    //int isNewGen = (sysBaseInfo.endTime%2 == 0)?8:9;//新产生的下1h移动,9位置是新产生的    
    int isNewGen = (sysBaseInfo.endTime%2 == 0)?9:10;//bug_fix:新产生的下1h移动,10位置是新产生的    

    for(int k = 0; k < 2; k++)
    {
        for(int i = 1; i < isNewGen; i++)//big_bug_fix:原来遍历方向反了
        {
            if(grassArray[k][i] == COMMON_ZOMBIE)
            {
                printf("move on %d,%d\n",k,i);        
                if(grassArray[k][i-1] == SUN_FLOWER)
                {
                    sysBaseInfo.sunflowerNum -= 1;
                    sysBaseInfo.diedSunflowerNum += 1;
                }

                if(grassArray[k][i-1] == BEAN_SHOOTER)
                {
                    sysBaseInfo.beanshooterNum -= 1;
                    sysBaseInfo.diedBeanshooterNum += 1;
                }
                
                /* 优化,提取公共部分,僵尸前移一步*/
                grassArray[k][i-1] = COMMON_ZOMBIE;
                grassArray[k][i] = NOTHING;
                zombieLife[k][i-1] = zombieLife[k][i];
                zombieLife[k][i] = 3;//生命值置为初始值
            }
        }
    }
}

bool gameOver()
{
    if(sysBaseInfo.endTime == 30)
    {
        api_defendsys_ret_ext(OP_E_GAME_SUCCESS, sysBaseInfo);
        return true;
    }

    if(grassArray[0][0] == COMMON_ZOMBIE || grassArray[1][0] == COMMON_ZOMBIE)
    {
        api_defendsys_ret_ext(OP_E_GAME_OVER, sysBaseInfo);
        return true;
    }

    return false;
}

PlantsVsZombies_v2.0_1