名人說:莫聽穿林打葉聲,何妨吟嘯且徐行。—— 蘇軾《定風波·莫聽穿林打葉聲》
Code_流蘇(CSDN)(一個喜歡古詩詞和編程的Coder)
目錄
- 一、項目描述
- 二、項目實現
- 三、項目步驟
- 四、項目擴展方向
更多項目內容,請關注我、訂閱專欄《項目探索實驗室》,內容持續更新中…
項目名稱:猜數字游戲
一、項目描述
創建一個猜數字游戲,玩家需要在限定次數內猜出一個由計算機隨機生成的數字。
游戲規則如下:
- 計算機生成一個1到100之間的隨機整數。
- 玩家有有限次數(如10次)來猜這個數字。
- 每次猜測后,計算機會告訴玩家猜的數字是太大了還是太小了。
- 如果玩家在限定次數內猜對,顯示勝利信息。
- 如果玩家用完所有次數還未猜對,顯示失敗信息并告知正確數字。
二、項目實現
可以選擇簡單地用函數來實現這個游戲,也可以定義一個GuessingGame
類來封裝游戲邏輯。
三、項目步驟
#include <iostream>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()using namespace std;class GuessingGame {
private:int secretNumber;int maxAttempts;int attemptsLeft;void resetGame() {// Seed the random number generatorsrand(static_cast<unsigned int>(time(0)));// Generate a random number between 1 and 100secretNumber = rand() % 100 + 1;attemptsLeft = maxAttempts;}public:GuessingGame(int maxAttempts) : maxAttempts(maxAttempts), attemptsLeft(maxAttempts) {resetGame();}void play() {int guess;char playAgain;do {cout << "歡迎來到猜數字游戲!" << endl;cout << "我已經選擇了一個1到100之間的數字。" << endl;cout << "你有 " << maxAttempts << " 次機會來猜它。" << endl;while (attemptsLeft > 0) {cout << "請輸入你的猜測:";cin >> guess;if (guess == secretNumber) {cout << "恭喜你!你猜對了這個數字!" << endl;break;} else if (guess < secretNumber) {cout << "太小了!" << endl;} else {cout << "太大了!" << endl;}attemptsLeft--;cout << "剩余次數:" << attemptsLeft << endl;}if (attemptsLeft == 0) {cout << "很抱歉,你已經用完了所有機會。正確的數字是 " << secretNumber << "。" << endl;}cout << "你想再玩一次嗎?(y/n):";cin >> playAgain;if (playAgain == 'y' || playAgain == 'Y') {resetGame();}} while (playAgain == 'y' || playAgain == 'Y');cout << "感謝你玩猜數字游戲!再見!" << endl;}
};int main() {int maxAttempts = 10; // 你可以更改嘗試次數GuessingGame game(maxAttempts);game.play();return 0;
}
#include "Book.h"
#include <iostream>
#include <vector>
using namespace std;void displayMenu() {cout << "1. 添加圖書" << endl;cout << "2. 更新圖書信息" << endl;cout << "3. 刪除圖書" << endl;cout << "4. 查找圖書" << endl;cout << "5. 顯示所有圖書信息" << endl;cout << "6. 退出" << endl;
}void addBook(vector<Book> &books) {string title, author, ISBN;cout << "輸入書名: ";cin.ignore();getline(cin, title);cout << "輸入作者: ";getline(cin, author);cout << "輸入ISBN: ";getline(cin, ISBN);books.push_back(Book(title, author, ISBN));
}void updateBook(vector<Book> &books) {string ISBN, title, author, newISBN;cout << "輸入要更新的圖書的ISBN: ";cin.ignore();getline(cin, ISBN);for (Book &book : books) {if (book.getISBN() == ISBN) {cout << "輸入新書名: ";getline(cin, title);cout << "輸入新作者: ";getline(cin, author);cout << "輸入新ISBN: ";getline(cin, newISBN);book.update(title, author, newISBN);return;}}cout << "未找到該ISBN的圖書。" << endl;
}void deleteBook(vector<Book> &books) {string ISBN;cout << "輸入要刪除的圖書的ISBN: ";cin.ignore();getline(cin, ISBN);for (auto it = books.begin(); it != books.end(); ++it) {if (it->getISBN() == ISBN) {books.erase(it);return;}}cout << "未找到該ISBN的圖書。" << endl;
}void findBook(const vector<Book> &books) {string ISBN;cout << "輸入要查找的圖書的ISBN: ";cin.ignore();getline(cin, ISBN);for (const Book &book : books) {if (book.getISBN() == ISBN) {book.display();return;}}cout << "未找到該ISBN的圖書。" << endl;
}void displayAllBooks(const vector<Book> &books) {for (const Book &book : books) {book.display();}
}int main() {vector<Book> books;int choice;while (true) {displayMenu();cin >> choice;switch (choice) {case 1:addBook(books);break;case 2:updateBook(books);break;case 3:deleteBook(books);break;case 4:findBook(books);break;case 5:displayAllBooks(books);break;case 6:return 0;default:cout << "無效選項,請重新選擇。" << endl;}}return 0;
}
效果如圖:
四、項目擴展方向
可以根據需要擴展項目,例如:
- 增加難度選擇(如選擇猜測范圍或最大次數)。
- 增加一個計分系統,根據猜測次數和成功與否來評分。
Code_流蘇(CSDN)(一個喜歡古詩詞和編程的Coder)
點贊加關注,收藏不迷路!本篇文章對你有幫助的話,還請多多點贊支持!