廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 6861 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
wps
數位造型
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[C/C++][求助] C++ 猜數字 求助
設計一猜數字遊戲,讓使用者有三個等級可選擇:
初級—猜0到99中任一數字,給使用者提示猜的太大或太小,直到猜到為止。
中級—猜0到99中任一數字,給使用者提示猜的太大或太小,最多猜五次。
高 ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容



獻花 x0 回到頂端 [樓 主] From:臺灣教育部 | Posted:2011-11-15 21:35 |
tw2twtw
個人頭像
個人文章 個人相簿 個人日記 個人地圖
小有名氣
級別: 小有名氣 該用戶目前不上站
推文 x50 鮮花 x548
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

在Dev-C++找到現成的...(修一下就可用了...zz)
複製程式
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Start ();
void GetResults ();

int i, j, life, maxrand;
char c;


void
Start ()
{
i = 0;
j = 0;
life = 0;
maxrand = 6;

cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
cout << "1 : Easy (0-15)\n";
cout << "2 : Medium (0-30)\n";
cout << "3 : Difficult (0-50)\n";
cout << "or type another key to quit\n";
c = 30;

cin >> c;         // read the user's choice
cout << "\n";

switch (c)
{
  case '1' : maxrand = 15; // the random number will be between 0 and maxrand
  break;
  case '2' : maxrand = 30;
  break;
  case '3' : maxrand = 50;
  break;
  default : exit(0);
  break;
}

life = 5;     // number of lifes of the player
srand( (unsigned)time( NULL ) ); // init Rand() function
j = rand() % maxrand; // j get a random value between 0 and maxrand

GetResults();

}


void
GetResults ()
{
if (life <= 0)
  // if player has no more life then he lose
{
  cout << "You lose !\n\n";
  Start();
}

cout << "Type a number: \n";
cin >> i;     // read user's number

if ((i>maxrand) || (i<0)) // if the user number isn't correct, restart
{
  cout << "Error : Number not between 0 and \n" << maxrand;
  GetResults();
}

if (i == j)
{
  cout << "YOU WIN !\n\n"; // the user found the secret number
  Start();
}

else if (i>j)
{
  cout << "Too BIG\n";
  life = life - 1;   // -1 to the user's "life"
  cout << "Number of remaining life: " << life << "\n\n";
  GetResults();
}

else if (i<j)
{
  cout << "Too SMALL\n";
  life = life - 1;
  cout << "Number of remaining life:\n" << life << "\n\n";
  GetResults();
}
}


int
main ()
{
cout << "** Jackpot game **\n";
cout << "The goal of this game is to guess a number. You will be ask to type\n";
cout << "a number (you have 5 guess)\n";
cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to find\n\n";
Start();
return 0;
}


要用我寫的也是可以...
複製程式
#include <iostream> 
#include <stdlib.h>
#include <time.h>

using namespace std;

int main() 
{
  int Guess_number = 0; 
  int Guess_total = 0; 
  int difficult = 0; 
  int Guess_Key = 0; 
  
  char Answer; 
  
  int Cycle = 0; 
  
/* 
初級—猜0到99中任一數字,給使用者提示猜的太大或太小,直到猜到為止。
中級—猜0到99中任一數字,給使用者提示猜的太大或太小,最多猜五次。
高級—猜0到999中任一數字,給使用者提示猜的太大或太小,最多猜三次。
*/ 
  cout << "猜數字 (由JOT製作)" << endl;
  cout << "1:初級 (1~99,無限次數)" << endl;
  cout << "2:中級 (1~99,限猜10次)" << endl;
  cout << "3:高級 (1~999,限猜10次)" << endl;
  cout << "請選擇難度:"; 

  do
  {
      cin >> Answer; 
      Cycle = 0; 

      if( Answer == '1') 
      {
          difficult = 1; 
          srand((unsigned)time(NULL)); // init Rand() function
          Guess_Key = rand()%100;
          cout << "*** 難度一(數字隨機1到99,無限次數) ***" << endl;
      }
      else if( Answer == '2') 
      {
          difficult = 2; 
          srand((unsigned)time(NULL)); // init Rand() function
          Guess_Key = rand()%100;
          cout << "*** 難度二(數字隨機1到99,限猜10次) ***" << endl;
      }
      else if( Answer == '3') 
      {
          difficult = 3; 
          srand((unsigned)time(NULL)); // init Rand() function
          Guess_Key = rand()%1000;
          cout << "*** 難度三(數字隨機1到999,限猜10次) ***" << endl;
      }
      else
      {
          Cycle = 1; 
          cout << "請輸入數字: (1~3)" << endl;
      }
  }while(Cycle);

  do
  {
      Guess_total += 1;
      if(difficult == 2 && Guess_total > 10) 
      {
          cout << "十次機會已用完." << endl;
          cout << "正確答案為 " << Guess_Key << endl;
          break; 
      }
      if(difficult == 3 && Guess_total > 10)
      {
          cout << "十次機會已用完." << endl;
          cout << "正確答案為 " << Guess_Key << endl;
          break; 
      }

      cout << "請輸入你的幸運數字:";
      cin >> Guess_number;
      if(Guess_number > Guess_Key)
      { 
          cout << "         --//此數字小於 " << Guess_number << endl;
          Cycle = 1; 
      } 
      else if(Guess_number < Guess_Key)
      { 
          cout << "         --//此數字大於 " << Guess_number << endl;
          Cycle = 1; 
      } 
      else
      { 
          cout << "*** 恭喜你得到正確答案 ***" << endl;
          cout << "*** 總計猜了 " << Guess_total << " 次 ***" << endl;
          Cycle = 0; 
      } 
  }while(Cycle);

  system("PAUSE");
  return 0;
}


[ 此文章被tw2twtw在2011-11-24 23:27重新編輯 ]

此文章被評分,最近評分記錄
財富:50 (by ebolaman) | 理由: 熱心解答問題 ʕ◕ᴥ◕ʔ


我的SV: 59.126.178.46:27015
模式:亂七八糟?
獻花 x1 回到頂端 [1 樓] From:臺灣中華電信股份有限公司 | Posted:2011-11-24 23:00 |
csr
個人文章 個人相簿 個人日記 個人地圖
小有名氣
級別: 小有名氣 該用戶目前不上站
推文 x0 鮮花 x898
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

大大寫的很好的程式
小弟用來當學習材料
謝謝


獻花 x0 回到頂端 [2 樓] From:局域網對方和您在同一內部網 | Posted:2011-12-06 09:04 |
iamboss123
個人文章 個人相簿 個人日記 個人地圖
小人物
級別: 小人物 該用戶目前不上站
推文 x0 鮮花 x3
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

高手果然多啊! 源碼拿去了!


獻花 x0 回到頂端 [3 樓] From:河北 | Posted:2014-01-12 14:34 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.022367 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言