史萊姆論壇

返回   史萊姆論壇 > 專業主討論區 > 程式語言討論區
忘記密碼?
論壇說明

歡迎您來到『史萊姆論壇』 ^___^

您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的!

請點擊這裡:『註冊成為我們的一份子!』

Google 提供的廣告


發文 回覆
 
主題工具 顯示模式
舊 2007-11-09, 05:23 PM   #1
rang0419
註冊會員
榮譽勳章

勳章總數
UID - 285008
在線等級: 級別:0 | 在線時長:4小時 | 升級還需:1小時
註冊日期: 2007-11-07
文章: 24
精華: 0
現金: 35 金幣
資產: 35 金幣
預設 疑問 - 請問下面的程式要怎麼寫阿??



--------------------
閱讀本主題的最佳解答
--------------------


Write a program of a dice game.
1.The computer and the player are given 100 dollars wager.
2.The player decides the number of dices to roll (It should be larger than 0).
3. The computer and the player roll the dices.
4.Whoever has the larger total is the winer.
5. Whoever wins twice wins a set of game. The winner gains the wager of 20 dollars and the loser looses the wager of 20 dollars.
6. A player can keep playing the game or quit the game.
7. The game is over when the player or the computer looses all the wager.
8.Once the game is over, the player can start a new game or quit the game.


A possible run may look like:
**************************************************
Welcome to the dice game!
**************************************************
Player's wager: 100
Computer's wager: 100
Enter the number of dices to roll (>= 1): 0
Try again! Enter the number of dices to roll: 2
Roll the dice (Push any key to roll)?
Player: 3 3
Computer: 2 6
=> Computer wins once.
Roll the dice (Push any key to roll)?
Player: 2 5
Computer: 3 4
=> No winner. Try again!
Roll the dice (Push any key to roll)?
Player: 2 6
Computer: 1 3
=> Player wins once.
Roll the dice (Push any key to roll)?
Player: 4 6
Computer: 3 5
=> Player wins the set.
Player's wager: 120
Computer's wager: 80
==================================================
Continue (Y/N)? Y
Enter the number of dices to roll: 3
Roll the dice (Push any key to roll)?
Player: 3 3 6
Computer: 2 6 5
=> Computer wins once.
.....
.....
Player's wager: 200
Computer's wager: 0
==================== Game Over! ==================
Start a new game (Y/N)? Y
....



這該怎麼寫勒.........

可以給我參考一下嗎
rang0419 目前離線  
送花文章: 5, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖
舊 2007-11-11, 12:51 AM   #2 (permalink)
註冊會員
 
joebin 的頭像
榮譽勳章

勳章總數
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)
感謝您發表一篇好文章
舊 2007-11-11, 12:52 PM   #3 (permalink)
註冊會員
榮譽勳章

勳章總數
UID - 285008
在線等級: 級別:0 | 在線時長:4小時 | 升級還需:1小時
註冊日期: 2007-11-07
文章: 24
精華: 0
現金: 35 金幣
資產: 35 金幣
預設

非常感謝你的解答喔!!

很詳細又有註解~~~:

我現在就來試試看..........

感謝捏 有問題我再問你!!
rang0419 目前離線  
送花文章: 5, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖
舊 2007-11-11, 03:01 PM   #4 (permalink)
註冊會員
 
joebin 的頭像
榮譽勳章

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

引用:
作者: rang0419 查看文章
非常感謝你的解答喔!!

很詳細又有註解~~~:

我現在就來試試看..........

感謝捏 有問題我再問你!!
歡迎^^剛打入全國賽~正愁沒題目練習
joebin 目前離線  
送花文章: 15, 收花文章: 18 篇, 收花: 28 次
回覆時引用此帖
舊 2007-11-12, 05:32 PM   #5 (permalink)
註冊會員
榮譽勳章

勳章總數
UID - 285008
在線等級: 級別:0 | 在線時長:4小時 | 升級還需:1小時
註冊日期: 2007-11-07
文章: 24
精華: 0
現金: 35 金幣
資產: 35 金幣
預設

引用:
作者: joebin 查看文章
歡迎^^剛打入全國賽~正愁沒題目練習


甚麼全國賽阿 這麼厲害
rang0419 目前離線  
送花文章: 5, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖
發文 回覆



發表規則
不可以發文
不可以回覆主題
不可以上傳附加檔案
不可以編輯您的文章

論壇啟用 BB 語法
論壇啟用 表情符號
論壇啟用 [IMG] 語法
論壇禁用 HTML 語法
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用

相似的主題
主題 主題作者 討論區 回覆 最後發表
系統 - 該記憶體不能為read或written的解決方案 psac 作業系統操作技術文件 5 2007-05-14 09:19 PM
資訊 - 電腦當機全集 psac 系統 & 硬體安裝及故障判斷技術文件 13 2006-08-24 10:48 AM
主要 Microsoft 伺服器產品使用的網路連接阜 psac 網路軟硬體架設技術文件 4 2004-09-19 07:44 AM
Windows 2000的可靠性 psac 作業系統操作技術文件 1 2003-08-11 11:46 PM


所有時間均為台北時間。現在的時間是 09:22 PM


Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.


SEO by vBSEO 3.6.1