Advertisement
medal893

Poker_element.h

Dec 16th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int player[8][13];
  7. int card[52];
  8. int card_index;
  9. int player_card_index[8];
  10.  
  11. //撲克牌遊戲的初始化設定
  12. //完成產生一副新牌、清空所有玩家的牌等動作
  13. void initial()
  14. {
  15.      int i,j;
  16.      for(i=0;i<=3;i++)
  17.      {
  18.         for(j=0;j<=12;j++)
  19.         {
  20.            player[i][j]=-1;
  21.         }
  22.      }
  23.      
  24.      for(i=0;i<=51;i++)
  25.      {
  26.         card[i]=i;
  27.      }
  28.      
  29.      for(i=0;i<=3;i++)
  30.      {
  31.         player_card_index[i]=0;
  32.      }
  33.        
  34.      card_index = 0;
  35.      srand( time(NULL) );
  36. }
  37.  
  38. //將兩張撲克牌交換
  39. void swap2card(int a, int b)
  40. {
  41.    int temp;
  42.    temp = card[a];
  43.    card[a] = card[b];
  44.    card[b] = temp;
  45. }
  46.  
  47. //洗牌(以每次交換兩張牌的方式進行打亂牌的動作)
  48. void shuffle(int n)
  49. {
  50.    int i;
  51.    int sa, sb;
  52.    for(i=1;i<=n;i++)
  53.    {
  54.       sa = rand() % 52;
  55.       sb = rand() % 52;
  56.       swap2card(sa, sb);  
  57.    }
  58. }
  59.  
  60. //取得撲克牌花色的字元碼
  61. char getsuit(int card_no)
  62. {
  63.    char sign[4]={6,3,4,5};
  64.    char suit;
  65.    
  66.    suit = sign[ card_no/13 ];
  67.    
  68.    return suit;    
  69. }
  70.  
  71. //取得撲克牌的點數
  72. int getpoint(int card_no)
  73. {
  74.    int point;
  75.    
  76.    point = card_no%13 + 1;  
  77.    return point;    
  78. }
  79.  
  80. //印出整副撲克牌,依序由第0張至第51張共52張撲克牌的花色與點數
  81. void display_allcard()
  82. {
  83.    int i;
  84.    char suit;
  85.    int point;
  86.    
  87.    for(i=0;i<=51;i++)
  88.    {
  89.      suit = getsuit( card[i] );
  90.      point = getpoint( card[i] );
  91.      cout << "(" << setw(2) << i << ")card no. " << setw(2) << card[i] << " " << suit << setw(2) << point << endl;
  92.    }
  93.    cout << "------------------------------------------------" << endl << endl;
  94. }
  95.  
  96. //某一位玩家抽取一張牌
  97. int draw(int player_no)
  98. {
  99.    int card_no;
  100.    int index;
  101.    
  102.    card_no = card[card_index];
  103.    card_index = card_index + 1;
  104.    
  105.    index = player_card_index[player_no];
  106.    player[player_no][index] = card_no;
  107.    player_card_index[player_no] = player_card_index[player_no] + 1;
  108.    
  109.    return card_no;
  110. }
  111.  
  112. //顯示某一位玩家手中的所有牌
  113. void display_player_card(int player_no)
  114. {
  115.    int i;
  116.    int index;
  117.    char suit;
  118.    int point;
  119.    
  120.    cout << "player " << player_no << endl;
  121.    index = player_card_index[player_no];
  122.    for(i=0;i<index;i++)
  123.    {
  124.      suit = getsuit( player[player_no][i] );
  125.      point = getpoint( player[player_no][i] );
  126.      cout << suit << setw(2) << point << "  " ;  
  127.    }
  128.    cout << endl;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement