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
#include
#include
#include
#include //文件操作文件头
#include //延时函数文件头
#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     {
        for(int k=0;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     {
        for(int k=0;k         {
            fout<         }
        fout<     }
    fout<<"###################"<     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"<<"("<     for(int j=0;j     {
        for(int k=0;k         {
            if(new_arrary[j][k]==0)
            {
                fout<<"*"<<"  ";
            }
            else
                fout<         }
        fout<     }
    fout<<"###################"<     fout.close();//关闭文件

    the_number[0]=the_map[x1][y1];
    the_number[1]=the_map[x2][y2];

 

    //打印翻转后没有判断的牌面
    for(int i=0;i     {
        for(int j=0;j         {
            if(new_arrary[i][j]!=0)
            {
                cout<             }
            else
                cout<<"*  ";

            if((j+1)%4==0)
            {
                cout<                 cout<             }
        }
    }

    //延时
    double start,stop;
    start=clock();

    Sleep(5*1000);//延时5秒

    stop=clock();

    //换行
    for(int i=0;i<50;i++)
    {
        cout<     }

    //判断翻出的牌面是否相等
    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     {
        for(int j=0;j         {
            if(new_arrary[i][j]==0)
                cout<<"*"<<"  ";
            else
                cout<             if((j+1)%4==0)
            {
                cout<                 cout<             }
        }
    }

    //返回计数的标志
    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<                 cout<             }
        }
    }
}

//游戏的规则
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"<             continue;
        }
        cnt++;
        k=(m.show_window(x1,y1,x2,y2));
        if(k==1)
            i++;

    }
    cout<<"game over,the input times is"< }


//主函数
int main()
{
    char select;
    cout<<"Whether to enter the game?(y/n)"<     cin>>select;
    while(select=='y')//此处采用while循环去判断是否要中途退出游戏
    {
        play_game();
        cout<         cout<<"**************************************"<         cout<<"Do you want to play again?"<         cin>>select;
    }
    cout<<"out of the  game";
    return 0;
}

四 效果展示

y与n决定是否进入游戏


C++扑克牌记忆匹配游戏_第1张图片

游戏结束效果--是否继续

C++扑克牌记忆匹配游戏_第2张图片

infile文件记录初始数据

 

C++扑克牌记忆匹配游戏_第3张图片

outfile文件记录过程翻转

 

C++扑克牌记忆匹配游戏_第4张图片

C++扑克牌记忆匹配游戏_第5张图片

你可能感兴趣的:(C++)