1. 程式人生 > >c++遊戲程式設計

c++遊戲程式設計

C++ Game Programming[1]

1 Guess Number!

Basic ideas: The program will generate a random number. And you can input your guess to the program, then the program will tell you too high or too low, then you can modify your guess according to the response.

Here is a simple c++ example:

#include <iostream>
using namespace std; int main() { // seed random number generator int secretNumber = rand() % 100 + 1; //cout << secretNumber; int tries = 0; int guess; cout << "Welcome to Guess My number!!\n"; do { cout << "Enter a guess: "; cin >> guess; ++tries; if (guess > secretNumber)
cout << "Too high!\n"; else if (guess < secretNumber) cout << "Too low!\n"; else cout << "\n That's it! You got it in " << tries << " guesses!\n"; } while (guess != secretNumber); return 0; }

Here is the executing result:
在這裡插入圖片描述