C++小游戏——猜数字


内容简介

本文介绍了一个作者原创的猜数推理小游戏qwq


以下是本篇文章正文内容

一、猜数规则

每一组数字按照一定的数学规则排列。找出规则,解开谜题,同时享受数学的魅力吧!

本游戏内容精简,画风简洁(

而且非常容易上手!

二、具体代码实现

1.调试环境

环境为微软的Visual Studio 2015,和上一期和上上期一样
(就没变过。。)

2.代码实现

代码如下:

//#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h> //
using namespace std;
typedef long long ll;

const int maxn = 10;
const int N = 6; //

int Cn4[15] = { 0,1,5,15,35,70,126,210,330,495,715,1001 };
int fib[15] = { 1,1,2,3,5,8,13,21,34,55,89,144 };

int a[maxn][maxn]; //
bool use[maxn][maxn]; //
int score = 0;
int t = 36; 

void inni(int x) {
	memset(use, 0, sizeof(use));
	if (x == 1) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				a[i][j] = i*j;
			}
		}
	}
	else if (x == 2) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				a[i][j] = Cn4[i + j - 1];
			}
		}
	}
	else if (x == 3) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				a[i][j] = fib[i + j - 1];
			}
		}
	}
	return;
}

void show() {
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			if (!use[i][j]) {
				cout << "    *";
			}
			else {
				printf("%5d", a[i][j]);
			}
		}
		cout << endl;
	}
	cout << "当前分数:" << score << endl;
	return;
}

bool judgeNum(string str) {

	int len = str.size(); // 长度

	for (int i = 0; i < len; i++) {
		if (str[0] < '0' || str[0] > '9') {
			return false; // 
		}
	}

	return true;
}

bool judgeNumRange(int x) {
	if (x >= 1 && x <= N) {
		return true; // 合法
	}
	return false;
}

int change(string str) {
	int len = str.size(); // 长度
	int ans = 0;
	
	for (int i = 0; i < len; i++) {
		ans = ans * 10 + (str[i] - '0');
	}

	return ans;
}

void guess() {
	string s[5];
	int x, y, z;

	cout << "请输入坐标x,y(1 <= x,y <= " << N << "),猜测值z" << endl;
	
	// 输入合法性判断1,防止非法字符
	cin >> s[1] >> s[2] >> s[3];
	if (!(judgeNum(s[1]) && judgeNum(s[2]) && judgeNum(s[3]))) {
		cout << "非法输入!" << endl;
		return;
	}

	x = change(s[1]);
	y = change(s[2]);
	z = change(s[3]);

	// 输入合法性判断2,防止a数组越界
	if (!(judgeNumRange(x) && judgeNumRange(y))) {
		cout << "非法输入!" << endl;
		return;
	}

	if (a[y][x] == z) {
		if (use[y][x]) { // 已经猜对过
			cout << "你已经猜对过啦,换个数字猜叭~~" << endl;
		}
		else {
			cout << "猜对啦!!" << endl;
			use[y][x] = true;
			score += 10;
			t--;
		}
	}
	else {
		cout << "猜错啦~~~" << endl;
		score -= 5;
	}
	return;
}

int main() { // 
	int k = 1;

	while (k <= 3) {
		inni(k);

		while (t) {
			show();
			guess();
		}
		cout << "第" << k << "轮猜数结束~~ 你的当前得分为:" << score << "分!" << endl;
		system("pause"); //

		k++;
		t = 36; //
	}
	
	cout << "游戏结束~~ 你的最终得分为:" << score << "分!" << endl;

	getchar(); getchar();
	return 0;
}
/*
样例:
略
*/

3.运行演示

开始界面
开始界面


游戏进行1

游戏进行界面


通关
通关啦!


小结

以上就是今天要讲的内容啦,看到这里不点个赞或者一键三连支持一下这个刚刚开始写博客的萌新博主嘛qwq你的关注和支持就是我进步的动力呐~

后续我会继续分享一些有趣的小游戏给大家哦~~
c++小游戏专栏保持不定期 更新~~qwq

好啦,朋友们下期再见啦!!!

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数
好的,下面是C++实现Socket实现猜数字小游戏的代码示例: 服务端代码: ```c++ #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> using namespace std; int main() { // 创建套接字 int listenFd = socket(AF_INET, SOCK_STREAM, 0); if (listenFd == -1) { cerr << "Failed to create socket!" << endl; return -1; } // 配置地址 struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); serverAddr.sin_port = htons(8080); // 绑定地址 if (bind(listenFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) { cerr << "Failed to bind address!" << endl; return -1; } // 监听端口 if (listen(listenFd, 5) == -1) { cerr << "Failed to listen on socket!" << endl; return -1; } // 接受连接 struct sockaddr_in clientAddr; socklen_t clientAddrSize = sizeof(clientAddr); int connFd = accept(listenFd, (struct sockaddr *)&clientAddr, &clientAddrSize); // 生成随机数 srand(time(NULL)); int number = rand() % 100 + 1; while (true) { // 读取客户端的猜测 char buffer[1024]; int n = read(connFd, buffer, sizeof(buffer)); if (n == -1) { cerr << "Failed to read from socket!" << endl; break; } // 处理客户端的猜测 int guess = stoi(buffer); if (guess == number) { write(connFd, "Correct!", 10); break; } else if (guess > number) { write(connFd, "Too large!", 11); } else { write(connFd, "Too small!", 11); } } // 关闭连接 close(connFd); close(listenFd); return 0; } ``` 客户端代码: ```c++ #include <iostream> #include <string> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> using namespace std; int main() { // 创建套接字 int clientFd = socket(AF_INET, SOCK_STREAM, 0); if (clientFd == -1) { cerr << "Failed to create socket!" << endl; return -1; } // 配置地址 struct sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); serverAddr.sin_port = htons(8080); // 连接服务器 if (connect(clientFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) { cerr << "Failed to connect to server!" << endl; return -1; } while (true) { // 读取用户的输入 string input; cout << "Please guess a number (1-100): "; getline(cin, input); // 发送猜测 if (write(clientFd, input.c_str(), input.size()) == -1) { cerr << "Failed to write to socket!" << endl; break; } // 读取结果 char buffer[1024]; int n = read(clientFd, buffer, sizeof(buffer)); if (n == -1) { cerr << "Failed to read from socket!" << endl; break; } // 处理结果 cout << buffer << endl; if (string(buffer) == "Correct!") { break; } } // 关闭连接 close(clientFd); return 0; } ``` 运行以上两个程序后,就可以开始猜数字小游戏了。服务端会生成一个1-100之间的随机数,客户端需要输入一个数字进行猜测,服务端会返回猜测结果(太大、太小或正确),直到猜中为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值