当条件为假时循环继续

时间:2016-11-27 16:50:22

标签: c while-loop

即使条件为假,我的while循环也会继续。 我打算打印的是当用户逃跑时会出现另一个小宠物,但是当用户遇到小宠物时会打印字符串。变量'choice'的声明是否与它有关?

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <time.h>

int chance();     //Generates percentage for catch
void encounter(); //Randomly spawns pokemons
void catch(int);         //Decides whether the pokemon is captured or not
void restart(); //decides whether to restart the game 
void reset();      //resets all global variables

void playerChoice();

//list of pokemons
char pokemon[5][10] = {"Magikarp", "Zubat", "Eevee", "Magmar", "Pikachu"};
int type[5];
int pokemonType = 0;
int captured = 0, pokeball = 20, metPokemon = 0, capPokemon = 0;


int main() {
    int inGame = 0;
    //if pokeballs != 0 AND not met any pokemon OR captured a pokemon
    while(pokeball != 0 && metPokemon == 0){
        system("cls");
        encounter();
        playerChoice();
    } 

    //DOES NOT CARE ABOUT MEETING POKEMON OR NOT 
    if(pokeball == 0) {
        system("cls");
        printf("You have used up all your pokeballs \n");
    }
    restart();
}

void encounter() {
    int i;

    //random number generator from 0 - 4
    srand(time(NULL));
    i = (rand() % 5);

    //prints out the pokemon name 
    printf("A wild %s appeared!\n", pokemon[i]);
    metPokemon = 1;
    capPokemon = 0;
    pokemonType = i;
}

void playerChoice() {
    int selection;
    int choice = 0;
    while (choice == 0){
        printf("\nWhat will you do?\n");
        printf("1:Throw a pokeball!\n2:Run Away!\n");
        scanf(" %d", &selection);

        if (selection == 1) {
            choice = 1;
            catch(chance());

        } else if (selection == 2) {
            choice = 1;         //choice is 1 but why does it keep asking ? 
            metPokemon = 0;
            printf("\nYou ran away successfully!\n");
            printf("%d\n", metPokemon);
            printf("%d\n", choice);
            system("pause");
        } else {
            printf("Please enter [1] or [2] only!\n");
            choice = 0;
            getchar();
        }
    };
}

int chance() {
    //variable declaration
    int percent;

    //random number generator from 1 - 100
    srand(time(0));
    percent = (rand() % 10) + 1;
    return percent;
}

void catch() {
    //int i = 1;
    //time_t end  = time(NULL) + 3; //7s

    /*spinning animation
    while(i) {
        spinner();
        if(time(NULL) >= end){
        break;
        }
    }
    */

    //if keep breaking free, loop until gotcha 
    if(chance() <5) {
        printf("The pokemon broke free!\n");
        metPokemon = 1;
        capPokemon = 0;
        pokeball -= 1;
        printf("You have %d pokeballs left!\n", pokeball);

        //recursive functions
        while (metPokemon = 1 && capPokemon != 1 && pokeball != 0) {
            playerChoice();
        }

    } else {
        printf("\nGotcha!\n");
        type[pokemonType] += 1;
        capPokemon = 1;
        metPokemon = 0;
        pokeball -= 1;
        captured += 1;
        printf("You have %d pokeballs left! \n\n", pokeball);
        getchar();
        getchar();
    }
}

void restart() {
    int startOver = 0;
    int input;
    do {
        printf("Do you wish to continue playing?\n1:Yes\n2:No\n");
        scanf( " %d", &input); 

        if(input == 1) {
            printf("Restarting the game... \n");
            system("pause");
            system("cls");
            startOver = 1;
        } else if (input == 2) {
            printf("See you again next time! \n"); 
            exit(0);
        } else {
            printf("Please enter [1] or [2] only! \n\n");
            startOver = 0;
            getchar();
        }
    }
        while (startOver == 0);
}

0 个答案:

没有答案