C/C++游戏编程实例-猜数字、数字炸弹、三子棋

猜数字
cpp 复制代码
#include<iostream>
using namespace std;
#include<ctime>

int main()	//猜数字
{
	unsigned int count_max = 8;
	srand((unsigned int)time(NULL));
	unsigned int num = rand() % 100 + 1;		//随机生成1~100
	unsigned int a = 0, count = 0, score = 0;

	while (1)
	{
		cout << "请输入数字:" << endl;
		cin >> a;
		if (a > num)
		{
			cout << "数字过大。" << endl;
			count++;
			if (count > count_max) break;
		}
		else if (a < num)
		{
			cout << "数字过小。" << endl;
			count++;
			if (count > count_max) break;
		}
		else
		{
			cout << "猜测正确!" << endl;
			score += 10;
			cout << "你已得:" << score << "分" << endl;
			count = 0;
			count_max = count_max / 2;
			cout << "游戏继续..." << endl;
			srand((unsigned int)time(NULL));
			num = rand() % 100 + 1;
		}
	}
	cout << "GAME OVER!" << endl;
	cout << "数字为:" << num << endl;
	cout << "获得积分:" << score << endl;
}
数字炸弹
cpp 复制代码
#include<iostream>
//使用< iostream>时,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。命名空间std封装的是标准程序库的名称。
using namespace std;
#include<ctime>

int main()	//数字炸弹
{
	srand((unsigned int)time(NULL));
	while (1)
	{
		unsigned int num = rand() % 100 + 1;		//随机生成1~100
		unsigned int a = 0, max = 100, min = 1;
		while (1)
		{
			cout << "炸弹在" << min << "到" << max << "之间(边界可选)。" << endl;
			cout << "请选择数字:" << endl;
			cin >> a;
			if (a >= min && a <= max)
			{
				if (a > num)
				{
					max = a - 1;
					cout << "请继续..." << endl;
					
				}
				else if (a < num)
				{
					min = a + 1;
					cout << "请继续..." << endl;
				}
				else
				{
					break;
				}
			}
			else
			{
				cout << "警告!输入错误!" << endl;
			}
			if (max == min)
			{
				cout << "艾玛,接下来的小兄弟中奖了~~~" << endl;
				break;
			}
		}
		cout << "Bomb!炸弹是:" << num << endl;
		cout << "你输了!" << endl;
		cout << "按空格键重新开始游戏" << endl;
		system("pause");//按任意键继续
		system("cls");//清屏
	}
}
三子棋

首先导入game.h:

cpp 复制代码
#define ROW 3
#define COL 3

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);

初始化棋面:

cpp 复制代码
void InitBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}

	}
}

棋面显示:

cpp 复制代码
void DisplayBoard(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ",board[i][j]);
			if (j < col - 1)printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)printf("|");
			}
			printf("\n");
		}
	}
}

玩家输入x、y坐标控制棋子移动:

cpp 复制代码
void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0,y = 0;
	printf("玩家走>\n");
	while (1)
	{
        //scanf("%d %d", &x,&y);
		printf("请输入要走的x坐标:");
		scanf("%d", &x);
		printf("请输入要走的y坐标:");
		scanf("%d", &y);
		if (x > 0 && x <= row && y > 0 && y <= col && board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '*';
			break;
		}
		else
		{
			printf("坐标非法,请重新输入!\n");
		}
	}
	
}

电脑随机产生棋子位置控制棋子移动:

cpp 复制代码
void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0, y = 0;
	printf("电脑走>\n");
	while (1)
	{
		x = rand() % row;
		y= rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}


}

判断棋面是否满:

cpp 复制代码
int IsFull(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

游戏输赢的机制:

cpp 复制代码
char IsWin(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	for (int j = 0; j < col; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
		{
			return board[1][j];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1]== board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1]== board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (IsFull(board, row, col) == 1)
	{
		return 'Q';
	}
	return 'C';
}

游戏显示菜单:

cpp 复制代码
void menu()
{
	printf("*************************************\n");
	printf("**********1.play     0.exit**********\n");
	printf("*************************************\n");
}

游戏执行机制:

cpp 复制代码
void game()
{
	char board[ROW][COL] = { 0 };
	char ret;
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	while (1)
	{
		PlayerMove(board, ROW, COL);
		system("cls");
		DisplayBoard(board, ROW, COL);
		ret=IsWin(board, ROW, COL);
		if (ret != 'C')break;

		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')break;
	}
	if (ret == '*')
	{
		printf("玩家胜利!\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢!\n");
	}
	else
	{
		printf("平局!\n");
	}
}

执行主函数:

cpp 复制代码
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
		{
			system("cls");
			game();
			break;
		}
		case 0:
		{
			system("cls");
			printf(" ˳   Ϸ\n");
			break;
		}
		default:
		{
			printf("请重新输入!\n");
			system("pause");
			system("cls");
			break;
		}
		}
	} while (input);

	return 0;
}

结果:

相关推荐
Ni-Guvara2 分钟前
函数对象笔记
c++·算法
似霰6 分钟前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
芊寻(嵌入式)17 分钟前
C转C++学习笔记--基础知识摘录总结
开发语言·c++·笔记·学习
獨枭18 分钟前
C++ 项目中使用 .dll 和 .def 文件的操作指南
c++
霁月风21 分钟前
设计模式——观察者模式
c++·观察者模式·设计模式
橘色的喵22 分钟前
C++编程:避免因编译优化引发的多线程死锁问题
c++·多线程·memory·死锁·内存屏障·内存栅栏·memory barrier
一颗松鼠26 分钟前
JavaScript 闭包是什么?简单到看完就理解!
开发语言·前端·javascript·ecmascript
泉崎26 分钟前
11.7比赛总结
数据结构·算法
有梦想的咸鱼_27 分钟前
go实现并发安全hashtable 拉链法
开发语言·golang·哈希算法
你好helloworld28 分钟前
滑动窗口最大值
数据结构·算法·leetcode