C++控制台游戏2048

2048这个游戏相信好多人都玩过,想想其实功能挺简单的,无非就是上下左右移动,然后判断数字,接着随机出现2或者4。
原来我是我用好多函数实现的,显得有点乱。然后看到别人用类实现,创建对象,调用成员函数,感觉写的非常好,借鉴了好多地方改了改。

1. 我们先来看看主函数:

int main()
{
    unsigned int x = 1, y = 1;
    while (x == y)
    {
        srand((unsigned)time(NULL));
        x = rand() % 16;
        y = rand() % 16;
    }
    Array a(x, y);
    cout << a;
    char c;
    while (c=_getch())
    {
        bool flag = false;
        switch (c)
        {
        case 'w':
            flag = a.moveUp();
            break;
        case 's':
            flag = a.moveDown();
            break;
        case 'a':
            flag = a.moveLeft();
            break;
        case 'd':
            flag = a.moveRight();
            break;
        default:
            break;
        }
        if (flag)
        {
            a.upDate();
            system("cls");
            cout << a;
        }
        if (a.checkGame())
        {
            cout << endl;
            cout << "  **********************" << endl;
            cout << "  小     game over!   妖" << endl;
            cout << "  小       拜拜       妖" << endl;
            cout << "  **********************" << endl;
            Sleep(10000);
            return 0;
        }
    }
    return 0;
}

a. 上面的x 和y是为了启动游戏时随机位置出现2。
b. 重载了输出符<<.
c. 然后就是上下左右移动了,为了方便判断是否执行成功,我们用bool类型作为这些函数的返回值。
d. checkGame()是一个检查游戏是否结束的函数。

2.我们看看这几个函数怎么实现的。

Array::Array(int x1, int x2)
{
    int* p = &m_num[0][0];//定义一个指针,指向数组的首地址
    memset(m_num, 0, sizeof(m_num));//初始化数组,memset用法:memset(数组名,初始化数字,数组大小)
    *(p + x1) = 2;//刚运行程序后出现的两个数字
    *(p + x2) = 2;
}
bool Array::checkGame()
{
    bool flag = true;
    unsigned int i, j;
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (m_num[i][j] == 0 || m_num[i][3] == 0 || m_num[3][i] == 0 || m_num[3][3]==0
                || m_num[i][j] == m_num[i][j + 1] || m_num[j][i] == m_num[j + 1][i]
                || m_num[i][3]==m_num[i+1][3] || m_num[3][i]==m_num[3][i+1])
            {
                flag = false;
                return flag;
            }
        }
    }
    return flag;
}

bool Array::moveUp()
{
    unsigned int i, j, k, m;
    int temp[4][4];
    bool flag = false;

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            temp[i][j] = m_num[i][j];
        }
    }

    for (i = 0; i < 4; i++)//每行四个数字
    {
        for (k = 0; k < 3; k++)//若是将下一行的数字为0,移动上去后,还是0,所以要每行都再次比较一下
        {
            for (j = 0; j < 3; j++)//上下比较三个数字
            {
                if (m_num[j][i] == 0)
                {
                    for (m = j; m < 3; m++)
                    {
                        m_num[m][i] = m_num[m + 1][i];//第N行时,将下面的几行向上移动一次
                    }
                    m_num[3][i] = 0;//最后一行赋值0
                }

            }
        }
    }

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 3; j++)//上下比较三个数字
        {
            if (m_num[j][i] == m_num[j + 1][i])
            {
                m_num[j][i] += m_num[j + 1][i];

                for (m = j + 1; m < 3; m++)//下下一行向上移动
                {
                    m_num[m][i] = m_num[m + 1][i];
                }
                m_num[3][i] = 0;//最后一行赋值0

            }

        }
    }

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            if (temp[i][j] != m_num[i][j])//移动后和以前的数字不一样,证明还能移动
            {
                flag = true;
                return flag;
            }
        }
    }
    return flag;
}

(上下左右移动的函数差不多,只改几个数字而已,这里仅列出向上移动的函数)

bool Array::addNum(int i)
{
    bool flag = false;
    int *p = &m_num[0][0];

    if (*(p + i) == 0)
    {
        srand((unsigned)time(NULL));

        int x = rand() % 4;//此处定义出现2和4的概率,我定义为1:4
        if (x == 1)
            *(p + i) = 4;
        else
            *(p + i) = 2;
        flag = true;
    }
    return flag;
}

ostream& operator<<(ostream& is, const Array& array)
{
    is << "   上(w) 下(s) 左(a) 右(d)   " << endl;
    is << "-----------------------------" << endl;
    for (unsigned int i = 0; i < 4; i++)
    {
        for (unsigned int j = 0; j < 4; j++)
        {
            is << "|" << setw(6);
            if (array.m_num[i][j] != 0)
                is << array.m_num[i][j];
            else
                is << " ";
        }
        is << "|" << endl;
        for (unsigned int j = 0; j < 4; j++)
        {
            is << "|" << setw(6) << " ";
        }
        is << "|" << endl;
        is << "-----------------------------" << endl;
    }
    return is;
}

这是我运行的截图:
C++控制台游戏2048_第1张图片
C++控制台游戏2048_第2张图片

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