C 編寫的猜數字遊戲

2021-04-17 14:53:19 字數 1759 閱讀 5734

*   功    能:猜數字的遊戲

*   編譯環境:windows2000 + dev-c++ 4.9

*   作    者:jhkdiy

*   電子郵件:[email protected]

*   備    注:無意中看到《beginning c++ game programming》一書,隨便瀏覽了一下,

*             覺得這個遊戲有點意思,所以自己完善了下。初學者用來學習一下語法

*             和提高一下興趣還是很有意思的。該程式有bug,大家找出來,

*             不是語法錯誤哦!!思考一下! 

#include

#include

#include

using namespace std;

int main(int argc, char *argv)

//產生隨機數種子

srand(time(0));            

int  thenumber = rand() % 100 + 1;     //隨機數控制在1-100之間

int  tries = 0,                        //使用者嘗試的次數

guess;                            //使用者輸入的數字

char bplayagain;                       //是否繼續遊戲

cout << "/twelcome to guess my number/n/n";

do//接受使用者的輸入

cout << "enter a guess: ";

cin >> guess;

++tries;

//如果輸入的數字大於產生的隨機數

if( guess > thenumber )

cout<<"too high!/n/n";

//如果輸入的數字小於產生的隨機數

if( guess < thenumber )

cout << "too low!/n/n";

//猜對了···

if( guess == thenumber )

cout << "/n/n/tvery good! you get it!"

<< "/tthe number is: " << thenumber << endl;

cout << "/n/n/tyou try " << tries << " times!" << endl;

//是否繼續遊戲

cout << "/n/ndo you want to play again?(y/n)";

cin  >> bplayagain;

if( bplayagain == 'y' || bplayagain == 'y')

//清屏後繼續遊戲

system("cls");

continue;

else if( bplayagain == 'n' || bplayagain == 'n')

cout << "/nsee you next time, bye!/n";

break;            

else

cout << "/nenter a wrong choice! program will exit!/n";

break;

}while(true);

//退出程式

system("pause");

return exit_success;

python編寫猜數字遊戲

一 猜數字遊戲介紹 猜數字 又稱 bulls and cows 是一種古老的的密碼破譯類益智類小遊戲,起源於20世紀中期,一般由兩個人或多人玩,也可以由乙個人和電腦玩。二 猜數字遊戲規則三 python編寫猜數字遊戲 import random while true a random.randint...

用Python編寫猜數字遊戲

首先,我們先來看一下效果 效果很不錯,如下 import random,time,easygui 隨機數函式把數字在1 99隨便選乙個數 數字 random.randint 1,99 回答 0機會 5回答 int input 請輸入乙個數 在機會不等於0與沒猜中時一直重複 while 機會 0and...

用python編寫猜數字遊戲

import random num random.randint 1,100 這裡你可指定num的數字進行測試猜中的結果,如果你總是猜不著的話 比如num 23 print 猜一猜我心裡想的是哪個數字吧 number input times 1 while true if times 2 break...