扑克牌洗牌发牌过程模拟
编写一个模拟人工洗牌的程序,将洗好的牌分别发给四个人。使用结构card来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。对每个人的牌要...
编写一个模拟人工洗牌的程序,将洗好的牌分别发给四个人。
使用结构card 来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。
对每个人的牌要按桥牌的规则输出。即一个人的牌要先按牌的花色(顺序为梅花、方块、红心和黑桃)进行分类,同一类的牌要再按A、K、Q、J、…、3、2牌的大小顺序排列。另发牌应按四个人的顺序依次分发。 展开
使用结构card 来描述一张牌,用随机函数来模拟人工洗牌的过程,最后将洗好的52张牌顺序分别发给四个人。
对每个人的牌要按桥牌的规则输出。即一个人的牌要先按牌的花色(顺序为梅花、方块、红心和黑桃)进行分类,同一类的牌要再按A、K、Q、J、…、3、2牌的大小顺序排列。另发牌应按四个人的顺序依次分发。 展开
3个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const int TOTAL = 52;
const int PLAYERS = 4;
const int MAXSIZE = TOTAL/PLAYERS;
const int TYPES = 4;
typedef struct card {
int type; // type 0:梅花,1:方片,2:红桃,3:黑桃
int point; // point = 2 -- 14
}CARD;
void sort(int a[][TYPES][MAXSIZE]) {
int i,j,k,m,n,t,size;
for(m = 0; m < PLAYERS; ++m) {
for(n = 0; n < TYPES; ++n) {
for(size = 0; a[m][n][size]; ++size);
for(i = 0; i < size - 1; ++i) {
k = i;
for(j = i + 1; j < size; ++j) {
if(a[m][n][k] < a[m][n][j]) k = j;
}
if(k != i) {
t = a[m][n][k];
a[m][n][k] = a[m][n][i];
a[m][n][i] = t;
}
}
}
}
}
void show(int a[][TYPES][MAXSIZE]) {
int i,j,k;
char type[TYPES] = {'\5','\4','\3','\6'};
char player[PLAYERS][6] = {"EAST","SOUTH","WEST","NORTH"};
char point[] = "JQKA";
for(k = 0; k < PLAYERS; ++k) {
printf("<%s>\n",player[k]);
for(i = 0; i < TYPES; ++i) {
printf(" %c ",type[i]);
for(j = 0; j < MAXSIZE && a[k][i][j];++j) {
if(a[k][i][j] < 11) printf("%d ",a[k][i][j]);
else printf("%c ",point[a[k][i][j] - 11]);
}
printf("\n");
}
printf("\n");
}
}
int Has(int a[][TYPES][MAXSIZE],CARD Card) {
int i,j;
for(i = 0; i < PLAYERS; ++i) {
for(j = 0; j < MAXSIZE && a[i][Card.type][j]; ++j)
if(a[i][Card.type][j] == Card.point)
return 1;
}
return 0;
}
int isFull(int a[][MAXSIZE]) {
int n = 0,i,j;
for(i = 0; i < TYPES; ++i) {
for(j = 0; a[i][j] && j < MAXSIZE; ++i)
++n;
}
return n == MAXSIZE;
}
int addCard(int a[PLAYERS][TYPES][MAXSIZE],CARD Card) {
int i = 0;
if(Has(a,Card)) return 0;
int who = rand()%PLAYERS;
while(isFull(a[who])) who = (who + 1)%PLAYERS;
while(a[who][Card.type][i]) ++i;
a[who][Card.type][i] = Card.point;
return 1;
}
int main() {
int i,playingCard[PLAYERS][4][MAXSIZE] = {0};
CARD t;
srand((unsigned)time(NULL));
for(i = 0; i < TOTAL; ++i) {
t.type = rand()%4;
t.point = rand()%13 + 2;
if(addCard(playingCard,t) == 0) --i;
}
sort(playingCard);
show(playingCard);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include"stdio.h"
#include"iostream"
#include "cstdlib"
#include"time.h"
using namespace std;
struct Card
{
int number;
int type;
};
string getType(int x)
{
string type;
switch(x)
{
case 1:type = "黑桃";break;
case 2:type = "红桃";break;
case 3:type = "梅花";break;
case 4:type = "方片";break;
}
return type;
}
string getNumber(int x)
{
string number;
switch(x)
{
case 1:number = "2";break;
case 2:number = "3";break;
case 3:number = "4";break;
case 4:number = "5";break;
case 5:number = "6";break;
case 6:number = "7";break;
case 7:number = "8";break;
case 8:number = "9";break;
case 9:number = "10";break;
case 10:number = "J";break;
case 11:number = "Q";break;
case 12:number = "K";break;
case 13:number = "A";break;
}
return number;
}
int cmp(const void *a , const void *b)
{
Card *c1 = (Card *)a;
Card *c2 = (Card *)b;
if(c1->type != c2->type) return c1->type - c2->type;
else return c2->number - c1->number;
}
int main()
{
int m = 0;
Card card[53];
int flag[53] = {0};
Card poker[4][13];
srand((unsigned)time(0));
for(int i=1; i<=4; i++){
for(int j=1; j<=13; j++){
card[(i-1)*13+j] = {j,i};
}
}
for(int i=0;i<4;i++){
m = 0;
while(m < 13){
int x = rand()%52+1;
if(flag[x]==0){
poker[i][m] = card[x];
flag[x] = 1;
m++;
}
}
}
for(int i=0; i<4; i++){
qsort(poker[i],13,sizeof(poker[0][0]),cmp);
cout<<"=========玩家"<<i+1<<"============\n";
for(int j=0; j<13; j++){
cout<<getType(poker[i][j].type)<<" "<<getNumber(poker[i][j].number)<<"\n";
}
}
}
c++写的, 如此可否?
#include"iostream"
#include "cstdlib"
#include"time.h"
using namespace std;
struct Card
{
int number;
int type;
};
string getType(int x)
{
string type;
switch(x)
{
case 1:type = "黑桃";break;
case 2:type = "红桃";break;
case 3:type = "梅花";break;
case 4:type = "方片";break;
}
return type;
}
string getNumber(int x)
{
string number;
switch(x)
{
case 1:number = "2";break;
case 2:number = "3";break;
case 3:number = "4";break;
case 4:number = "5";break;
case 5:number = "6";break;
case 6:number = "7";break;
case 7:number = "8";break;
case 8:number = "9";break;
case 9:number = "10";break;
case 10:number = "J";break;
case 11:number = "Q";break;
case 12:number = "K";break;
case 13:number = "A";break;
}
return number;
}
int cmp(const void *a , const void *b)
{
Card *c1 = (Card *)a;
Card *c2 = (Card *)b;
if(c1->type != c2->type) return c1->type - c2->type;
else return c2->number - c1->number;
}
int main()
{
int m = 0;
Card card[53];
int flag[53] = {0};
Card poker[4][13];
srand((unsigned)time(0));
for(int i=1; i<=4; i++){
for(int j=1; j<=13; j++){
card[(i-1)*13+j] = {j,i};
}
}
for(int i=0;i<4;i++){
m = 0;
while(m < 13){
int x = rand()%52+1;
if(flag[x]==0){
poker[i][m] = card[x];
flag[x] = 1;
m++;
}
}
}
for(int i=0; i<4; i++){
qsort(poker[i],13,sizeof(poker[0][0]),cmp);
cout<<"=========玩家"<<i+1<<"============\n";
for(int j=0; j<13; j++){
cout<<getType(poker[i][j].type)<<" "<<getNumber(poker[i][j].number)<<"\n";
}
}
}
c++写的, 如此可否?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
咋模拟?用软件?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询