C++扑克牌记忆匹配游戏
这个是我的一次课设的作业,它并不是很难,但是感兴趣的话还是可以看一看。
一 编译环境
windows10+codeblocks
二 问题描述
“记忆匹配”( memory matching game )是小孩子喜欢玩的一个益智游戏。首先准备好一墩牌,它由几个“对儿”组成。例如,假定一墩牌里有6张牌,2张是“1”,2张是“2”,另外2张是“3”。现在洗好这墩牌,然后全部反扣在桌面上。玩家选择2张反扣着的牌,把它们翻过来,如果恰好是“一对儿”,就保持它们现在的样子,不要再反扣回去。相反,如果不是“一对儿,就把它们反扣回去。一直这样翻牌,直到最后所有牌都正面朝上。
写一个程序来完成这个记忆游戏。要求使用16张牌,他们按4 x 4排列,用1~8的数字成对标记这些牌。程序应该允许玩家通过一个坐标系来指定要翻的牌。
例如,假定现在的排面布局是:
1 2 3 4
---------------------------------------
1 | 8 * * *
2 | * * * *
3 | * 8 * *
4 | * * * *
所有牌都是反扣着的,除了“8”这一对儿。“8”这对牌位于坐标(1, 1)和(2, 3)处。为了隐藏被临时翻起来的牌,请输出大量换行符,强迫旧图离开屏幕。
三 源代码
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>//文件操作文件头
#include <windows.h>//延时函数文件头
#define size 4
using namespace std;
void print();//函数声明
//窗口的数据的展示
class Mainwindow
{
public:
Mainwindow(){};
~Mainwindow(){};
void init_data();//初始化数据
int show_window(int a,int b,int c,int d);//将数据展示在界面上
private:
int new_arrary[size][size];//记录翻牌后的界面分布
int the_map[size][size];//界面上的数据
int the_number[2];//定义一个长度为2的一维数组,记录数据
int x1;
int y1;
int x2;
int y2;
};
//初始化数据
void Mainwindow::init_data()
{
x1=0;
y1=0;
x2=0;
y2=0;
the_number[0]=0;
the_number[1]=0;
//初始化界面数组
for(int j=0;j<size;j++)//写入数据
{
for(int k=0;k<size;k++)
{
new_arrary[j][k]=0;
}
}
int new_arr[16]={1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};//临时数组
int index,tmp,i;
srand(time(NULL));
for(i=0;i<16;i++)
{
index=rand()%(16-i)+i;
if(index!=i)
{
tmp=new_arr[i];
new_arr[i]=new_arr[index];
new_arr[index]=tmp;
}
}
//对类数据进行初始化,模拟随机化
the_map[0][0]=new_arr[0];
the_map[0][1]=new_arr[1];
the_map[0][2]=new_arr[2];
the_map[0][3]=new_arr[3];
the_map[1][0]=new_arr[4];
the_map[1][1]=new_arr[5];
the_map[1][2]=new_arr[6];
the_map[1][3]=new_arr[7];
the_map[2][0]=new_arr[8];
the_map[2][1]=new_arr[9];
the_map[2][2]=new_arr[10];
the_map[2][3]=new_arr[11];
the_map[3][0]=new_arr[12];
the_map[3][1]=new_arr[13];
the_map[3][2]=new_arr[14];
the_map[3][3]=new_arr[15];
ofstream fout("E:\\C++\\poker_memery\\infile.txt",ios::app);//选择你的文件记录的路径
for(int j=0;j<size;j++)//写入数据
{
for(int k=0;k<size;k++)
{
fout<<the_map[j][k]<<",";
}
fout<<endl;
}
fout<<"###################"<<endl;
fout.close();//关闭文件
}
//将数据显示在终端上
int Mainwindow::show_window(int a,int b,int c,int d)
{
x1=a-1;
y1=b-1;
x2=c-1;
y2=d-1;
//输出outfile文件
new_arrary[x1][y1]=the_map[x1][y1];
new_arrary[x2][y2]=the_map[x2][y2];
ofstream fout("E:\\C++\\poker_memery\\outfile.txt",ios::app);
fout<<"you trans location is"<<"("<<x1+1<<","<<y1+1<<") and("<<x2+1<<","<<y2+1<<")"<<endl;
for(int j=0;j<size;j++)//写入数据
{
for(int k=0;k<size;k++)
{
if(new_arrary[j][k]==0)
{
fout<<"*"<<" ";
}
else
fout<<new_arrary[j][k]<<" ";
}
fout<<endl;
}
fout<<"###################"<<endl;
fout.close();//关闭文件
the_number[0]=the_map[x1][y1];
the_number[1]=the_map[x2][y2];
//打印翻转后没有判断的牌面
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(new_arrary[i][j]!=0)
{
cout<<new_arrary[i][j]<<" ";
}
else
cout<<"* ";
if((j+1)%4==0)
{
cout<<endl;
cout<<endl;
}
}
}
//延时
double start,stop;
start=clock();
Sleep(5*1000);//延时5秒
stop=clock();
//换行
for(int i=0;i<50;i++)
{
cout<<endl;
}
//判断翻出的牌面是否相等
if(the_number[0]==the_number[1])
{
new_arrary[x1][y1]=the_map[x1][y1];
new_arrary[x2][y2]=the_map[x2][y2];
}
else
{
new_arrary[x1][y1]=0;
new_arrary[x2][y2]=0;
}
//打印翻转并判断后的牌面
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(new_arrary[i][j]==0)
cout<<"*"<<" ";
else
cout<<new_arrary[i][j]<<" ";;
if((j+1)%4==0)
{
cout<<endl;
cout<<endl;
}
}
}
//返回计数的标志
if(the_number[0]==the_number[1])
{
return 1;
}
else
return 0;
}
void print()
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
cout<<"*"<<" ";
if((k+1)%4==0)
{
cout<<endl;
cout<<endl;
}
}
}
}
//游戏的规则
void play_game()
{
int x1,y1,x2,y2;
int i=0;//记录已经配对好的对数
int cnt=0;//记录点击数
int k=0;
Mainwindow m;
m.init_data();
print();
while(i<8)
{
cout<<"please enter the location you want to select";
cin>>x1>>y1>>x2>>y2;
if((x1<=0||y1<=0||x2<=0||y2<=0)||(x1>=5||y1>=5||x2>=5||y2>=5))//报错处理error
{
cout<<"the input is error! please input again"<<endl;
continue;
}
cnt++;
k=(m.show_window(x1,y1,x2,y2));
if(k==1)
i++;
}
cout<<"game over,the input times is"<<cnt;//点击次数
}
//主函数
int main()
{
char select;
cout<<"Whether to enter the game?(y/n)"<<endl;
cin>>select;
while(select=='y')//此处采用while循环去判断是否要中途退出游戏
{
play_game();
cout<<endl;
cout<<"**************************************"<<endl;
cout<<"Do you want to play again?"<<endl;
cin>>select;
}
cout<<"out of the game";
return 0;
}
四 效果展示
y与n决定是否进入游戏
游戏结束效果--是否继续
infile文件记录初始数据
outfile文件记录过程翻转
C++扑克牌记忆匹配游戏相关推荐
- 挑战记忆力-Web前端实现记忆纸牌游戏(JS+CSS)
游戏介绍: js实现扑克牌翻牌记忆小游戏代码.连续点击翻开两张扑克牌,相同去重,不同则合上重新翻,考验你的记忆力.
- bootstrap项目实例_Vue.js 项目实践——创建记忆卡片游戏
作者:Jiawei Pan 转发链接:https://mp.weixin.qq.com/s/VXPD2p7q2S3yR9I7lzAkfw 前言 如果你刚开始学习 Vue,想巩固基础知识,那么你可以试试 ...
- Web前端---HTML+CSS+JS实现记忆纸牌游戏
游戏介绍: js实现扑克牌翻牌记忆小游戏代码.连续点击翻开两张扑克牌,相同去重,不同则合上重新翻,考验你的记忆力. 视频演示: 挑战记忆力-Web前端实现记忆纸牌游戏.mp4 主要源码展示: styl ...
- 【Java小白】 简易扑克牌比大小游戏
简易扑克牌比大小游戏 背景 涉及的知识点 源代码 代码执行结果 注意事项 结尾碎碎念 背景 自学Java已有一周,决定开始做一个慕课里的小作业:简易扑克牌比大小 ps:我还真不会打牌--_-|| 游戏 ...
- 1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结
1记忆卡片游戏代码 CardItem.h #pragmaonce #ifndef__CardItem_H__ #define__CardItem_H__ #include"cocos2 ...
- html扑克牌游戏源码,html5扑克牌消除小游戏源码
特效描述:html5扑克牌 消除小游戏源码.html5扑克牌消除小游戏源码 代码结构 1. 引入CSS 2. 引入JS 3. HTML代码 $(function(){ //实现随机洗牌 neusoft ...
- 扑克牌猜数字游戏规则_【趣味游戏】扑克牌的10种益智玩法!
原标题:[趣味游戏]扑克牌的10种益智玩法! "扑克牌"算是十分常见的材料了,你是否有想过用扑克牌也能和孩子玩出花样.玩出乐趣? 您可别小瞧这扑克牌,也能一物多玩. 今天,小编就为 ...
- 记忆翻牌游戏代码html,原生JS实现记忆翻牌游戏
本文实例为大家分享了JS实现记忆翻牌游戏的具体代码,供大家参考,具体内容如下 html代码 css代码 * { padding: 0; margin: 0; } #game { width: 600p ...
- 记忆小游戏(附源码)
记忆小游戏 哈工大 大连实训内容 Java EE编写 awt 制作界面 分三个难度 每次选出对应数量相同图标即可通关 中途中断之后重新开始 有提示功能和计时功能 jar文件可以直接运行 源码:http ...
最新文章
- python字典(dict)+常用方法操作+列表、元组、集合、字典的互相转换
- linux ubuntu 安装samba ftp nfs tftp,Ubuntu配置TFTP和NFS和samba服务配置.doc
- 浏览器对象模型BOM
- mysql恢复root密码
- 3.odoo的学习(odoo中的视图:tree视图、form视图、search视图)
- java 合成mp3_java如何把文本合成音频格式(MP3)
- Linux-dmidecode 下面查看主机BIOS信息命令
- [SCOI2016]萌萌哒
- brother打印机清零步骤_兄弟打印机清零方法兄弟打印机清零方法步骤
- c++语言编程软件视频教程下载,C++编程开发全套视频教程下载
- Python数据分析基础之CSV文件(5)
- 常用Linux命令,记录一下,避免搞忘记!
- bbs.php168,PHP168与PHPWIND深度联手 CMS+BBS整合将成趋势
- 伪静态与重定向--RewriteBase
- 分数阶傅立叶变换中午matlab,怎么做短时分数阶傅里叶变换
- 2023前端大厂面试题之JavaScript篇(4)
- 拼多多拼团电子商务论文题目(精选)
- 基于BIM+GIS技术,如何构建智慧楼宇三维可视化管控平台?
- 图片实测:智能鉴黄,哪家强
- SQL-update更新数据