C语言实现消消乐小游戏

 更新时间:2020年12月16日 10:41:00   作者:傻子是小傲娇  
这篇文章主要为大家详细介绍了C语言实现消消乐小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现消消乐小游戏的具体代码,供大家参考,具体内容如下

代码:

#include<iostream>
#include<cstdlib>
#include<bitset>
#include<conio.h>
#include<time.h>
#include <windows.h>
#include<queue>
#include<algorithm>
using namespace std;
 
struct node{
 int x, y;
};
 
const int size = 9;
//地图大小
int Score;
//得分
 
 
int Map[size][size];
//主地图
int Map_2[size][size];
//辅助地图 用于显示
int dropNumbe[size][size];
//下降距离统计
int bfsVis[size][size];
//bfs标记数组
 
int xx[4] = { 0, 0, 1, -1 };
int yy[4] = { 1, -1, 0, 0 };
//方向调整数组
 
int random();
//随机数产生
void initMap();
//地图初始化
void updateMap(int flag);
//打印地图
void printSqure(int i);
//形状打印
void dropNumberCount();
//下落高度统计
void squreDrop();
//根据下落高度更新地图
void reflashMap();
//下落后的地图新元素添加
void mapCopy();
//数组复制
void displayUpdate();
//消失效果
bool updateCheck();
//检测是否有符合消除条件,通过bfs消除
bool bfsCheck(int x, int y, int squre);
//bfs标记及越界检测
void Bfs(int x, int y);
 
 
 
int main()
{
 initMap();
 Score = 0;
 updateMap(1);
 while (true)
 {
 bool isUpdate = false;
 int x1, x2, y1, y2;
 cout << "please input x1,y1,x2,y2" << endl;
 cin >> x1 >> y1 >> x2 >> y2;
 mapCopy();
 swap(Map[x1][y1], Map[x2][y2]);
 
 updateMap(1);
 
 isUpdate = updateCheck();
 if (isUpdate){
 dropNumberCount();
 squreDrop();
 cout << endl;
 cout << "-------------------- drop" << endl;
 updateMap(1);
 
 cout << endl;
 cout << "-------------------- reflash" << endl;
 reflashMap();
 updateMap(1);
 
 
 while (isUpdate = updateCheck()){
 dropNumberCount();
 squreDrop();
 cout << endl;
 cout << "-------------------- drop" << endl;
 updateMap(1);
 
 cout << endl;
 cout << "-------------------- reflash" << endl;
 reflashMap();
 updateMap(1);
 system("pause");
 }
 }
 else{
 system("CLS");
 cout << "GAME OVER!" << endl;
 cout << "Total Score: ";
 cout << Score << endl;
 break;
 }
 
 }
}
 
int random(){
 //随机数产生
 int temp;
 while (1){
 temp = rand() % 4;
 if (temp >= 0)return temp;
 }
}
void initMap(){
 //地图初始化
 srand((int)time(0));
 for (int i = 1; i < size; i++){
 for (int j = 1; j < size; j++){
 Map[i][j] = (rand() % 4);
 }
 }
}
void printSqure(int i){
 //形状打印
 switch (i){
 case -1:cout << "□"; break;
 case 0:cout << "■"; break;
 case 1:cout << "★"; break;
 case 2:cout << "▲"; break;
 case 3:cout << "●"; break;
 }
}
void updateMap(int flag){
 //打印地图
 cout << "Current Score:";
 cout << Score << endl;
 for (int i = 0; i < size; i++){
 for (int j = 0; j < size; j++){
 if (i == 0){
 cout << j << " ";
 }
 else if (j == 0){
 cout << i;
 }
 else{
 int x;
 if (flag == 1)x = Map[i][j];
 else x = Map_2[i][j];
 printSqure(x);
 }
 }
 cout << endl;
 }
}
bool updateCheck(){
 //检测是否有符合消除条件,通过bfs消除
 bool isUpdate = false;
 
 memset(bfsVis, 0, sizeof(bfsVis));
 
 for (int i = 1; i < size; i++){
 for (int j = 1; j < size; j++){
 if (bfsVis[i][j] == 0){
 bool mark = false;//存在三个一排
 if ((i - 1 >= 1) && (i + 1 < size)){
  int t1, t2, t3;
  t1 = Map[i][j];
  t2 = Map[i - 1][j];
  t3 = Map[i + 1][j];
  if ((t1 == t2) && (t1 == t3)){
  mark = true;
  isUpdate = true;
  }
 }
 if ((j - 1 >= 1) && (j + 1 < size)){
  int t1, t2, t3;
  t1 = Map[i][j];
  t2 = Map[i][j - 1];
  t3 = Map[i][j + 1];
  if ((t1 == t2) && (t1 == t3)){
  mark = true;
  isUpdate = true;
  }
 }
 
 if (mark){
  mapCopy();
  Bfs(i, j);
 }
 
 
 }
 }
 }
 return isUpdate;
}
bool bfsCheck(int x, int y, int squre){
 //bfs标记及越界检测
 if (x < 1 || x >= size || y < 1 || y >= size)return false;
 if (bfsVis[x][y] != 0 || Map[x][y] != squre)return false;
 return true;
}
void Bfs(int x, int y){
 int ans = 0;
 queue<node>S;
 node now, next;
 now.x = x, now.y = y;
 bfsVis[x][y] = 1;
 //point_vis[x][y] = 1;
 
 int squre = Map[x][y];
 
 Map[x][y] = -1;
 
 cout << "BFS: " << x << " " << y << endl;
 S.push(now);
 while (!S.empty()){
 now = S.front();
 ans++;
 S.pop();
 for (int i = 0; i < 4; i++){
 next = now;
 next.x += xx[i], next.y += yy[i];
 if (bfsCheck(next.x, next.y, squre) == 0)continue;
 bfsVis[next.x][next.y] = 1;
 
 Map[next.x][next.y] = -1;
 
 S.push(next);
 }
 }
 Score += ans;
 displayUpdate();
}
 
void displayUpdate(){
 //消失效果
 system("CLS");
 updateMap(1);
 Sleep(500);
 system("CLS");
 updateMap(2);
 Sleep(500);
 system("CLS");
 updateMap(1);
 Sleep(500);
 system("CLS");
 updateMap(2);
 Sleep(500);
 system("CLS");
 updateMap(1);
}
 
void dropNumberCount(){
 //下落高度统计
 for (int i = 1; i < size; i++){
 for (int j = 1; j < size; j++){
 if (Map[i][j] == -1){
 dropNumbe[i][j] = 0;
 continue;
 }
 int sum = 0;
 for (int z = i + 1; z < size; z++){
 if (Map[z][j] == -1)sum++;
 }
 dropNumbe[i][j] = sum;
 }
 }
}
 
void squreDrop(){
 //根据下落高度更新地图
 for (int i = size - 1; i >= 1; i--){
 for (int j = 1; j < size; j++){
 int temp = dropNumbe[i][j];
 if (temp != 0){
 Map[i + temp][j] = Map[i][j];
 Map[i][j] = -1;
 }
 }
 }
}
 
void reflashMap(){
 //下落后的地图新元素添加
 for (int i = 1; i < size; i++){
 for (int j = 1; j < size; j++){
 if (Map[i][j] == -1){
 Map[i][j] = (rand() % 4);
 }
 }
 }
}
 
void mapCopy(){
 //数组复制
 for (int i = 1; i < size; i++){
 for (int j = 1; j < size; j++){
 Map_2[i][j] = Map[i][j];
 }
 }
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • opencv配置的完整步骤(win10+VS2015+OpenCV3.1.0)

    opencv配置的完整步骤(win10+VS2015+OpenCV3.1.0)

    OpenCV是计算机视觉中经典的专用库,其支持多语言、跨平台,功能强大,这篇文章主要给大家介绍了关于opencv配置(win10+VS2015+OpenCV3.1.0)的相关资料,需要的朋友可以参考下
    2021-06-06
  • C++语言基础 命名空间

    C++语言基础 命名空间

    一个中大型软件往往由多名程序员共同开发,会使用大量的变量和函数,当有两个人都同时定义了一个名字相同的全局变量或函数的时候,若是把他们的代码整合在一块编译,此时编译器就会提示变量或函数重复定义,C++为了解决这个问题,便引用了命名空间(namespace)的概念
    2020-01-01
  • C语言指针基础详解

    C语言指针基础详解

    这篇文章主要介绍了C语言指针的基础,主要对C语言中指针的本质及常见用法做了较为通俗易懂的分析,是后续深入学习C语言的基础,需要的朋友可以参考下
    2021-10-10
  • C++ 中消息队列函数实例详解

    C++ 中消息队列函数实例详解

    这篇文章主要介绍了C++ 中消息队列函数实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • C++遍历文件夹目录的方法

    C++遍历文件夹目录的方法

    这篇文章主要介绍了C++遍历文件夹目录的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • opencv实现机器视觉检测和计数的方法

    opencv实现机器视觉检测和计数的方法

    在机器视觉中,有时需要对产品进行检测和计数。其难点无非是对于产品的图像分割。本文就来介绍一下机器视觉检测和计数的实现,感兴趣的可以参考一下
    2021-05-05
  • C++利用PCL点云库操作txt文件详解

    C++利用PCL点云库操作txt文件详解

    这篇文章主要为大家详细介绍了C++如何利用PCL点云库操作txt文件,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2024-01-01
  • Qt编写秒表功能

    Qt编写秒表功能

    这篇文章主要为大家详细介绍了Qt编写秒表功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C语言模拟实现简单扫雷游戏

    C语言模拟实现简单扫雷游戏

    这篇文章主要为大家详细介绍了C语言模拟实现简单扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 纯C语言:折半查找源码分享

    纯C语言:折半查找源码分享

    这篇文章主要介绍了纯C语言:折半查找源码,有需要的朋友可以参考一下
    2014-01-01

最新评论