同时命令激活两次C语言

时间:2015-09-02 01:33:13

标签: c xcode command-line

我一直试图制作这款基于文字的游戏,并且我注意到,如果玩家选择了有效的选择,它会检测到天气。玩家第一次输入错误时它只打印一次。但如果玩家第二次犯错,它会打印两次。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

int main()
{

char enter = 0;
char pNAME[30];
char pGENDER;
char pWEAPON[20];
char pARMOR [10];
int  pWEAPONID;
int choice1,choice2 = 0,choice3;
int pAGE;
int pATK;
int pHP = 25;
int pATKS = 10;
int pATKC = 0;
int knightHP = 20;
int knightATKS = 21;
int knightATKC;
int kingHP = 30;
int kingATKC;
srand((unsigned)time(NULL));


printf("While you are enjoying your breakfast at your house, suddenly you get teleported to a different dimension and no one to be seen except an old man.\n");

printf("\nOld Man: What is your name warrior?\n");
printf("\nInsert name:");
scanf("%s",pNAME);
fpurge( stdin );

printf("\nInsert gender (m/f):");
scanf("%c",&pGENDER);
fpurge( stdin );
while ( pGENDER != 'f' &&pGENDER != 'm' ) //Second mistake prints 2 times
{
    printf("\nInvalid entry, please try again.\n");
    printf("\nInsert gender (m/f):");
    scanf("%c",&pGENDER);
}

printf("\nOld Man: Now how old are you warrior?\n");
printf("\nInsert Age:");
scanf("%d",&pAGE);
fpurge( stdin );
if ( pAGE < 18)
{
    printf("\nOy mate! No stalker!\n");
    printf("\nTeleporting back to reality...\n");

    return 0;
}

if ( pAGE > 80)
{
    printf("\nNo oldies allowed!\n");
    printf("\nTeleporting back to reality...\n");

    return 0;
}

printf("\nOld Man: Welcome warrior I fear the knights has taken over our kingdom, and you, %s, are the only one who can save us.\n",pNAME);
printf("\n%s: How am I suppose to do that?[Enter]\n",pNAME);
while (enter != '\r' && enter != '\n') { enter = getchar(); }


printf("Old Man: Pick one of these three items\n");
printf("\n[1] Axe : Damage = 7\t Accuracy = 25%%\n");   //
printf("\n[2] Sword : Damage = 5\t Accuracy = 30%%\n");
printf("\n[3] Knife : Damage = 3\t Accuracy = 35%%\n");
printf("\nYou choose: ");
scanf("%d",&pWEAPONID);
while (pWEAPONID != 1 && pWEAPONID != 2 && pWEAPONID != 3)
{
    printf("\nInvalid entry, please try again.\n");
    printf("\nYou choose:");
    scanf("%d",&pWEAPONID); //Second mistake prints 2 times
} 
if (pWEAPONID == 1)
{
    pATK = 8;
    pATKS = 20;     // 5/20 chance
    strcpy(pWEAPON, "Axe");
}
if (pWEAPONID == 2)
{
    pATK = 6;
    pATKS = 16;     // 5/16 chance
    strcpy(pWEAPON, "Sword");
}
if (pWEAPONID == 3)
{
    pATK = 4;
    pATKS = 12;     // 5/12 chance
    strcpy(pWEAPON, "Knife");
}

printf("\nOld Man: Good choice, you chose the %s, now continue foward to destroy The Evil King\n",pWEAPON);
printf("\nWhile you are continuing your journey to destroy The Evil King, you come face to face with a knight\n");
printf("\nWhat do you want to do?\n");
printf("\n[1] Run [2] Battle:");
scanf("%d",&choice1);

if (choice1 == 1) //Dead
{
    printf("\nYou attempted to run, but the knight chases you with his horse and a spear.\n");
    printf("\nThe knight stabs you right through your body.\n");
    printf("\nYou are dead, thanks for playing.\n");
    return 0;
}


while (pHP != 0 && knightHP != 0) // Combat starts
{

    knightATKC = rand()%knightATKS;     // 7/21 chance

    if (knightATKC == 1 || knightATKC == 6 || knightATKC == 13 || knightATKC == 19 )
    {
        printf("\nThe knight swings his sword at %s\n",pNAME);
        printf("The knight hits %s for 3HP\n",pNAME);
        pHP = pHP - 3;
        printf("%s has %d/25 HP\n",pNAME,pHP);
        if (pHP == 0 || pHP <0)
        {
            printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
            printf("\nYou are dead, thanks for playing.\n");
            return 0;
        }
        printf("\nWhat do you want to do?\n");
        printf("[1] Attack [2] Charge your attack [3] Heal");
        scanf("%d",&choice1);

    }
    else if (knightATKC == 7 || knightATKC == 0)
    {
        printf("\nThe knight slashes his spear at %s\n",pNAME);
        printf("The knight critically hit %s for 5 HP\n",pNAME);
        pHP = pHP - 5;
        printf("%s has %d/25 HP\n",pNAME,pHP);
        if (pHP == 0 || pHP <0)
        {
            printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
            printf("\nYou are dead, thanks for playing.\n");
            return 0;
        }
        printf("\nWhat do you want to do?\n");
        printf("[1] Attack [2] Charge your attack [3] Heal:");
        scanf("%d",&choice2);
    }
    else if (knightATKC == 11)
    {
        printf("\nThe knight's horse kicks %s\n",pNAME);
        printf("The knight's horse hit %s for 7 HP\n",pNAME);
        pHP = pHP - 7;
        printf("%s has %d/25 HP\n",pNAME,pHP);
        if (pHP == 0 || pHP <0)
        {
            printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
            printf("\nYou are dead, thanks for playing.\n");
            return 0;
        }
        printf("\nWhat do you want to do?\n");
        printf("[1] Attack [2] Charge your attack [3] Heal:");
        scanf("%d",&choice2);
    }
    else
    {
        printf("\nThe knight swings his spear at %s\n",pNAME);
        printf("You blocked his sword with your %s\n",pWEAPON);
        printf("%s has %d/25 HP\n",pNAME,pHP);
        printf("\nWhat do you want to do?\n");
        printf("[1] Attack [2] Charge your attack [3] Heal:");
        scanf("%d",&choice2);
    }

    if (choice2 == 1)
    {
        pATKC = rand()%pATKS;
        if (pATKC == 1 || pATKC == 4 || pATKC == 7 || pATKC == 11)
        {
          // Not done
        }}}}

0 个答案:

没有答案