Thread: Need help in C programming

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

    Smile Need help in C programming

    Hello guys! This is my first post here- ok lets go to the problem. I am currently making a turn-based game and I have encountered a problem. Whenever I run the program, Player 2 always wins the game. Could you please take a look and try to fix anything if possible? Thank you very much!

    #include <stdio.h>

    Code:
    void PreviewClasses()
    {
    printf("Good day!!! Lets look at each classes.\n");
    printf("Swordsman is the melee class of the game and excels in close combat fights.\n");
    printf("HP = 1000 MP = 500\n");
    printf("Skills: Tackle\tSlash\tBlade Slash\tWhirlwind\n");
    printf("\n\n");
    printf("Archer prefers attacking from a distance and deal great damage with arrows.\n");
    printf("HP = 800 MP = 400\n");
    printf("Skils: Dual Shot\tFrost Arrow\tArrow Bomb\tCharged Shot\n");
    printf("\n\n");
    printf("Mage specializes in the way of magic to defeat the enemy.\n");
    printf("HP = 600 MP = 1000\n");
    printf("Skills: Fireball\tImpale\tDragon Slave\tFinger of Death\t\n\n");
    
    
    
    
    }
    
    
    int main()
    {
    int hp1, hp2, mp1, mp2, sa1, sa2, sa3, sa4, sb1, sb2, sb3, sb4;
    char Player1, Player2, p1, p2;
    
    
    
    
    
    
    
    
    PreviewClasses();
    
    
    printf("Lets Begin!!!\n");
    
    
    printf("Player 1 please choose your class: Swordsman = A, Archer = B, Mage = C \n");
    scanf("%c", &Player1);
    
    
    if(Player1 == 'A' || Player1 == 'a')
    {    
    hp1 = 1000;
    mp1= 500;
    sa1 = 90;
    sa2 = 70;
    sa3 = 100;
    sa4 = 120;
    printf("Player 1 chose SWORDSMAN.\n");
    }
    else if(Player1 == 'B' || Player1 == 'b')
    {
    
    
    hp1 = 800;
    mp1 = 600;
    sa1 = 90;
    sa2 = 100;
    sa3 = 120;
    sa4 = 150;
    printf("Player 1 chose ARCHER.\n");
    }
    else if(Player1 == 'C' || Player1 == 'c')
    {
    hp1 = 900;
    mp1 = 800;
    sa1 = 90;
    sa2 = 85;
    sa3 = 95;
    sa4 = 100;
    printf("Player1 chose MAGE.\n");
    }
    
    
    printf("Player 2 please choose your class: Swordsman = A, Archer = B, Mage = C \n");
    scanf("%c", &Player2);
    scanf("%c", &Player2);
    
    
    
    
    
    
    
    
    if(Player2 == 'A' || Player2 == 'a')
    {
    hp2 = 1000;
    mp2 = 500;
    sb1 = 90;
    sb2 = 70;
    sb3 = 100;
    sb4 = 120;
    printf("Player 2 chose SWORDSMAN.\n");
    }
    
    
    else if(Player2 == 'B' || Player2 == 'b')
    {
    hp2 = 800;
    mp2 = 600;
    sb1 = 90;
    sb2 = 100;
    sb3 = 120;
    sb4 = 150;
    printf("Player 2 chose ARCHER.\n");
    }
    
    
    else if(Player2 == 'C' || Player2 == 'c')
    {
    hp2 = 900;
    mp2 = 800;
    sb1 = 90;
    sb2 = 85;
    sb3 = 95;
    sb4 = 100;
    printf("Player 2 chose MAGE.\n");
    }
    system("pause");
    
    
    system("cls");
    
    
    printf("Start Battle!\n");
    
    
    do {
    printf("Player 1 please choose a skill: ");
    scanf("%c", &p1);
    scanf("%c", &p1);
    
    
    
    
    if(p1 == 'A' || p1 == 'a') 
    hp2 -= sa1; 
    else if(p1 == 'B' || p1 == 'b') 
    hp2 -= sa2; 
    else if(p1 == 'C' || p1 == 'c') 
    hp2 -= sa3; 
    else 
    hp2 -= sa4; 
    printf("\nPlayer 2's HP = %d\n", hp2);
    
    
    printf("Player 2 please choose a skill: ");
    scanf("%c", &p2);
    scanf("%c", &p2);
    
    
    if(p2 == 'A' || p2 == 'a') 
    hp1 -= sb1; 
    else if(p2 == 'B' || p2 == 'b') 
    hp1 -= sb2; 
    else if(p2 == 'C' || p2 == 'c') 
    hp1 - sb3; 
    else 
    hp1 -= sb4; 
    printf("\nPlayer 1's HP = %d\n", hp1);
    
    
    } 
    while(hp1 >= 0 || hp2 >= 0);
    
    
    if(hp1 <= 0)
    {
           printf("Player 2 wins!\n");
           }
           
           else if(hp2 <= 0)
           {
                printf("Player 1 wins!\n");
                }
    system("pause");
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    One problem is this:
    Code:
    while(hp1 >= 0 || hp2 >= 0);
    In order for this loop to terminate hp1 < 0 AND hp2 < 0. After the loop, you check hp1 first, so player two will always win.

    Instead try this:
    Code:
    while(hp1 >= 0 && hp2 >= 0);

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    Thank you very much failure67!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread