2014-10-28 37 views
-2

所以我一次性尋求幫助後,就更加努力了我的程序,這就是我現在擁有的。更多2D撲克牌數組的幫助C++

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <ctime> 
#include <iomanip> 

using namespace std; 

void main() 
{ 
const int ROWS = 4; 
string suits[ROWS] = 
{ 
    "Diamonds", "Clubs", "Hearts", "Spades" 
}; 

const int COLS = 13; 
string faces[COLS] = 
{ 
     "Ace", "Deuce", "Three", "Four", "Five", "Six", 
    "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" 
}; 

int deck[ROWS][COLS] = 
{ 
    {1,2,3,4,5,6,7,8,9,10,11,12,13}, 
    {14,15,16,17,18,19,20,21,22,23,24,25,26}, 
    {27,28,29,30,31,32,33,34,35,36,37,38,39}, 
    {40,41,42,43,44,45,46,47,48,49,50,51,52} 
}; 

for(int iii = 0; iii < ROWS; iii++) 
{ 
    for(int jjj = 0; jjj < COLS; jjj++) 
    { 

     cout << deck[iii][jjj] << " " << faces[jjj] << " of " << suits[iii] << endl; 
    } 
} 

srand((unsigned) time(0)); 

for(int c = 0; c < ROWS; c++) 
{ 
    for(int d = 0; d < COLS; d++) 
    { 
     int face = rand() % 13; 
     int suit = rand() % 4; 

     cout << left << setw(5) << " " << faces[face] << " of " << suits[suit] << endl; 
     int e = deck[suit][face]; 
     deck[suit][face] = deck[c][d]; 
     deck[c][d] = e; 
     cout << deck[c][d]; 





    } 
} 

}

當我運行該程序的第一循環簡單地列出了甲板爲了偉大工程:

1 Ace of Diamonds 
2 Deuce of Diamonds 
3 Three of Diamonds 
4 Four of Diamonds 
5 Five of Diamonds 
6 Six of Diamonds 
7 Seven of Diamonds 
8 Eight of Diamonds 
9 Nine of Diamonds 
10 Ten of Diamonds 

等。第二環路然而應該洗牌,然後再次打印甲板:

Nine of Clubs 
22  King of Hearts 
39  Queen of Spades 
51  Seven of Spades 
46  Ace of Spades 
40  Six of Spades 
45  Three of Spades 
42  Seven of Hearts 
33  Four of Diamonds 
46  Five of Diamonds 
40  Seven of Diamonds 
42  Queen of Clubs 
25  Five of Clubs 
18  Four of Hearts 
30  Ace of Clubs 
30  Three of Clubs 
16  Jack of Clubs 
24  Deuce of Spades 
41  Eight of Diamonds 
33  Queen of Spades 
3  Seven of Hearts 
8  Jack of Hearts 
37  Eight of Diamonds 
19  Four of Clubs 
24  Queen of Hearts 
38  Three of Hearts 
29  Nine of Clubs 
37  Ace of Diamonds 
22  Ace of Hearts 
37  Queen of Diamonds 
25  Four of Diamonds 
9  Four of Diamonds 
31  Three of Hearts 
37  Nine of Clubs 
27  Nine of Clubs 
34  Ten of Hearts 
36  Five of Hearts 
9  Jack of Clubs 
24  Five of Spades 
44  Seven of Clubs 
3  Six of Hearts 
31  Jack of Spades 
50  Ten of Diamonds 
40  Six of Clubs 
33  Eight of Diamonds 
23  Ten of Hearts 
36  Ace of Hearts 
26  Six of Spades 
23  Nine of Clubs 
35  Seven of Clubs 
5  Five of Hearts 
1  Five of Diamonds 
10Press any key to continue . . . 

問題1:所有2D陣列的數目的不正確地與一維數組的字符串對應。例如,九號俱樂部的號碼是正確的,號碼是正確的,直到你找到一號碼,這是三個俱樂部並不正確,並從那裏一切搞亂。

問題2:數字和字符串沒有完全對齊,字符串開始首先打印,所以字符串數字在一個和一個以上。

問題3:一些數字和字符串重複我不想要的。我如何確保他們不會重複。

+0

至於對齊問題,請檢查['setw()'I/O流操縱器](http://en.cppreference.com/w/cpp/io/manip)。至於你的第一個問題,C++中的索引開始於'0',而不是'1'!你的第一副牌應該有'{0,1,2,3,4,5,6,7,8,9,10,11,12}'aso。 – 2014-10-28 17:02:11

+0

「更多幫助」不是一個問題。而且帖子中陳述的問題過於寬泛(沒有提及它不止一個問題)。請關注一個特定的問題。它不僅會幫助你在SO上得到答案,甚至可以自己解決問題。 – BartoszKP 2014-10-28 17:05:41

+0

使用調試器總是比使用StackOverflow查找問題的效率更高。 – 2014-10-28 17:25:18

回答

1

第一個問題的原因是打印輸出循環被打破。由於您使用iiijjj來索引deck[][]faces[]/suits[]對,因此只有在卡片全部按順序排列時,它才能正常工作,因爲它們在洗牌之前。這就是說,如果你想一秒鐘,那麼當你洗牌的時候,保持甲板作爲二維數組變得毫無意義。只需將其維持爲52張卡片的一維數組。無論如何,從051(包括這兩個數字)都可以將它們編號,從長遠來看,它將爲您節省很多痛苦和痛苦。

要轉換卡號回臉和西裝,你需要採取從deck陣列讀取的卡號,評估x/13得到一個索引到suit[]陣列,並x % 13得到索引到faces[]陣列。

最後但並非最不重要的是,你的洗牌並不完美。在實踐中,對於測試程序它可以,但是如果你想要認真的話,你應該修改它以使用Fisher-Yates算法。 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle一見鍾情,看起來完全相同,但對隨機數發生器參數的修改卻有所不同。

也避開圖書館提供的rand(),它通常很爛。我使用的是異或RNG http://en.wikipedia.org/wiki/Xorshift,因爲它非常好,因爲它足以讓蒙特卡洛使用,而且速度非常快。

0

試着將問題分解成更小的部分或步驟,然後運行/調試它們並查看結果是什麼。嘗試在有問題的循環中的每個步驟處放置cout語句(或用斷點調試),並打印(或在調試器中查看)涉及的不同變量。一步一步來嘗試確定發生了什麼問題。不要害怕將循環的執行步驟寫在紙上。這樣做後,有時錯誤會變得非常明顯。

如何調試是所有程序員必須學習的關鍵技能。如果你不能很好地調試你的代碼,那麼隨着你的程序變得更加複雜,這些問題只會變得更糟,更難以解決。