我将如何随机迭代循环

时间:2018-04-05 12:48:00

标签: c++ loops random

我设置了一个程序,我有两种角色:玩家和敌人,我将两个玩家插入一个列表,两个敌人插入另一个列表。现在我有一个循环合并两个列表。我不确定该怎么做就是让每个循环让玩家和敌人变成随机。所以转弯的顺序总是不同的。有人可以帮我弄清楚如何在这个循环中随机设置玩家和敌人的顺序吗?

#include "stdafx.h"
#include "Character.h"
#include "Weapon.h"
#include "PlayerCharacter.h"
#include "EnemyCharacter.h"
#include <ctime>
#include <list>

using namespace std;



void playerAttack(Character* c1, Character* c2, Weapon w, string target)
{
if (target == "1")
{
    cout << "Attack Enemy 1" << endl;
    c1->takeDamage(w.Damage);
    if (c1->healthPoints <= 0) 
        c1->die();
}
else if (target == "2")
{
    cout << "Attack Enemy 2" << endl;
    c2->takeDamage(w.Damage);
    if (c2->healthPoints <= 0)
        c2->die();
}
else if (target == "1" && c1->isDead())
{
    cout << "Attack Enemy 2" << endl;
    c2->takeDamage(w.Damage);
    if (c2->healthPoints <= 0)
        c2->die();
}
else if (target == "2" && c1->isDead())
{
    cout << "Attack Enemy 1" << endl;
    c1->takeDamage(w.Damage);
    if (c1->healthPoints <= 0)
        c1->die();
}
}

void enemyAttack(Character* c1, Character* c2, Weapon w)
{
srand(time(NULL));
int randTarget = (rand() % 2 + 1);
if (randTarget = 1 || c2->isDead())
{
    cout << "Attacks Player One" << endl;
    c1->takeDamage(w.Damage);
    if (c1->healthPoints <= 0)
        c1->die();
}
else if (randTarget = 2 || c1->isDead())
{
    cout << "Attacks Player Two" << endl;
    c2->takeDamage(w.Damage);
    if (c2->healthPoints <= 0)
        c2->die();
}
}




int main()
{

srand(time(NULL));  

PlayerCharacter* p1 = new PlayerCharacter;
PlayerCharacter* p2=new PlayerCharacter;
EnemyCharacter* e1 = new EnemyCharacter;
EnemyCharacter* e2 = new EnemyCharacter;

Weapon p1W = p1->currentWeapon;
Weapon p2W = p2->currentWeapon;
Weapon e1W = e1->currentWeapon;
Weapon e2W = e2->currentWeapon;

string enemyTarget, playerTarget;

p1->printCharacter();
p2->printCharacter();
e1->printCharacter();
e2->printCharacter();

list<Character*> players;
list<Character*> enemies;

list<Character*>::iterator PinsertIt = players.begin();

list<Character*>::iterator EinsertIt = enemies.begin();




for (; PinsertIt != players.end() && EinsertIt != enemies.end(); 
++PinsertIt, ++EinsertIt)
{
    players.push_back(p1);
    players.push_back(p2);

    enemies.push_back(e1);
    enemies.push_back(e2);
}

0 个答案:

没有答案