天天对着电脑编程,呃,有时候挺枯燥的,所以还是找点乐子吧。捡到一本好书,里面是一些各种各样有趣的小算法的书。COPY下,顺便练练手,活动活动大脑。
代码挺简单的,就是调用随机函数生成扑克牌的随机序列,然后输出。
虽然这段代码挺简单的,但是还是加点说明吧。我也理一下思路,顺便减少大家理解代码的时间。
wSuit[4]这个数组里储存的是扑克牌的花色,wFace[13]存的是扑克牌的值。wDeck[4][13]用来存放随机生成的扑克牌序列。
shuffle这个函数的作用:让card这个值从1循环到52,在每一次循环中,随机生成与前面生成不同的行和列,然后wDeck[列][行]=card的当前值。
deal这个函数用来输出,还是让card从1循环到52,找到对应的值为card的wDeck[列][行],然后输出行和列,当然不是直接输出行和列的值,而是将其转化为wSuit[4]和wFace[13]的值。
输出函数看上去可能不是那么好理解,它是先找到特定的card值,然后再去找行和列,这样来输出的。
1 / poker.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5
6 #include <stdlib.h>
7 #include <time.h>
8
9 void shuffle(int wDeck[4][13]);
10 void deal(int wDeck[4][13], char*wFace[13], char*wSuit[4]);
11
12 void main()
13 {
14 char*wSuit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
15 char*wFace[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight",
16 "Nine", "Ten", "Jack", "Queen", "King"};
17 int wDeck[4][13] = {0};
18
19 srand(time(NULL));
20
21 shuffle(wDeck);
22 deal(wDeck, wFace, wSuit);
23
24 system("PAUSE");
25 }
26
27 void shuffle(int wDeck[4][13])
28 {
29 int r;
30 int card, row, column;
31
32 for(card =1; card <=52; card ++)
33 {
34 r = rand();
35 row = r%4;
36 r = rand();
37 column = r%13;
38
39 while(wDeck[row][column] !=0)
40 {
41 r = rand();
42 row = r%4;
43 r = rand();
44 column = r%13;
45 }
46
47 wDeck[row][column] = card;
48 }
49 }
50
51 void deal(int wDeck[4][13], char*wFace[13], char*wSuit[4])
52 {
53 char c;
54 int card, row, column;
55
56 for (card =1; card <=52; card ++)
57 {
58 for (row =0; row <=3; row ++)
59 {
60 for(column =0; column <=12; column ++)
61 {
62 if (wDeck[row][column] == card)
63 {
64 if (card %3==0)
65 {
66 c ='\n';
67 }
68 else
69 {
70 c ='\t';
71 }
72 printf("%5s of %-8s%c", wFace[column], wSuit[row], c);
73 }
74 }
75 }
76 }
77 }
2 //
3
4 #include "stdafx.h"
5
6 #include <stdlib.h>
7 #include <time.h>
8
9 void shuffle(int wDeck[4][13]);
10 void deal(int wDeck[4][13], char*wFace[13], char*wSuit[4]);
11
12 void main()
13 {
14 char*wSuit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
15 char*wFace[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight",
16 "Nine", "Ten", "Jack", "Queen", "King"};
17 int wDeck[4][13] = {0};
18
19 srand(time(NULL));
20
21 shuffle(wDeck);
22 deal(wDeck, wFace, wSuit);
23
24 system("PAUSE");
25 }
26
27 void shuffle(int wDeck[4][13])
28 {
29 int r;
30 int card, row, column;
31
32 for(card =1; card <=52; card ++)
33 {
34 r = rand();
35 row = r%4;
36 r = rand();
37 column = r%13;
38
39 while(wDeck[row][column] !=0)
40 {
41 r = rand();
42 row = r%4;
43 r = rand();
44 column = r%13;
45 }
46
47 wDeck[row][column] = card;
48 }
49 }
50
51 void deal(int wDeck[4][13], char*wFace[13], char*wSuit[4])
52 {
53 char c;
54 int card, row, column;
55
56 for (card =1; card <=52; card ++)
57 {
58 for (row =0; row <=3; row ++)
59 {
60 for(column =0; column <=12; column ++)
61 {
62 if (wDeck[row][column] == card)
63 {
64 if (card %3==0)
65 {
66 c ='\n';
67 }
68 else
69 {
70 c ='\t';
71 }
72 printf("%5s of %-8s%c", wFace[column], wSuit[row], c);
73 }
74 }
75 }
76 }
77 }
输出结果:(每次输出都会不同)