返回列表 發帖

賽馬程式 (七)& 優化程式

本帖最後由 陳品肇 於 2022-4-2 11:45 編輯

完成選項 (3)離開 的相對應程式碼
在離開時顯示戰果, 譬如:
"沒輸沒贏! 全身而退!"
"恭喜你! 這次總共贏了100元!"
"不好意思! 讓你損失了200元!"
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     // 開局第一場
  8.     // money 拿來作為輸贏的統計
  9.     int n =1,blance=0,money=0;
  10.     re:   
  11.     system("cls");
  12.     int a=0,b=0,c=0,d=0,r, option, buyin =0,bet,horse,winer ; // 每匹馬前進的進度   
  13.    
  14.     string words ="賽馬進行中";
  15.     // 代表馬的名子
  16.     string name[4] = {"◆","★","▲","●"};
  17.    
  18.     // 宣告一個暫存的變數
  19.     int position =0;
  20.     cout<<"「好事成雙」賽馬場 第"<<n<<"局"<<endl;
  21.     cout<<"------------------------------------------------------------------------| 終點"<<endl;
  22.     cout<<"◆"<<endl;
  23.     cout<<"★"<<endl;
  24.     cout<<"▲"<<endl;
  25.     cout<<"●"<<endl;
  26.     cout<<endl;
  27.     cout<<"可用餘額:"<<blance<<"元"<<endl;
  28.     cout<<endl;
  29.     cout<<"(1)買入 (2)下注 (3)離開 請選擇:";
  30.     cin>>option;
  31.    
  32.     // 如果買入 把錢加入餘額
  33.     if(option==1)
  34.     {
  35.         cout<<"買入:";
  36.         cin>>buyin;
  37.         blance += buyin;
  38.         goto re;
  39.     }else if(option==2)
  40.     {
  41.         cout<<"請下注:";
  42.         cin>>bet;
  43.         if(bet > blance)
  44.         {
  45.            cout<<"您餘額不足,請重新下注!"<<endl;            
  46.            system("pause");
  47.            goto re;
  48.         }else if(bet >0 && bet<=blance)
  49.         {
  50.            cout<<"(1)◆ (2)★ (3)▲(4)●請選擇:";
  51.            cin>>horse;
  52.            blance -= bet;
  53.            cout<<"即將開始賽馬!"<<endl;
  54.         }else
  55.         {
  56.            cout<<"您輸入錯誤,請重新下注!"<<endl;            
  57.            system("pause");
  58.            goto re;
  59.         }
  60.         
  61.     }else
  62.     {
  63.        // 離開      
  64.         goto end;
  65.     }
  66.    
  67.     system("pause"); // 按下enter 才開始
  68.     system("cls"); // 清空畫面
  69.     srand(time(NULL)); // 撒種子亂數
  70.   
  71.     // 當有任一匹跑到終點70 的時候就跳離迴圈
  72.     while(a<70 && b<70 && c<70 && d<70)
  73.     {  
  74.    
  75.       // 隨機挑一匹馬
  76.       r = rand()%4+1;
  77.       // 每匹馬 跑的進度
  78.       switch(r)
  79.       {
  80.           case 1:
  81.                a++;
  82.                break;
  83.           case 2:
  84.                b++;
  85.                break;
  86.           case 3:
  87.                c++;
  88.                break;
  89.           case 4:
  90.                d++;
  91.                break;
  92.       }
  93.       
  94.    
  95.       if(a==70 || b==70 || c==70 || d==70)
  96.       {  
  97.          if(a==70)
  98.          {         
  99.               position = 0;
  100.               winer = 1;
  101.          }
  102.          if(b==70)
  103.          {         
  104.               position =1;
  105.               winer = 2;
  106.          }
  107.          if(c==70)
  108.          {         
  109.               position =2;
  110.               winer = 3;
  111.          }
  112.          if(d==70)
  113.          {         
  114.               position =3;
  115.               winer =4;
  116.          }         
  117.          
  118.          words = "比賽結束!由"+name[position]+"先馳得點!";
  119.       }
  120.       
  121.       cout<<words<<endl;
  122.       cout<<"------------------------------------------------------------------------| 終點"<<endl;   
  123.       
  124.       // 要把每匹馬的進度給輸出
  125.       // 第一匹馬
  126.       for(int i=0;i<=a;i++)
  127.       {
  128.           cout<<" ";
  129.       }
  130.       cout<<name[0]<<endl;
  131.       
  132.       // 第二匹馬
  133.       for(int i=0;i<=b;i++)
  134.       {
  135.           cout<<" ";
  136.       }
  137.       cout<<name[1]<<endl;
  138.       
  139.        // 第三匹馬
  140.       for(int i=0;i<=c;i++)
  141.       {
  142.           cout<<" ";
  143.       }
  144.       cout<<name[2]<<endl;
  145.       
  146.       // 第四匹馬
  147.       for(int i=0;i<=d;i++)
  148.       {
  149.           cout<<" ";
  150.       }
  151.       cout<<name[3]<<endl;
  152.       
  153.       
  154.       //還沒有任一匹馬到終點 要清空畫面
  155.       if(a<=69 && b<=69 && c<=69 && d<=69)
  156.       {           
  157.         system("cls"); // 清空畫面
  158.       }
  159.       
  160.     }
  161.    
  162.     // 如果選的馬 = 最終跑贏的結果
  163.     if(horse == winer)
  164.     {
  165.         // 現有的錢 + 投注的錢*3
  166.         blance = blance + bet*3;
  167.         // 把贏的錢做統計
  168.         money = money + bet*3;
  169.         cout<<"您贏了"<<bet*3<<"元"<<endl;
  170.     }else
  171.     {
  172.         // 把輸的錢做統計
  173.         money = money -bet;
  174.         cout<<"損失"<<bet<<"元"<<endl;
  175.     }
  176.    
  177.     // 局數+1  
  178.     n++;
  179.     system("pause");
  180.     goto re;     
  181.     end:
  182.    
  183.     if(money ==0 )
  184.     {
  185.        cout<<"沒輸沒贏! 全身而退!"<<endl;
  186.     }else if( money > 0)
  187.     {
  188.        cout<<"恭喜你! 這次總共贏了"<<money<<"元"<<endl;
  189.     }else
  190.     {
  191.        cout<<"不好意思! 讓你損失了"<<-money<<"元"<<endl;
  192.     }
  193.     cout<<endl;
  194.     cout<<"謝謝光臨! 下次再來!"<<endl;
  195.     system("pause");  
  196.     return 0;   
  197. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表