2014-04-29 54 views
0
#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

如果'i'是'0'而你做'我 - 12'結果如何? –

+0

順便說一句'srand()'應該在程序一開始就被調用,永遠不會再次 – yizzlez

+0

謝謝你 – Aritoone

回答

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);會爲我< 12,這是最該循環的設置負值。

我想知道爲什麼你在那裏有正確的代碼並註釋掉了......它有什麼問題嗎?

+0

與他人一起工作,並且因爲我在這裏有一個帳戶,所以我只是複製粘貼。謝謝 – Aritoone