查看單個文章
舊 2007-11-11, 12:51 AM   #2 (permalink)
joebin
註冊會員
 
joebin 的頭像
榮譽勳章

勳章總數0
UID - 270712
在線等級: 級別:2 | 在線時長:16小時 | 升級還需:5小時級別:2 | 在線時長:16小時 | 升級還需:5小時
註冊日期: 2007-06-06
住址: 過去*現在*未來
文章: 42
精華: 0
現金: 58 金幣
資產: 118 金幣
預設



語法:
#include<iostream>
using namespace std;
int main(){
  int i,r,n,w[2],s[2],m[2]={100,100};   
  char p='y';  
  //p表示否繼續玩,陣列0為玩家,1為電腦,w表贏的次數,s表骰子和,m表金錢數
   cout <<"Welcome to the dice game!"<<endl;
  for(i=0;i<30;i++) cout <<"*";
  srand(time(NULL));
  cout <<endl<<"Player's wager: 100"<<endl<<"Computer's wager: 100";
  while(p=='y'){   
    w[0]=w[1]=0;
    cout <<endl<<"Enter the number of dices to roll (>= 1):";
    while(cin >>n){ //n表骰子數目 
          if(n>0) break;
      cout<<endl<<"Try again! Enter the number of dices to roll:";
    }    
    while(1){
      s[0]=s[1]=0;
      cout<<endl<<"Roll the dice (Push any key to roll)?"<<endl;
      cin >>p;
      cout <<"Player:";
      for(i=0;i<n;i++){
        r=(rand()%6)+1;
        cout <<" "<<r;
        s[0]+=r;    
      }
      cout <<endl<<"Computer:";
      for(i=0;i<n;i++){
        r=(rand()%6)+1;
        cout <<" "<<r;
        s[1]+=r;    
      }
      if(s[0]>s[1]){  //玩家贏
              if(w[0]>0){
          cout <<endl<<"=> Player wins the set."<<endl;
          m[0]+=20;m[1]-=20;    
          break;
        }
        cout <<endl<<"=> Player wins once."<<endl;
        w[0]++;   
      }
      if(s[1]>s[0]){  //電腦贏
              if(w[1]>0){
          cout <<endl<<"=> Computer wins the set."<<endl; 
          m[1]+=20;m[0]-=20;   
          break;
        }
        cout <<endl<<"=> Computer wins once."<<endl;
        w[1]++;   
      }
      if(s[0]==s[1]) cout <<endl<<"=> No winner. Try again!"<<endl;
    } 
    for(i=0;i<30;i++) cout <<"=";
    cout <<endl<<"Player's wager: "<<m[0]<<endl<<"Computer's wager: "<<m[1]<<endl;
    if(m[0]>0&&m[1]>0){  //是否繼續遊戲
          cout <<"Continue (Y/N)?";
      cin >>p;
    }   
    else{
      for(i=0;i<20;i++){
          if(i==10) cout <<" Game Over! ";
          cout <<"=";
      }
      cout <<endl<<"Start a new game (Y/N)?";  //是否結束遊戲
          cin >>p;
      m[0]=m[1]=100;
    }                      
  }
  system("pause");
  return 0;
}

此帖於 2007-11-11 03:02 PM 被 joebin 編輯.
joebin 目前離線  
送花文章: 15, 收花文章: 18 篇, 收花: 28 次
回覆時引用此帖
有 2 位會員向 joebin 送花:
rang0419 (2007-11-11),飛鳥 (2007-11-11)
感謝您發表一篇好文章