c++扑克翻牌游戏

这是我大一的c++课设作业,代码是自己写的,如果需要用,请私信我。

问题描述
“记忆匹配”( memory matching game )是小孩子喜欢玩的一个益智游戏。首先准备好一墩牌,它由几个“对儿”组成。例如,假定一墩牌里有6张牌,2张是“1”,2张是“2”,另外2张是“3”。现在洗好这墩牌,然后全部反扣在桌面上。玩家选择2张反扣着的牌,把它们翻过来,如果恰好是“一对儿”,就保持它们现在的样子,不要再反扣回去。相反,如果不是“一对儿,就把它们反扣回去。一直这样翻牌,直到最后所有牌都正面朝上。

写一个程序来完成这个记忆游戏。要求使用16张牌,他们按4 x 4排列,用1~8的数字成对标记这些牌。程序应该允许玩家通过一个坐标系来指定要翻的牌。


#include <iostream>
#include <fstream> //文件操作头文件
#include <ctime>   //随机数的头文件
#include <cstdlib>
#include <cstdio>
#include <windows.h> //延时函数的头文件
using namespace std;

//定义一个Game类
class Game
{
private:
    int data[4][4];    //记录翻牌后的数据
    int display[4][4]; //记录界面上的数据
    int selected[2];   //定义一个数组,记录用户通过坐标选择的两个数
public:
    Game() {};
    virtual ~Game() {}; //虚析构函数

    void init() //初始化数据
    {
        // 初始化所有元素为0
        for (int j = 0; j < 4; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                data[j][k] = 0;
            }
        }
        // 生成随机数
        int data1[16] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8 }; //一个临时数组
        srand(time(NULL));                                                      //生成随机数
        for (int i = 0; i < 16; i++)
        {
            int j = rand() % (16 - i) + i; //随机数的范围
            if (j != i)
            {
                // 交换操作
                int temp = data1[i];
                data1[i] = data1[j];
                data1[j] = temp;
            }
        }
        // 将data1数组分成四行四列,形成初始矩阵display
        int index = 0;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                display[i][j] = data1[index++];
            }
        }
        // 将display矩阵(生成的矩阵)写入文件infile
        ofstream fout("infile.txt", ios::app); //以追加的方式打开文件夹
        for (int j = 0; j < 4; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                fout << display[j][k] << ",";
            }
            fout << endl;
        }
        fout.close(); //关闭文件
    }

    //显示界面函数,返回值是布尔类型
    bool show(int x1, int y1, int x2, int y2)
    {
        data[x1][y1] = display[x1][y1];//记录界面上的数据赋值给记录翻牌后的数据
        data[x2][y2] = display[x2][y2];

        //写入数据到outfile文件,outfile文件就是每次输入两个位置都会记录在里面,然后每次玩游戏之前会清空两个文件
        ofstream fout("outfile.txt", ios::app);
        fout << "选择点:" << endl;
        fout << "[" << x1 + 1 << "," << y1 + 1 << "]  and  [" << x2 + 1 << "," << y2 + 1 << "]" << endl;
        for (int j = 0; j < 4; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                if (data[j][k] == 0)
                {
                    fout << "*"
                        << "  ";
                }
                else
                    fout << data[j][k] << "  ";
            }
            fout << endl;
        }
        fout.close(); //关闭文件

        selected[0] = display[x1][y1];
        selected[1] = display[x2][y2];
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (data[i][j] == 0) //如果为0,则输出*号,否则输出数字
                {
                    cout << "*  ";
                }
                else
                    cout << data[i][j] << "  ";
            }
            cout << endl;
            cout << endl;
        }

        //判断选择的两个数是否相等
        if (selected[0] == selected[1])
        {
            data[x1][y1] = display[x1][y1];
            data[x2][y2] = display[x2][y2];
            cout << "bingo!";//选择的两个数是一对
        }
        else
        {
            data[x1][y1] = 0;
            data[x2][y2] = 0;
        }

        //程序延时3s
        Sleep(3 * 1000);

        //输入大量换行符,清空界面
        for (int i = 0; i < 20; i++)
        {
            cout << endl;
        }

        //打印翻转后的界面
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (data[i][j] == 0)
                    cout << "*"
                    << "  ";
                else
                    cout << data[i][j] << "  ";
                ;
            }
            cout << endl;
            cout << endl;
        }

        //返回计数值
        if (selected[0] == selected[1])
        {
            return true;
        }
        else
            return false;
    }
}; //Game类结束

//输出函数
void print()
{
    for (int j = 0; j < 4; j++)
    {
        for (int k = 0; k < 4; k++)
        {
            cout << "* ";
        }
        cout << endl;
        cout << endl;
    }
}

// 输入函数
void Input(int& x1, int& y1, int& x2, int& y2)//形参是引用
{

    while (true)//while循环
    {
        cout << "Please input the location: x1 y1 x2 y2" << endl;
        cin >> x1 >> y1 >> x2 >> y2;
        // 判断输入的坐标是否在范围内
        if (x1 <= 0 || y1 <= 0 || x2 <= 0 || y2 <= 0 || x1 >= 5 || y1 >= 5 || x2 >= 5 || y2 >= 5)
        {
            //超出范围,则提示用户输入
            cout << "Out of the range, please try again!" << endl
                << endl;
            continue;
        }
        else
        {
            return;
        }
    }
}

//游戏运行函数
void PlayGame()
{
    int x1, y1, x2, y2; // 用户输入的坐标
    int count = 0;      //对用户正确输入点进行计数
    Game game;
    game.init();
    print();
    // 只要匹配的对数不到8对,就一直进行游戏
    for (int match_num = 0; match_num < 8;)
    {
        Input(x1, y1, x2, y2);
        count++; //计数
        if (game.show(x1 - 1, y1 - 1, x2 - 1, y2 - 1))
            match_num++;
    }
    cout << "Game over! attempt: " << count << endl; //输出次数
}

//主函数

int main()
{
    char flag;
    cout << "Whether to start play?(Y/N)" << endl; //是否开始游戏
    cin >> flag;
    while (flag == 'Y' || flag == 'y')
    {
        // 删除之前已保存的文件
        remove("infile.txt");
        remove("outfile.txt");

        PlayGame(); //玩游戏的函数
        cout << "Try  again?" << endl;
        cin >> flag;
    }
    cout << "Game over!";
    return 0;
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值