2011-07-29 229 views
1

我寫了一個小的代碼來獲得一些C++ exerice ...C++紙牌遊戲,運動帶班

代碼工作...但它有一個小問題...如果你想解決的問題繼續閱讀,如果你不想評論編程風格,我需要它來提高自己。

遊戲被稱爲「在camisa壓力」, 是我在意大利的區域紙牌遊戲...

遊戲是一個非常簡單的運氣的遊戲,我打它,當我是4 ...規則很簡單:

分佈所有52張法國甲板兩個玩家之間,

每個球員在一排丟棄在桌子中央的手頂部的卡,

如果一張卡少於三張,則爲

其他球員必須給的卡報卡上的號碼對應的號碼......

如果其他球員扔在一排卡,無需卡小於3,其他球員獲取桌子中央的所有牌。

失去了誰手中沒有任何卡。

「冷杉轉」

 player 1: 
     King of Spades 
     player 2: 
     Queen of Clubs 
     player 1: 
     3 of Diamond (>now player2 has to discard 3 cards) 
     player 2: 
     Jack of Hearts 
     8 of Clubs 
     6 of Diamond 

播放器1韓元,採取:

 King of Spades 
     Queen of Clubs 
     3 of Diamond 
     Jack of Hearts 
     8 of Clubs 
     6 of Diamond 

「第二轉」

 player 2: 
     2 of Clubs (> player 1 has to throw 2 cards) 
     player 1: 
     King of Clubs (> 1st card) 
     2 of Diamond (> 2nd card is less then 3 so player 2 has to throw 2 card) 
     player 2: 
     5 of Hearts (> 1st card) 
     Jack of Diamond (> 2nd card) 

播放一個whon再次,和需要:

 2 of Clubs 
     King of Clubs 
     2 of Diamond 
     5 of Hearts 
     Jack of Diamond 

我定義的函數來檢查,如果卡被拋出的代碼是< 3,並使對方球員扔卡這樣相應的數字:

 void check(Card card, Hand &g1, Hand &g2, Hand &Deck_c){ 

    cout << endl; 
    int a = card.get_number(); 
    if (card.get_number() <= 3) { 
     cout << g2.get_name() << " discard: " << endl; 
     while (a > 0) { 

      if (g2.void_hand()) { 
       return; 
      } 
      Card discarded_card = g2.discard(0); 
      cout << " "; 
      cout << discarded_card; 
      Deck_c.append_card(discarded_card); 
      check(discarded_card, g2,g1, Deck_c); 
      a--; 
     } 
     int size_Deck_c = Deck_c.size_cards(); 
     for (int i = 0; i < size_Deck_c; i++) { 
      g1.feed_from_back(Deck_c.discard(0)); 
     } 
     return; 

    } 

    return; 
} 

Giovanni discard: 
2 of Heart 
Pietro discard: 
    8 of Clubs 
    7 of Heart 

,但現在的問題是:如果在玩的少只有一個網卡,然後3工作正常

Pietro discard: 
3 of Heart 
Giovanni discard: 
    2 of Spades 
Pietro discard: 
    Queen of Spades 
    5 of Spades (>should pass the turn but continue to throw cards) 
    5 of Diamond 
    9 of Diamond 

彼得應該拋棄2卡,然後通過轉....

功能是遞歸函數,其中檢查是否卡是小於3每卡被拋出

如果它超過3它回到正常的遊戲,

否則它再次調用切換球員的功能......

的問題是在迭代器「詮釋了」 ......

這是源代碼,如果你想看看:

#include <iostream> 
#include <string> 
#include <vector> 
#include <stdlib.h> 

using namespace std; 



int array_numbers[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; 
string array_suit[] = {"Heart","Clubs","Spades","Diamond"}; 

class Card{ 
private: 
    int Number; 
    string Suit; 
public: 
    Card(); 
    Card(int,string); 
    int get_number(); 
    string get_suit(); 
    void assign_values(int,string); 
    friend ostream &operator<<(ostream&, const Card&); 
}; 

Card::Card(){ 
    Number = 0; 
    Suit = "void"; 
} 
Card::Card(int number, string suit){ 
    Number = number; 
    Suit = suit; 
} 

int Card::get_number(){ 
    return Number; 
} 

string Card::get_suit(){ 
    return Suit; 
} 

void Card::assign_values(int i, string suit){ 
    Number = i; 
    Suit = suit; 
} 

ostream &operator<<(ostream& os, const Card& card){ 
    if (card.Number == 1) { 
     os << "Ace of " << card.Suit; 
    } 
    else if(card.Number == 11){ 
     os << "Jack of " << card.Suit; 
    } 
    else if(card.Number == 12){ 
     os << "Queen of " << card.Suit; 
    } 
    else if(card.Number == 13){ 
     os << "King of " << card.Suit; 
    } 
    else{ 
     os << card.Number << " of " << card.Suit; 
    } 
    return os; 
} 

class Deck { 
private: 
    int x; 
    Card cards[52]; 
public: 
    Deck(); 
    void print_card(int); 
    void print_Deck(); 
    void shuffle(); 
    Card get_card(int); 

}; 

Deck::Deck(){ 
    for (int n = 0; n < 13; n++) { 
     for (int seme = 0; seme < 4; seme++) { 
      cards[n*4+seme].assign_values(array_numbers[n], array_suit[seme]); 
     } 
    } 
} 

void Deck::print_card(int i){ 
    cout << cards[i]; 
} 

void Deck::print_Deck(){ 
    for (int i = 0; i<52; i++) { 
     for (int a = 0; a < i; a++) { 
      cout << " "; 
     }  
     print_card(i); 
     cout << endl; 
    } 
} 
void Deck::shuffle(){ 
    srand (time(NULL)); 
    for (int i = 0; i<500; i++) { 
     int x = rand()%51; 
     int y = rand()%51; 
     Card cpy; 
     cpy.assign_values(cards[x].get_number(),cards[x].get_suit()); 
     cards[x] = cards[y]; 
     cards[y] = cpy; 
    } 
} 

Card Deck::get_card(int i){ 
    return cards[i]; 
} 

class Hand { 
private: 
    vector <Card> cards; 
    string name; 
public: 
    Hand(); 
    Hand(string); 
    string get_name(); 
    void append_card(Card); 
    void print_hand(); 
    Card discard(int); 
    bool operator==(Hand); 
    bool void_hand(); 
    void feed_from_back(Card); 
    int size_cards(); 
}; 

int Hand::size_cards(){ 
    return cards.size(); 
} 

void Hand::append_card(Card card){ 
    cards.insert(cards.begin(),card); 

} 

Hand::Hand(){ 
    name = "void"; 
} 

Hand::Hand(string x){ 
    name = x; 
} 

string Hand::get_name(){ 
    return name; 
} 

void Hand::print_hand(){ 
    int a= 0; 
    vector <Card> ::iterator i; 
    for (i = cards.begin(); i != cards.end(); i++) { 
     a++; 
     for (int c = 0; c < a; c++) { 
      cout << " "; 
     } 
     cout << *i << endl; 
    } 
} 



Card Hand::discard(int a){ 
    Card c_return; 
    vector <Card>::iterator i; 
    i = cards.begin(); 
    for (int f = 0; f < a; f++) { 
     i++; 
    } 
    c_return = *i; 
    cards.erase(i); 
    return c_return; 
} 


bool Hand::operator==(Hand other_hand){ 
    if (name == other_hand.get_name()) { 
     return 1; 
    } 
    else { 
     return 0; 
    } 
} 

bool Hand::void_hand(){ 
    if (cards.size()== 0) { 
     return 1; 
    } 
    else { 
     return 0; 
    } 
} 

void Hand::feed_from_back(Card x){ 
    cards.insert(cards.end(),x); 
} 



void check(Card card, Hand &g1, Hand &g2, Hand &Deck_c){ 

    cout << endl; 
    int a = card.get_number(); 
    if (card.get_number() <= 3) { 
     cout << g2.get_name() << " discard: " << endl; 
     while (a > 0) { 

      if (g2.void_hand()) { 
       return; 
      } 
      Card discarded_card = g2.discard(0); 
      cout << " "; 
      cout << discarded_card; 
      Deck_c.append_card(discarded_card); 
      check(discarded_card, g2,g1, Deck_c); 
      a--; 
     } 
     int size_Deck_c = Deck_c.size_cards(); 
     for (int i = 0; i < size_Deck_c; i++) { 
      g1.feed_from_back(Deck_c.discard(0)); 
     } 
     return; 

    } 

    return; 
} 


void turn(Hand &g1, Hand &g2, Hand &Deck_c){ 
    if (g1.void_hand()) { 
     return; 
    } 
    Card discarded_card = g1.discard(0); 
    cout << g1.get_name() << " discard:" << endl << " "; 
    cout << discarded_card; 
    Deck_c.append_card(discarded_card); 
    check(discarded_card, g1, g2, Deck_c); 
    turn(g2,g1,Deck_c); 

} 

int main (int argc, char * const argv[]) { 
    // inizializzo il mazzo 
    Deck Deck1; 
    Deck1.shuffle(); 


    Hand hand1("Giovanni"); 
    Hand hand2("Pietro"); 
    Hand central_deck; 

    // inizializzo le mani 
    for (int i = 0; i < 52/2; i++) { 
     hand1.append_card(Deck1.get_card(i)); 
    } 
    for (int i = 52/2; i < 52; i++) { 
     hand2.append_card(Deck1.get_card(i)); 
    } 

    turn(hand1, hand2, central_deck); 
    //turn(mano1, mano2, mazzo_centrale); 
    cout << "mazzo" << endl; 
    central_deck.print_hand(); 

    cout << "mano 1" << endl; 
    hand1.print_hand(); 
    cout << "mano 2" << endl; 
    hand2.print_hand(); 




    std::cout << "Hello, World!\n"; 
    return 0; 
} 
+2

如果您想要進行代碼審查 - 請考慮發佈在codereview.stackexchange.com上,否則請提出一些問題,這只是太糟糕了。 – littleadv

+1

什麼問題? – AJG85

回答

2

這是一個良好的開端到C++的職業生涯。這些類都經過深思熟慮,算法很簡單。我唯一的風格批評是你的成員有一個不一致的命名約定。變量通常有一個小寫的第一個字母。有那麼多不同風格的休息即駱駝套管,M_前綴,等等...

我會改變:

class Card{ 
private: 
    int Number; 
    string Suit; 
public: 
    Card(); 
    Card(int,string); 
    int get_number(); 
    string get_suit(); 
    void assign_values(int,string); 
    friend ostream &operator<<(ostream&, const Card&); 
}; 

class Card{ 
private: 
    int number; 
    string suit; 
public: 
    Card(); 
    Card(int,string); 
    int get_number(); 
    string get_suit(); 
    void assign_values(int,string); 
    friend ostream &operator<<(ostream&, const Card&); 
}; 

我看到你這樣做在別人身上,所以只要一致。無論如何,這是一些寫得很好的代碼。另外,如果我打算使用多個文件,我通常會避免使用像瘟疫這樣的指令。

+0

謝謝:D什麼是瘟疫? – Pella86

+0

使用指令很難跟上和混淆你的代碼的讀者。使用std :: string或std :: cout等等,而不是使用std是更好的風格。讀者不會知道你沒有定義一個名爲string或類的類,而不是使用std庫。 –

+0

哦oky ... @Jonathan Henson ...不要錯以爲什麼...我問你是什麼意思,我是歐洲人,真的很冒犯告訴我,375'000' 000名受害者不能被忽略......無論如何...... thx再次爲答案。我已經改變了:)壞習慣 – Pella86