猜拳游戏c++实现

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

/*最后再优化
struct{
	int a[4];//存储电脑的四个随机数
	int b[4];//存储用户输入的四个随机数
	int count[2];//存储A和B的数量
	int win_count;//统计获胜所需次数
}gamer_config;
*/

int a[4];//存储电脑的四个随机数
int b[4];//存储用户输入的四个随机数
int count[2];//存储A和B的数量
int win_count;//统计获胜所需次数

void Four_Randon()
{
	int temp;
	do
	{
		temp = rand() % (10000 - 1000 + 1) + 1000;

		a[0] = temp % 10;
		temp /= 10;

		a[1] = temp % 10;
		temp /= 10;

		a[2] = temp % 10;
		temp /= 10;

		a[3] = temp;

	} while ((a[0] == a[1]) || (a[0] == a[2]) || (a[0]== a[3]) || (a[1] == a[2]) || (a[1] == a[3]) || (a[2] == a[3]));
	printf("%d,%d,%d,%d",a[0],a[1],a[2],a[3]);
	
}

//拆分四位数位四个数
void Get_Four(int input_number){
	b[0] = input_number / 1000;
	b[1] = input_number / 100 % 10;
	b[2] = input_number / 10 % 10;
	b[3] = input_number % 10;
}



//比较函数
void compare(){
	int countA = 0;
	int countB = 0;
	int input_number;
	printf("please input four number:\n");
	scanf("%d",&input_number);
	Get_Four(input_number);
	for (int i = 0; i < 4;i++){
		if (a[i] == b[i]){
			countA++;
		}
	}
	
	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4; j++){
			if (a[i] == b[j]){
				countB++;
			}
		}
	}
	count[0] = countA;
	count[1] = countB;

}


void playgame(){
	printf("**************start game*************\n");
	Four_Randon();
	printf("computer have make four random number\n");
	while (count[0] != 4){
		compare();
		win_count++;
		printf("%dA%dB", count[0], count[1]);
	}
	printf("**************you win*************\n");
	printf("%d\n",win_count);
}

/*
//写进数据库ID与获胜所需次数
int do_count(MSG * msg, sqlite3 * db,int cishu)
{
	char * errmsg;
	char sql[128] = { 0 };
	sprintf(sql, "insert into history values('%s','%s');", msg->name, cishu);
	if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("%s", errmsg);
		printf("排序失败");
	}
	else
	{
		printf("排序完成\n");
	}


	return 0;
}
*/



/*
void query(MSG * msg, sqlite3 * db){
	//查询数据库中获胜者的前10排名并打印ID与次数
	char * errmsg;
	char sql[128] = { 0 };
	sprintf(sql, "SELECT * FROM COMPANY ORDER BY win_count DESC;");
	if (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("%s", errmsg);
		printf("your count have already exit cannot cover");
	}
	else
	{
		printf("your count successfully save\n");
	}

	if(sqlite3_exec(sql,"select *from history;",NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("%s",errmsg);
	}
	return 0;
}
*/

int main(){
	
	playgame();


	

	system("pause");


	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您介绍一下猜拳游戏C++代码实现。在面向对象的设计中,我们可以将猜拳游戏的参与者抽象为一个Player类,其中包含了玩家的姓名和出拳的方法。另外,我们还可以创建一个Game类来管理游戏的进行,包括判断胜负、输出结果等功能。 下面是一个简单的猜拳游戏C++代码示例: ```cpp #include <iostream> #include <ctime> using namespace std; // 定义手势的枚举类型 enum Gesture { ROCK, PAPER, SCISSORS }; // 玩家类 class Player { private: string name; Gesture gesture; public: Player(string n) : name(n) {} void setGesture(Gesture g) { gesture = g; } Gesture getGesture() { return gesture; } }; // 游戏类 class Game { private: Player* player1; Player* player2; public: Game(Player* p1, Player* p2) : player1(p1), player2(p2) {} void play() { Gesture g1 = player1->getGesture(); Gesture g2 = player2->getGesture(); if (g1 == g2) { cout << "平局!" << endl; } else if ((g1 == ROCK && g2 == SCISSORS) || (g1 == PAPER && g2 == ROCK) || (g1 == SCISSORS && g2 == PAPER)) { cout << player1->getName() << " 获胜!" << endl; } else { cout << player2->getName() << " 获胜!" << endl; } } }; int main() { // 创建两个玩家对象 Player* p1 = new Player("玩家1"); Player* p2 = new Player("玩家2"); // 随机生成玩家的手势 srand(time(0)); Gesture g1 = static_cast<Gesture>(rand() % 3); Gesture g2 = static_cast<Gesture>(rand() % 3); p1->setGesture(g1); p2->setGesture(g2); // 创建游戏对象并进行游戏 Game game(p1, p2); game.play(); // 释放内存 delete p1; delete p2; return 0; } ``` 这段代码中,我们定义了一个Gesture枚举类型来表示石头、剪刀和布三种手势。Player类表示玩家,包含了玩家的姓名和出拳的方法。Game类表示游戏,包含了两个玩家对象,并实现了游戏的进行和判断胜负的功能。 相关问题: 1. 什么是枚举类型? 2. 如何在C++中生成随机数? 3. 如何动态创建和释放内存?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值