返回列表 發帖

賽馬程式 (四)

本帖最後由 陳品肇 於 2022-3-26 11:54 編輯

加入首頁選單, 新增買入的功能, 執行畫面如下:
可用餘額: 0 元
(1)買入  (2)下注  (3)離開  請選擇: 1
買入: 200


在使用者輸入數值後, 畫面更新如下:
可用餘額: 200 元
(1)買入  (2)下注  (3)離開  請選擇:
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     // 開局第一場
  8.     int n =1,blance=0;
  9.     re:   
  10.     system("cls");
  11.     int a=0,b=0,c=0,d=0,r, option, buyin =0 ; // 每匹馬前進的進度   
  12.     // 當有任一匹跑到終點70 的時候就跳離迴圈
  13.     string words ="賽馬進行中";
  14.     // 代表馬的名子
  15.     string name[4] = {"◆","★","▲","●"};
  16.    
  17.     // 宣告一個暫存的變數
  18.     int position =0;
  19.     cout<<"「好事成雙」賽馬場 第"<<n<<"局"<<endl;
  20.     cout<<"------------------------------------------------------------------------| 終點"<<endl;
  21.     cout<<"◆"<<endl;
  22.     cout<<"★"<<endl;
  23.     cout<<"▲"<<endl;
  24.     cout<<"●"<<endl;
  25.     cout<<endl;
  26.     cout<<"可用餘額:"<<blance<<"元"<<endl;
  27.     cout<<endl;
  28.     cout<<"(1)買入 (2)下注 (3)離開 請選擇:";
  29.     cin>>option;
  30.    
  31.     // 如果買入 把錢加入餘額
  32.     if(option==1)
  33.     {
  34.         cout<<"買入:";
  35.         cin>>buyin;
  36.         blance += buyin;
  37.         goto re;
  38.     }
  39.    
  40.     system("pause"); // 按下enter 才開始
  41.     system("cls"); // 清空畫面
  42.     srand(time(NULL)); // 撒種子亂數
  43.     while(a<=70 && b<=70 && c<=70 && d<=70)
  44.     {  
  45.       cout<<words<<endl;
  46.       cout<<"------------------------------------------------------------------------| 終點"<<endl;   
  47.       // 隨機挑一匹馬
  48.       r = rand()%4+1;
  49.       // 每匹馬 跑的進度
  50.       switch(r)
  51.       {
  52.           case 1:
  53.                a++;
  54.                break;
  55.           case 2:
  56.                b++;
  57.                break;
  58.           case 3:
  59.                c++;
  60.                break;
  61.           case 4:
  62.                d++;
  63.                break;
  64.       }
  65.       
  66.       // 要把每匹馬的進度給輸出
  67.       // 第一匹馬
  68.       for(int i=0;i<=a;i++)
  69.       {
  70.           cout<<" ";
  71.       }
  72.       cout<<name[0]<<endl;
  73.       
  74.       // 第二匹馬
  75.       for(int i=0;i<=b;i++)
  76.       {
  77.           cout<<" ";
  78.       }
  79.       cout<<name[1]<<endl;
  80.       
  81.        // 第三匹馬
  82.       for(int i=0;i<=c;i++)
  83.       {
  84.           cout<<" ";
  85.       }
  86.       cout<<name[2]<<endl;
  87.       
  88.       // 第四匹馬
  89.       for(int i=0;i<=d;i++)
  90.       {
  91.           cout<<" ";
  92.       }
  93.       cout<<name[3]<<endl;
  94.       
  95.    
  96.       if(a==70 || b==70 || c==70 || d==70)
  97.       {  
  98.          if(a==70)
  99.               position = 0;
  100.          if(b==70)
  101.               position =1;
  102.          if(c==70)
  103.               position =2;
  104.          if(d==70)
  105.               position =3;           
  106.          
  107.          words = "比賽結束!由"+name[position]+"先馳得點!";
  108.       }
  109.       // 當有任一個跑到71 就代表到終點 不要清空畫面
  110.       if(a==71 || b==71 || c==71 || d==71)
  111.       {               
  112.       }else{      
  113.         system("cls"); // 清空畫面
  114.       }
  115.       
  116.     }
  117.     // 局數+1  
  118.     n++;
  119.     system("pause");
  120.     goto re;
  121.     return 0;   
  122. }
複製代碼

返回列表