首页 > 解决方案 > C ++中数组中的随机字符串

问题描述

我试图让程序从数组中输出随机攻击,monsterAttack但它一遍又一遍地输出相同的东西,有人可以帮我解决这个问题吗?

我的代码:

/* Skyy Civil
C++ Legend Game
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <random>
#include <vector>
using namespace std;

/*
Declaring functions
*/
void Menu();
void Login();
void LevelStart();
void GameMenu();

/* No files, struct instead.

structing monsters and hero and damage
*/
struct Monster{
    int monsterCount;
    string monsterType;
    int monsterHealth;
    
};

// Hero
struct Hero{
    int coins;
    int health;
    
};


struct Attacks{
    string attack1;
    string attack2;
    string attack3;
    string attack4;
    int damageAttack1;
    int damageAttack2;
    int damageAttack3;
    int damageAttack4;
};


/*
        The actual level starts here
*/


void LevelStart(int XP, int Lvl, int NewXP, int AddXP, int NewLvl){
    int playerAttack;
        struct Hero player;
    struct Attacks playerAttacks;
    struct Attacks monsterAttacks;

    struct Monster easyLevelMonster;

    // Player attacks and stats
    
    player.health = 150;
    playerAttacks.attack1 = "FireBall";
    playerAttacks.attack2 = "Fire Slash";
    playerAttacks.attack3 = "Fire Barrage";
    playerAttacks.attack4 = "Blue Flame Bomb";
    playerAttacks.damageAttack1 = 4;
    playerAttacks.damageAttack2 = 9;
    playerAttacks.damageAttack3 = 14;
    playerAttacks.damageAttack4 = 19;

    // Monster attacks and stats
    
    easyLevelMonster.monsterCount = 1;
    easyLevelMonster.monsterType = "monster";
    easyLevelMonster.monsterHealth = 100;

    string monsterAttack[3] = {"Bash", "Spin Attack", "Heavy Punch"};
    int monsterAttackDamage[3] = {10, 15, 20};
    int RandAtk = rand() % 2 + 0;

    
// -----------------MAKING ATTACKS---------------------\\

    cout << "Monsters: " << easyLevelMonster.monsterCount;
    cout << "\n\n Monster type: " << easyLevelMonster.monsterType;
    cout << "\n\n (1) " << playerAttacks.attack1;
    cout << "\n\n (2) " << playerAttacks.attack2;
    cout << "\n\n (3) " << playerAttacks.attack3;
    cout << "\n\n (4) " << playerAttacks.attack4;
    cout << "\n > ";
    
    // While loop

    while((easyLevelMonster.monsterCount = 1)){
    cin >> playerAttack;
    // Attack 1 Loop
    if(playerAttack == 1){
        easyLevelMonster.monsterHealth = easyLevelMonster.monsterHealth - playerAttacks.damageAttack1;
        srand (time(NULL));
        cout << endl << easyLevelMonster.monsterType << " used " << monsterAttack[RandAtk];
        if(RandAtk == monsterAttackDamage[0]){
            player.health = player.health - monsterAttackDamage[0];
        }
        if(RandAtk == monsterAttackDamage[1]){
            player.health = player.health - monsterAttackDamage[1];
        }
        if(RandAtk == monsterAttackDamage[2]){
            player.health = player.health - monsterAttackDamage[2];
        }
            cout << "HP: " << player.health << endl;
            cout << "\nMonsters: " << easyLevelMonster.monsterCount << endl;
            cout << "Monster health: " << easyLevelMonster.monsterHealth << endl;
    cout << "\n\n Monster type: " << easyLevelMonster.monsterType;

    cout << "\n\n (1) " << playerAttacks.attack1;
    cout << "\n\n (2) " << playerAttacks.attack2;
    cout << "\n\n (3) " << playerAttacks.attack3;
    cout << "\n\n (4) " << playerAttacks.attack4;
    cout << "\n > ";

    }

    // Attack 2 loop
    if(playerAttack == 2){
        srand (time(NULL));
        cout << endl << easyLevelMonster.monsterType << " used " << monsterAttack[RandAtk];
        if(RandAtk == monsterAttackDamage[0]){
            player.health = player.health - monsterAttackDamage[0];
        }
        if(RandAtk == monsterAttackDamage[1]){
            player.health = player.health - monsterAttackDamage[1];
        }
        if(RandAtk == monsterAttackDamage[2]){
            player.health = player.health - monsterAttackDamage[2];
        }
        easyLevelMonster.monsterHealth = easyLevelMonster.monsterHealth - playerAttacks.damageAttack2;
        cout << "HP: " << player.health << endl;
            cout << "Monsters: " << easyLevelMonster.monsterCount;
            cout << "Monster health: " << easyLevelMonster.monsterHealth;
    cout << "\n\n Monster type: " << easyLevelMonster.monsterType;
    cout << "\n\n (1) " << playerAttacks.attack1;
    cout << "\n\n (2) " << playerAttacks.attack2;
    cout << "\n\n (3) " << playerAttacks.attack3;
    cout << "\n\n (4) " << playerAttacks.attack4;
    cout << "\n > ";
    }

    // Attack 3 loop

    if(playerAttack == 3){
        srand (time(NULL));
        cout << endl << easyLevelMonster.monsterType << " used " << monsterAttack[RandAtk];
        if(RandAtk == monsterAttackDamage[0]){
            player.health = player.health - monsterAttackDamage[0];
        }
        if(RandAtk == monsterAttackDamage[1]){
            player.health = player.health - monsterAttackDamage[1];
        }
        if(RandAtk == monsterAttackDamage[2]){
            player.health = player.health - monsterAttackDamage[2];
        }
        easyLevelMonster.monsterHealth = easyLevelMonster.monsterHealth - playerAttacks.damageAttack3;
        cout << "HP: " << player.health << endl;
            cout << "Monsters: " << easyLevelMonster.monsterCount;
            cout << "Monster health: " << easyLevelMonster.monsterHealth;
    cout << "\n\n Monster type: " << easyLevelMonster.monsterType;
    cout << "\n\n (1) " << playerAttacks.attack1;
    cout << "\n\n (2) " << playerAttacks.attack2;
    cout << "\n\n (3) " << playerAttacks.attack3;
    cout << "\n\n (4) " << playerAttacks.attack4;
    cout << "\n > ";
    }

    // Attack 4 loop
    if(playerAttack == 4){
        // Random monster attacks
        srand (time(NULL));
        cout << endl << easyLevelMonster.monsterType << " used " << monsterAttack[RandAtk];
        if(RandAtk == monsterAttackDamage[0]){
            player.health = player.health - monsterAttackDamage[0];
        }
        if(RandAtk == monsterAttackDamage[1]){
            player.health = player.health - monsterAttackDamage[1];
        }
        if(RandAtk == monsterAttackDamage[2]){
            player.health = player.health - monsterAttackDamage[2];
        }
  easyLevelMonster.monsterHealth = easyLevelMonster.monsterHealth - playerAttacks.damageAttack4;
    cout << "HP: " << player.health << endl;
    cout << "Monsters: " << easyLevelMonster.monsterCount;
    cout << "Monster health: " << easyLevelMonster.monsterHealth;
    cout << "\n\n Monster type: " << easyLevelMonster.monsterType;
    cout << "\n\n (1) " << playerAttacks.attack1;
    cout << "\n\n (2) " << playerAttacks.attack2;
    cout << "\n\n (3) " << playerAttacks.attack3;
    cout << "\n\n (4) " << playerAttacks.attack4;
    cout << "\n > ";
    }
    // When player wins
    else if(easyLevelMonster.monsterHealth <= 0){
        
    }
}

}






/*
Game menu includes options for the player to choose to play the game
*/

void GameMenu(){
    struct Attacks sword;
    struct Hero player;
    string gameMenuChoice;
    int XP;
        int Lvl = 0;
        int a;
        int NewLvl = 0;
        int AddXP = 0;
        int UpOne;
        int NewXP = 0;
        string username;
        string password;
        // XP system

if(NewXP >= 100){
                    UpOne = NewXP / 100;
                    NewLvl = Lvl + UpOne;
}
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << "~~~~~~~+~~~~~~~LEGEND!~~~~~~~+~~~~~~~\n\n\n\n";
    cout << username << "\n\n";
    cout << "XP: " << NewXP << endl;
    cout << "Level: " << NewLvl << endl;
    cout << "(a) Play Level\n";
    cout << "(b) Exit\n";
    cin >> gameMenuChoice;
    if(gameMenuChoice == "a"){
        LevelStart(XP, Lvl, NewXP, AddXP, NewLvl);
    }
    if(gameMenuChoice == "b"){
        cout << "Game EXITED\n";
    
    }

}


void Login(){
    int loginChoice;
    string username;
    string password;
    ofstream login;
    login.open("login.dat", ios::in | ios::out);
    cout << "Username\n";
    cout << "\n > ";
    cin >> username;
    login << "Username: " << username << endl;
    cout << "\n\n\n Password\n";
    cout << "\n > ";
    cin >> password;
    login << "Pass: " << password << endl;
    login.close();
    cout << "loading...\n\n\n";
    cout << "(1) Start\n";
    cin >> loginChoice;
    if(loginChoice == 1){
        GameMenu();
    }
}

// StoryMode

void StoryMode(void){
    int storyModeChoice;
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n-----------------Welcome to story mode!----------------\n\n";
    cout << "(1) Login\n";
    cin >> storyModeChoice;
    if(storyModeChoice == 1){
        Login();
    }
}

// Credits
void Credits(){
    
    int creditsChoice;
    cout << "\n\n\n\n\nCreator: Skyy Civil\n\n\n\n";
    cout << "(1) Back to menu\n";
    cout << "(2)Continue to story mode\n";
    cin >> creditsChoice;
    if(creditsChoice == 1){
        Menu();
    }
    if (creditsChoice == 2){
        StoryMode();
    }
}

// Main Menu

void Menu(){
    int menuChoice;
    cout << "\nLegend\n";
    cout << "(1)Story Mode\n";
    cout << "(2) Credits\n";
    cin >> menuChoice;
    if(menuChoice == 1){
        StoryMode();
    }
    if(menuChoice == 2){
        Credits();
    }
};

// ------------MAIN--------------- \\

int main(void){
    Menu();
}

标签: c++random

解决方案


推荐阅读