返回列表 發帖

賽馬程式 (三)

本帖最後由 鄭繼威 於 2023-7-7 20:54 編輯

1. 在比賽首頁顯示第幾局
2. 在比賽結束頁顯示哪一位選手勝出




法1:看哪隻馬跑到終點(s==73),就代表他贏
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.         int round=1;
  8.        
  9.     re:
  10.     system("cls");      
  11.    
  12.     int s[]={0,0,0,0};     //存放進度用的
  13.     string n[]={"◆","★","▲","●"};     //存放馬用的
  14.     srand(time(NULL));
  15.     cout<<"「好事成雙」賽馬場 round:"<<round<<endl;
  16.     cout<<"-------------------------------------------------------------------------| 終點"<<endl;

  17.     //把馬印出來
  18.     for(int i=0;i<=3;i++)
  19.     {
  20.             cout<<n[i]<<endl;
  21.         }

  22.     system("pause");

  23.     //開始賽馬
  24.     while(true)
  25.     {
  26.         system("cls");    //清空畫面
  27.         int r=rand()%4;   //0~3   (抽馬)
  28.         s[r]++;       //看電腦抽到哪隻馬就+1
  29.         cout<<"比賽進行中"<<endl;
  30.         cout<<"-------------------------------------------------------------------------| 終點"<<endl;
  31.         
  32.         for(int j=0;j<=3;j++)
  33.         {
  34.                 for(int i=0; i<=s[j]; i++)
  35.                 {
  36.                         cout<<" ";       
  37.                         }
  38.                         cout<<n[j]<<endl;       
  39.                 }

  40. //        
  41.         //抵達終點
  42.         if(s[r]==73)
  43.             break;
  44.         _sleep(0.05);                     
  45.     }
  46.    
  47.     system("cls");

  48.         for(int i=0;i<=3;i++)
  49.         {
  50.                 if(s[i]==73)
  51.                 {
  52.                          cout<<"比賽結束!  由 "<<n[i]<<" 先馳得點!"<<endl;
  53.                 }
  54.                
  55.         }
  56.     cout<<"-------------------------------------------------------------------------| 終點"<<endl;
  57.     //印空格(看進度是多少就印多少空格)     
  58.     //把馬印出來
  59.     for(int j=0;j<=3;j++)
  60.     {
  61.             for(int i=0; i<=s[j]; i++)
  62.             {
  63.                     cout<<" ";
  64.                 }
  65.         cout<<n[j]<<endl;
  66.         }
  67.    

  68.     system("pause");
  69.     round++;
  70.     goto re;
  71.     return 0;
  72. }
複製代碼
法2:取最大數(max)並將馬紀錄下來(win=i;),就代表他贏
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.         int round=1;        //局數
  8.        
  9.     re:
  10.     system("cls");
  11.     srand(time(NULL));
  12.     int s[]={0,0,0,0};        //存放進度用的
  13.     string p[]={"◆","★","▲","●"};        //存放馬用的
  14.     int r=0;
  15.     cout<<"「好事成雙」賽馬場 第"<<round<<"局"<<endl;
  16.     cout<<"-------------------------------------------------------------------------| 終點"<<endl;
  17.     for(int i=0; i<4; i++)
  18.         cout<<p[i]<<endl;        //把馬印出來
  19.     system("pause");
  20.     system("cls");        //清空畫面
  21.     while(s[r]<=73)        //開始賽馬
  22.     {
  23.         r=rand()%4;   //0~3        (抽馬)
  24.         s[r]++;        //看電腦抽到哪隻馬就+1
  25.         cout<<"比賽進行中"<<endl;
  26.         cout<<"-------------------------------------------------------------------------| 終點"<<endl;
  27.         for(int i=0; i<4; i++)
  28.         {
  29.             for(int j=0; j<s[i]; j++)
  30.                 cout<<" ";        //印空格(看進度是多少就印多少空格)
  31.             cout<<p[i]<<endl;        //把馬印出來
  32.         }
  33.         _sleep(50);
  34.         system("cls");
  35.     }
  36.     //取最大數
  37.     int max=0,win;
  38.     for(int i=0;i<=3;i++)
  39.     {
  40.             if(max<s[i])
  41.             {
  42.                     max=s[i];
  43.                     win=i;
  44.                 }
  45.     }
  46.        
  47.     cout<<"比賽結束!  由 "<<n[win]<<" 先馳得點!"<<endl;
  48.     cout<<"-------------------------------------------------------------------------| 終點"<<endl;
  49.     for(int i=0; i<4; i++)
  50.     {
  51.            
  52.         for(int j=0; j<s[i]; j++)
  53.             cout<<" ";
  54.         cout<<p[i]<<endl;
  55.     }
  56.     system("pause");
  57.     round++;        //局數+1
  58.     goto re;
  59.     return 0;
  60. }
複製代碼

返回列表