文章目录
前言
最近一直想做珠玑妙算的小游戏,今天就给大家分享一下。
效果
系统制作
首先我们来做头文件,里面定义一个class game_system
。
public
public:
void startGame() {
PrintRules();
RunGame();
}
private
变量
string guess, ans; //答案和猜测
int digit; //挑战位数
PrintRules
这段代码简单粗暴,用来解释规则:
系统随机生成n位的随机数,没有任意位是相同的。然后你猜一个数,猜完系统会给你两个提示。
第一个数代表数字和位置都猜对的数字个数,第二个数代表只有数字猜对的数字个数。
void PrintRules() {
printf(" MASTERMIND\n");
printf("The rules of this game is:\n 1. The computer will randomly create a number.");
printf("And every number in this number is different.\n");
printf(" 2. You will guess the number in each round, and the computer will give you two hints.\n");
printf("The first number output is the number of numbers and positions guessed correctly");
printf(", and the second number output is the number of numbers guessed correctly only.GoodLuck!\n");
}
GetDigit
这段代码作用是获得玩家希望挑战的数字位数(3到7之间),这个值难度比较适中。当然,你可以自行根据需要调整,注意最大值不要超过10。
void GetDigit() {
printf("Please enter the digit of the number ");
printf("that you want to challange in this round of 'MASTERMIND'(3 - 7):");
for (;;) {
cin >> digit;
if (digit <= 7 && digit >= 3) break;
else printf("\nWrong input, please enter again\n");
}
printf("\n");
}
randomNumber
这段代码用来创作没有重复数位且第一位不是0的随机数
void randomNumber() {
string choice = "123456789";
srand(time(nullptr));
int r = rand() % choice.size(); //创建从0到8的随机数做选择的下标
ans += choice[r]; //答案后面加上随机选择的字符
choice.erase(r, 1); //删除已选择过的选项
choice += '0'; // 最高为不是零,将'0'加入选项
for (int i = 1; i < digit; ++i) {
srand(time(nullptr));
r = rand() % choice.size();
ans += choice[r];
choice.erase(r, 1);
}
}
获得合法输入
checkInput
bool checkInput() {
string comp;
for (int i = 0; i < guess.size(); ++i) {
if (count(comp.begin(), comp.end(), guess[i]) > 0) return false; //判断是否重复
else comp += guess[i]; //否则将遍历到的字符存入已出现字符串里
}
if (guess.size() != digit) return false; //若字符串大小超过选择挑战位数,返回假
return true; //若合法,返回真
}
getInput
void getInput() {
for (;;) {
cin >> guess;
if (checkInput()) break; //不停输入直到合法
else printf("Wrong input, please enter again:");
}
}
RunGame
函数都写完了,现在我们写运行游戏的函数。
void RunGame() {
GetDigit(); //获得挑战位数
randomNumber(); //获得随机数
for (;;) {
int all = 0, half = 0; //创建变量:数位皆猜对个数,数猜对个数
getInput();
for (int i = 0; i < digit; ++i) {
if (ans[i] == guess[i]) ++all; //若位置和数字都猜对则提示第一个数加1
else {
for (auto j : guess) {
if (ans[i] == j) { //如果数字猜对则提示第二个数加1
++half;
break;
}
}
}
}
if (all == digit) { //若全部数位猜对,游戏胜利
printf("Correct!");
break;
}
else printf("%d%c%d\n", all, ' ', half); //否则输出提示
}
}
头文件
#ifndef ABACUSCALCULATION_H_INCLUDED
#define ABACUSCALCULATION_H_INCLUDED
#include <iostream>
#include <algorithm>
#include <ctime>
#include <vector>
#include <string>
using namespace std;
class game_system
{
public:
void startGame() {
PrintRules();
RunGame();
}
private:
string guess, ans;
int digit;
void RunGame() {
GetDigit();
randomNumber();
for (;;) {
int all = 0, half = 0;
getInput();
for (int i = 0; i < digit; ++i) {
if (ans[i] == guess[i]) ++all;
else {
for (auto j : guess) {
if (ans[i] == j) {
++half;
break;
}
}
}
}
if (all == digit) {
printf("Correct!");
break;
}
else printf("%d%c%d\n", all, ' ', half);
}
}
void PrintRules() {
printf(" MASTERMIND\n");
printf("The rules of this game is:\n 1. The computer will randomly create a number.");
printf("And every number in this number is different.\n");
printf(" 2. You will guess the number in each round, and the computer will give you two hints.\n");
printf("The first number output is the number of numbers and positions guessed correctly");
printf(", and the second number output is the number of numbers guessed correctly only.GoodLuck!\n");
}
void GetDigit() {
printf("Please enter the digit of the number ");
printf("that you want to challange in this round of 'MASTERMIND'(3 ¡ª 7):");
for (;;) {
cin >> digit;
if (digit <= 7 && digit >= 3) break;
else printf("\nWrong input, please enter again\n");
}
printf('\n');
}
void randomNumber() {
string choice = "123456789";
srand(time(nullptr));
int r = rand() % choice.size();
ans += choice[r];
choice.erase(r, 1);
choice += '0';
for (int i = 1; i < digit; ++i) {
srand(time(nullptr));
r = rand() % choice.size();
ans += choice[r];
choice.erase(r, 1);
}
}
bool checkInput() {
string comp = "";
for (int i = 0; i < guess.size(); ++i) {
if (count(comp.begin(), comp.end(), guess[i]) > 0) return false;
else comp += guess[i];
}
return guess.size() == digit;
return true;
}
void getInput() {
for (;;) {
cin >> guess;
if (checkInput()) break;
else printf("Wrong input, please enter again:");
}
}
};
#endif // ABACUSCALCULATION_H_INCLUDED
请将此文件存放于正确的路径:xxx\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++
这个路径是Codeblocks::IDE
的路径,如有其他需求请自行调整。
否则也可以随便放一个位置然后
#include <盘名:\\路径\AbacusCalculation.h>
main()
最后,创建新的C++源文件,代码如下:
#include <AbacusCalculation.h> // 路径名请自行调整
int main() {
game_system game;
game.startGame();
}