CCF201609(3)爐石傳說

這道題早就想要把它拿下了明天加油

#include <stdio.h>
#include <string.h>

struct role{//創建隨從數據結構 
    int attack;//攻擊力 
    int health;//血量 
};
struct role player[2][8];//創建一個二維數組存儲雙方的英雄和隨從 
int role_num[2] = {0 , 0};//分別表示雙方角色數量 

void summon(int who ,int pos ,int att , int hea);
void delete(int who ,int pos);
void attack(int who , int pos1 , int pos2);
int main(void) {
    int i, j,pos, att , hea,n;
    int attacker , defender;
    char cmd[15];
    int who = 0 ;
    player[0][0].attack = 0 ;//初始化雙方英雄的血量和攻擊力 
    player[0][0].health = 30;
    player[1][0].attack = 0;
    player[1][0].health = 30;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",cmd);
        if(strcmp(cmd,"summon") == 0)
        {//summon密令,召喚隨從 
            scanf("%d%d%d",&pos,&att,&hea);
            summon(who,pos,att,hea);
        }
        if(strcmp(cmd,"attack")== 0)
        {//攻擊 
            scanf("%d%d",&attacker,&defender);//attacker表示攻擊方位置,defender表示被攻擊方位置 
            attack(who,attacker,defender);//攻擊,who表示是哪方在攻擊,初始who = 0,表示先手攻擊 
        }
        if(strcmp(cmd,"end")== 0)
            who = 1- who ;//密令end 後,換對方操作,who = 1 - who 
    }
    
    if(player[0][0].health>0 && player[1][0].health<=0)//所有操作之後,比較雙方英雄的血量確定勝負 
        printf("1\n");
    else if(player[0][0].health<=0 && player[1][0].health>0)
        printf("-1\n");
    else
        printf("0\n");
    for(i = 0 ; i < 2 ; i++)//打印雙方角色數量,血量 
    {
        printf("%d\n",player[i][0].health);
        printf("%d ",role_num[i]);
        for(j = 1 ; j <= role_num[i]; j++)
        {
            printf("%d ",player[i][j].health);
        }
        printf("\n");
    }
    return 0;
}

void summon(int who ,int pos ,int att , int hea)
{//召喚函數,who表示召喚方,pos表示在哪個位置召喚,att表示隨從攻擊力,hea表示血量 
    int i;
    if(pos > 0)//召喚的隨從只能在英雄的右方 
    {
        role_num[who]++;//每召喚一次,角色數量加1 
        for(i = role_num[who] ; i > pos ; i --)
        {//位置Pos中和右側隨從都要向右移動一個位置,以讓剛召喚的隨從放置在pos位置 
            player[who][i] = player[who][i-1];
        }
        player[who][pos].attack = att ;//給召喚的隨從賦值攻擊力、血量 
        player[who][pos].health = hea ;
    }
    
}

void delete(int who ,int pos)
{//移除死掉的隨從 
    int i;
    if(pos > 0)//只有隨從才需要移除 
    {
        for(i = pos ; i < role_num[who]; i ++)
        {//移除pos位置的隨從,pos右側的隨從都要向左移動一個位置 
            player[who][i] = player[who][i+1];
        }
        role_num[who] -- ;//移除一個隨從後,角色數量減1 
    }
}

void attack(int who , int pos1 , int pos2)
{//攻擊函數 
    player[who][pos1].health -= player[1-who][pos2].attack;//被攻擊方血量的減少值等於攻擊方的攻擊力值 
    player[1-who][pos2].health -= player[who][pos1].attack;//攻擊對方的同時,自己也會受到對方攻擊力大小的掉血量 
    if(player[who][pos1].health <= 0)//隨從血量小於等於0後,需要移除 
        delete(who , pos1);
    if(player[1-who][pos2].health <= 0)
        delete(1-who , pos2);
}

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