返回列表 發帖

賽馬程式 (六)

本帖最後由 周政輝 於 2018-5-19 15:19 編輯

1. 比對比賽結果與玩家下注, 判斷玩家贏了錢還是輸了錢.
2. 對變數balance做加減, 使可用餘額會隨著玩家輸贏而增減.

規則如下:
若最後勝出的選手與玩家下注相同, 玩家贏得下注金3倍的錢.
反之, 玩家損失下注金.



  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<time.h>
  4. using namespace std;
  5. int main()
  6. {
  7.   int times=1;
  8.   int point = 0;
  9.   int select = 0;
  10.   int buy = 0;
  11.   int bet = 0;
  12.   bool isStart= true;
  13.   int my_select = 0;
  14.   int win = 0; // 判斷是哪一批馬勝利
  15.   while(true)
  16.   {
  17.     int horse1=0, horse2=0, horse3=0, horse4=0, random=0;
  18.     srand(time(NULL));
  19.    
  20.     cout<<"「好事成雙」賽馬場   " << "第 " <<times << "局 "<<endl;
  21.     cout<<"------------------------------------------------------------------------| 終點"<<endl;
  22.     cout<<"◆"<<endl;
  23.     cout<<"★"<<endl;
  24.     cout<<"▲"<<endl;
  25.     cout<<"●"<<endl;
  26.     cout<< "可用帳戶餘額:" <<  point << endl;
  27.     while(isStart)
  28.     {
  29.      
  30.        cout<< "(1)儲值 (2) 下注 (3)離開 請選擇:";
  31.        cin >> select;
  32.        switch(select)
  33.        {
  34.         case 1:
  35.            cout << "買入:";
  36.            cin >> buy;
  37.            // 三元運算子
  38.            point += (buy<=0) ? point : buy;
  39.            cout<< "可用帳戶餘額:" <<  point << endl;
  40.            break;
  41.         case 2:
  42.                if(point >=0)  
  43.               {
  44.                cout << "下注:";
  45.                cin >> bet;
  46.                string error = (bet>point || bet<=0) ? "輸入錯誤":"";
  47.                if(error == "error")
  48.                {
  49.                 break;
  50.                }
  51.                else
  52.                {
  53.                  point = point - bet;
  54.                  cout<< "可用帳戶餘額:" <<  point << endl;
  55.                  cout << "請選擇以下的馬匹:" << endl;
  56.                  cout << "(1)◆ (2)★ (3)▲ (4)● ";
  57.                  cin >> my_select;
  58.                  cout << "比賽正式開始!" << endl;
  59.                  isStart = false;
  60.                }
  61.                 break;
  62.               }
  63.               else
  64.               {
  65.                cout << "餘額不足,請先進行儲值" << endl;
  66.                continue;
  67.               }

  68.        }
  69.     }
  70.    
  71.    
  72.     system("pause");
  73.     system("cls");
  74.    
  75.     while(horse1!=75 && horse2!=75 && horse3!=75 && horse4!=75)
  76.     {
  77.         random=rand()%4;   //產生0~3之隨機亂數
  78.         if(random==0) {
  79.            horse1++;
  80.         }  
  81.         else if(random==1) {
  82.            horse2++;
  83.         }
  84.         else if(random==2) {
  85.            horse3++;
  86.         }
  87.         else if(random==3) {
  88.            horse4++;
  89.         }
  90.         cout<<"比賽進行中"<<endl;
  91.         cout<<"------------------------------------------------------------------------| 終點"<<endl;
  92.       
  93.         for(int i=1; i<=horse1; i++) {
  94.            cout<<" ";
  95.         }   
  96.         cout<<"◆"<<endl;
  97.         
  98.         for(int i=1; i<=horse2; i++) {
  99.            cout<<" ";
  100.         }   
  101.         cout<<"★"<<endl;
  102.         
  103.          for(int i=1; i<=horse3; i++) {
  104.            cout<<" ";
  105.         }   
  106.         cout<<"▲"<<endl;
  107.         
  108.          for(int i=1; i<=horse4; i++) {
  109.            cout<<" ";
  110.         }   
  111.         cout<<"●"<<endl;
  112.         system("cls");      
  113.     }
  114.     times++;
  115.     system("cls");     
  116.     if(horse1 == 75)
  117.     {
  118.       cout<<"比賽結束,由◆先馳得點"<<endl;
  119.       win = 1;
  120.     }
  121.     else if(horse2 == 75)
  122.     {
  123.       cout<<"比賽結束,由★先馳得點"<<endl;
  124.       win = 2;
  125.     }
  126.     else if(horse3 == 75)
  127.     {
  128.       cout<<"比賽結束,由▲先馳得點"<<endl;
  129.       win = 3;
  130.     }
  131.     else if(horse4 == 75)
  132.     {
  133.       cout<<"比賽結束,由●先馳得點"<<endl;
  134.       win = 4;
  135.     }
  136.    
  137.     if(my_select == win)
  138.     {
  139.      point = point + (bet*3);
  140.      cout << "恭喜您獲得勝利!,目前金額如下" << endl;
  141.      cout << point << endl;
  142.     }
  143.     else
  144.     {
  145.      cout << "賭注失敗,目前金額如下" << endl;
  146.      cout << point << endl;
  147.     }
  148.     isStart = true;
  149. }

  150.     system("pause");
  151.     return 0;
  152.       
  153. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表