1 /*
2 * Author: houcy
3 * Date: 2010-01-10
4 *
5 * This file is used to simulate shuffling poker.
6 * There are four players and 52 piece of cards.
7 * The function shuffle shuffles pokers using linear swap algorithm in O(n) time.
8 * Shuffle 52 times and give the last number to one player, then continue shuffling the rest cards.
9 *
10 */
11 #include <vector>
12 #include <iostream>
13 #include <fstream>
14 #include <ctime>
15 using namespace std;
16
17 int record[53][4]={0}; // to record the times of card for every players
18 int play[4][13]={0}; // to record the shuffle result
19 vector<int> cards;// contain 52 piece of cards
20
21 void __swap(int& x, int& y)
22 {
23 if ( x!=y)
24 {
25 int tmp=x;
26 x = y;
27 y = tmp;
28 }
29 }
30
31
32 void __shuffle(int begin, int end)
33 {
34 if(begin==end)
35 return;
36
37 for(int n=begin; n<end; n++)
38 __swap( cards[n], cards[begin + rand()%(n-begin+1)] ); // linear swap two numbers
39 }
40
41 void shuffle(int record[][4])
42 {
43 __shuffle(0,52);
44 for(int i=0; i<52; i++)
45 {
46 int who=i%4;
47 play[who][(52-i)%13]=cards[52-i-1]; // who has which card
48 record[cards[52-i-1]][who]++; // keep record of every card for every player
49 }
50 }
51
52 int main()
53 {
54 srand(time(0));
55 clock_t Start, End;
56 for(int i=1; i<=52; i++)
57 cards.push_back(i);
58 Start=clock();
59
60 for(int r=0; r<1000; r++)
61 {
62 shuffle(record);
63 }
64 End=clock();
65
66 cout<<"Usage:\n1-13 stands for 'heart'\n14-26 stands for 'spade'\n27-39 stands for 'diamond'\n40-52 stands for 'club'\n\nRecord for cards after 1000 times of shuffling:\nCard\tPlayer1\tPlayer2\tPlayer3\tPlayer4"<<endl;
67 for(int line=1; line<53; line++)
68 {
69 cout<<line<<"\t";
70 for(int rank=0; rank<4; rank++)
71 {
72 cout<<record[line][rank]<<"\t";
73 }
74 cout<<endl;
75 }
76
77 cout<<"Total Time Used: "<<(double)(End-Start)/CLOCKS_PER_SEC<<"(s)"<<endl;
78 }
程序运行结果:
Usage:
1-13 stands for 'heart'
14-26 stands for 'spade'
27-39 stands for 'diamond'
40-52 stands for 'club'
Record for cards after 1000 times of shuffling:
Card Player1 Player2 Player3 Player4
1 240 265 227 268
2 251 246 260 243
3 223 265 247 265
4 257 234 277 232
5 257 255 233 255
6 249 256 243 252
7 250 235 261 254
8 246 240 269 245
9 278 272 220 230
10 233 240 278 249
11 244 238 258 260
12 234 258 236 272
13 246 225 269 260
14 246 267 233 254
15 240 254 255 251
16 235 291 248 226
17 286 223 237 254
18 242 231 267 260
19 255 258 247 240
20 275 258 245 222
21 252 242 263 243
22 263 280 222 235
23 232 268 255 245
24 243 242 263 252
25 264 251 244 241
26 263 243 255 239
27 261 244 245 250
28 249 241 235 275
29 266 240 254 240
30 243 260 242 255
31 268 251 232 249
32 259 261 252 228
33 242 242 266 250
34 233 260 275 232
35 259 250 234 257
36 232 267 240 261
37 253 242 243 262
38 252 244 231 273
39 245 242 257 256
40 243 262 251 244
41 265 268 242 225
42 259 237 244 260
43 237 242 249 272
44 247 230 260 263
45 261 220 267 252
46 266 264 240 230
47 234 268 245 253
48 262 248 243 247
49 233 266 260 241
50 209 245 291 255
51 273 251 231 245
52 245 218 259 278
Total Time Used: 0.01(s)