战争游戏随机播放功能返回否定## s

时间:2014-04-29 22:10:48

标签: c++

#include <iostream>
#include <string>
#include <vector>  
#include <algorithm> 
#include <ctime>
using namespace std;

//Class for a card deck:
class CardDeck
{
public:
CardDeck(int theValue, string theSuit);
CardDeck(){} 

// Setters--Don't think we will need
void setValue(int theValue);
void setSuit(string theSuit);

// Getters
int getValue();
string getSuit();
private:
int value;
string suit; 
};// end CardDeck class



int main()
{
int i = 0; 
int gameInPlay = 1;

const string DR = "Dragons";
const string MG = "Mages";
const string WR = "Warriors";
const string CF = "Confessors";


vector<CardDeck> startDeck(52);
vector<CardDeck> tempCards(1); 

// Dragons Suit
for (i = 0; i < 13; i++)
{
    startDeck[i].setValue(i - 12);
    startDeck[i].setSuit("Dragons");
    //startDeck[i].setValue(i+1);
    //  startDeck[i].setSuit("Dragons");
}
// Mages Suit
for (i = 13; i < 26; i++)
{
    startDeck[i].setValue(i - 12);
    startDeck[i].setSuit("Mages");
}
for (i = 26; i < 39; i++)
{
    startDeck[i].setValue(i - 25);
    startDeck[i].setSuit("Warriors");
}

for (i = 39; i < 52; i++)
{
    startDeck[i].setValue(i - 38);
    startDeck[i].setSuit("Confessors");
}


// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << "\n\n";



//****************************************************************************
// Shuffle the deck
int shuffleTimes = (rand() % 120) + 1;
// Need to shuffle a random # of times, else deck is
// "shuffled" in same order every time
for (int i = 0; i < shuffleTimes; i++)
{
    srand(time(0));
    for (i = 0; i < startDeck.size(); i++)
    {
        int second = rand() % startDeck.size();
        CardDeck temp = startDeck[i];
        startDeck[i] = startDeck[second];
        startDeck[second] = temp;

    }
}
//*******************************************************************************



// Verify cards are shuffled for de-bug
cout << "After shuffling:\n Value \t Suit\n";

// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << endl;


// Creat human deck
vector<CardDeck> humanDeck(26);
for (i = 0; i< 26; i++)
{
    humanDeck[i] = startDeck[i];

}

// Creat computer deck
vector<CardDeck> computerDeck(26);
for (i = 0; i< 26; i++)
{
    computerDeck[i] = startDeck[i + 26];

}


// Output for de-bug
cout << "The first human card is " << humanDeck[0].getValue() << " of " << humanDeck[0].getSuit() << endl;
cout << "The second human card is " << humanDeck[1].getValue() << " of " << humanDeck[1].getSuit() << "\n\n";

cout << "The first computer card is " << computerDeck[0].getValue() << " of " << computerDeck[0].getSuit() << endl;
cout << "The second computer card is " << computerDeck[1].getValue() << " of " << computerDeck[1].getSuit() << "\n\n";



getchar();
return 0;

} // end main

// Functions for CardDeck class
CardDeck::CardDeck(int theValue, string theSuit)
{
value = theValue;
suit = theSuit;
}

void CardDeck::setValue(int theValue)
{
value = theValue;
}

void CardDeck::setSuit(string theSuit)
{
suit = theSuit;
}

int CardDeck::getValue()
{
return value;
}

string CardDeck::getSuit()
{
return suit;
}

显然没有完成游戏,我是C ++和编程的新手,所以任何帮助都可以做到

我想要一些帮助,试图弄清楚如何只得到正数而不是负数。还想弄清楚为什么它们返回前两个输出的值总是相同的。

  • 谢谢

1 个答案:

答案 0 :(得分:1)

您可能打算这样做:

for (i = 0; i < 13; i++)
{
    startDeck[i].setValue(i+1);
    startDeck[i].setSuit("Dragons");
    //startDeck[i].setValue(i+1);
    //  startDeck[i].setSuit("Dragons");
}

否则,startDeck[i].setValue(i-12);将为i <设置负值。 12,这是该循环的大部分。

我想知道为什么你在那里有正确的代码并注释掉了......它的问题是什么?