C编程之旅:自动发牌程序

// card.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
using namespace std;

#define MAX_LEN 54
#define SEND_END 0
#define SEND_NOT_END 1

void Input(int* arr, int nLen);
void GetNum(int* arr, int nLen, int* a);
void PrintCard(int a);
void Output(int a, int b, int c, int d, int e);
int SendCard(int* arr, int nLen, int* a, int* b, int* c, int* d, int* e);

int main()
{
 int nArr[MAX_LEN] = {0};
 int nRet = 0;
 int a = 0;
 int b = 0;
 int c = 0;
 int d = 0;
 int e = 0;

 Input(nArr, MAX_LEN);

 while (true)
 {
  cout << "press any key to send five cards..." << endl;
  getch();
  
  a = -1;
  b = -1;
  c = -1;
  d = -1;
  e = -1;

    nRet = SendCard(nArr, MAX_LEN, &a, &b, &c, &d, &e);
  Output(a, b, c, d, e);  

  if (SEND_END == nRet)
  {
   break;
  }
 }

 return 0;
}

void Input(int* arr, int nLen)
{
 int i = 0;

 for (i = 0; i < nLen; i++)
 {
  arr[i] = 0;
 }
}

int SendCard(int* arr, int nLen, int* a, int* b, int* c, int* d, int* e)
{
 GetNum(arr, nLen, a);
 GetNum(arr, nLen, b);
 GetNum(arr, nLen, c);
 GetNum(arr, nLen, d);
 GetNum(arr, nLen, e);

 if (-1 == *e)
 {
  return SEND_END;
 }
 else
 {
  return SEND_NOT_END;
 }
}

void GetNum(int* arr, int nLen, int* a)
{
 int i = 0;

 while (true)
 {
  *a = rand() % 54;

  if (1 == arr[*a])
  {
   for (i = 0;i < nLen; i++)
   {
    if (1 != arr[i])
    {
     break;
    }
   }

   if (i < nLen)
   {
    continue;
   }
   else
   {
    *a = -1;
    break;
   }
  }
  else
  {
   arr[*a] = 1;
   break;
  }
 }

 //arr[*a] = 1;
}

void Output(int a, int b, int c, int d, int e)
{
 PrintCard(a);
 PrintCard(b);
 PrintCard(c);
 PrintCard(d);
 PrintCard(e);
}

void PrintCard(int a)
{
 int  n = 0;
 char ch = '/0';

 if (-1 == a)
 {
  cout << "没有牌" << endl;
  return;
 }

 if (53 == a)
 {
  cout << "大王" << endl;
 }
 else if (52 == a)
 {
  cout << "小王" << endl;
 }
 else
 {
  switch (a % 4)
  {
  case 0:
   cout << "黑桃";
   break;

  case 1:
   cout << "红桃";
   break;

  case 2:
   cout << "梅花";
   break;

  case 3:
   cout << "方块";
   break;

  default:
   cout << "error";
   break;
  }

  n = a / 4 + 1;

  switch (n)
  {
  case 1:
   ch = 'A';
   break;
   
  case 11:
   ch = 'J';
   break;

  case 12:
   ch = 'Q';
   break;

  case 13:
   ch = 'K';
   break;

  default:
   cout << n << endl;

   return;
  }

  cout << ch << endl;  
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值