Advertisement
ymliang

6-2、a113: 99遊戲

Jul 11th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Player//玩家的結構
  4. {
  5.     char name;//名字
  6.     int index;//出牌數
  7.     string card[13];//牌內容
  8. }player[4];
  9.  
  10. int main()
  11. {
  12.     for(int i = 0 ; i < 4 ; i++)
  13.     {//依序讀入4位玩家資料
  14.         cin >> player[i].name;
  15.         player[i].index = 0;
  16.         for(int j = 0 ; j < 13 ; j++)
  17.             cin >> player[i].card[j];
  18.     }
  19.     int total = 0;//總數字
  20.     int i = 0;
  21.     int add = 1;//出牌方向,1為向下,-1為向上
  22.     while(1)
  23.     {
  24.         char newcard = player[i].card[player[i].index][0];
  25.         if (newcard == 'A')
  26.         {//A為歸零牌
  27.             player[i].index++;
  28.             total = 0;
  29.             if (player[i].index == 13)
  30.             {//該玩家出牌結束
  31.                 cout << player[i].name << endl;
  32.                 cout << total << endl;
  33.                 return 0;
  34.             }
  35.         }
  36.         else if (newcard == '4')
  37.         {//4為迴轉牌
  38.             player[i].index++;
  39.             if (player[i].index == 13)
  40.             {//該玩家出牌結束
  41.                 cout << player[i].name << endl;
  42.                 cout << total <<endl;
  43.                 return 0;
  44.             }
  45.             add *= -1;//轉向
  46.         }
  47.         else if (newcard == '5')
  48.         {//5為指定牌(出此牌不用累加數字,換下一位出牌者出牌)
  49.             player[i].index++;
  50.             if (player[i].index == 13)
  51.             {//該玩家出牌結束
  52.                 cout << player[i].name << endl;
  53.                 cout << total << endl;
  54.                 return 0;
  55.             }
  56.         }
  57.         else if (newcard == '1')
  58.         {//10為加減10牌(將累加數字加10,但如累加數字加10會超過99則減10)
  59.             player[i].index++;
  60.             if (total <= 89)
  61.                 total += 10;
  62.             else total -= 10;
  63.             if (player[i].index == 13)
  64.             {//該玩家出牌結束
  65.                 cout << player[i].name << endl;
  66.                 cout << total << endl;
  67.                 return 0;
  68.             }
  69.         }
  70.         else if (newcard=='J')
  71.         {//J為跳過牌
  72.             player[i].index++;
  73.             if (player[i].index == 13)
  74.             {//該玩家出牌結束
  75.                 cout << player[i].name << endl;
  76.                 cout << total <<endl;
  77.                 return 0;
  78.             }
  79.         }
  80.         else if (newcard=='Q')
  81.         {//Q為加減20牌(將累加數字加20,但如累加數字加20會超過99則減20)
  82.             player[i].index++;
  83.             if (total <= 79)
  84.                 total += 20;
  85.             else total -= 20;
  86.             if (player[i].index == 13)
  87.             {//該玩家出牌結束
  88.                 cout << player[i].name << endl;
  89.                 cout << total << endl;
  90.                 return 0;
  91.             }
  92.         }
  93.         else if (newcard=='K')
  94.         {//K為99牌(出此牌不管累加數字為何,直接累加到99,當累加數字已為99時可以再出K,累加數字保持99)
  95.             player[i].index++;
  96.             total = 99;
  97.             if (player[i].index == 13)
  98.             {//該玩家出牌結束
  99.                 cout << player[i].name << endl;
  100.                 cout << total << endl;
  101.                 return 0;
  102.             }
  103.         }
  104.         else
  105.         {//其他牌
  106.             player[i].index++;
  107.             total += newcard - '0';
  108.             if (total > 99)
  109.             {
  110.                 cout << player[i].name << endl;
  111.                 cout << 13 - player[i].index << endl;
  112.                 return 0;
  113.             }
  114.         }
  115.         i += add; //換下一個玩家
  116.         if (i == -1) i = 3;
  117.         else if (i == 4) i = 0;
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement