Hi~ o(* ̄▽ ̄*)ブ我来了!!!!!

前面说过的要发游戏的,吾来也!!!

今天我们的游戏是——《2048简易版》

额,不要问我规则,游戏里有哦!!!

嗯好了,上代码吧,同志们吧!!!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <termio.h>
#include <iomanip>
#define clear() cout << "\033c" << flush
using namespace std;

int num[10][10]; // 值为0表示没有值,非0表示有数据
int score = 0;
int rd = 0;
bool validMove = false; // 是否是有效移动

int getch() // 不回显函数
{
    struct termios nts, ots;
    // 得到当前终端(0表示标准输入)的设置
    if (tcgetattr(0, &ots) < 0) return EOF;
    // 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理
    nts = ots;
    cfmakeraw(&nts); 
    // 设置上更改之后的设置
    if (tcsetattr(0, TCSANOW, &nts) < 0) return EOF;
    // 设置还原成老的模式
    int cr;
    cr = getchar();
    if (tcsetattr(0, TCSANOW, &ots) < 0)  return EOF;
    return cr;
}

void MoveUp() // 向上移动
{   
    validMove = false;
    for (int i = 1; i <= 4; i++) // 行
    {
        for (int j = 1; j <= 4; j++) // 列
        {
        	// 2 2 2 2 不能合并为 8 而应是 4 4 一个数字只能合并一次
            bool merge = false; 
            if (num[i][j] != 0)
            {
                for (int k = i; k >= 2; k--) // 从下往上
                {
                    if (num[k][j] == num[k - 1][j] && !merge) // 合并
                    {
                        num[k - 1][j] *= 2;
                        num[k][j] = 0;
                        score += num[k - 1][j];
                        merge = true;
                        validMove = true;
                    }
                    else if (num[k - 1][j] == 0) // 向上一格
                    {
                        num[k - 1][j] = num[k][j];
                        num[k][j] = 0;
                        validMove = true;
                    }
                    else break;
                }
            }
        }   
    }
}

void MoveDown() // 向下移动
{   
    validMove = false;
    for (int i = 4; i >= 1; i--) // 行
    {
        for (int j = 1; j <= 4; j++) // 列
        {
            bool merge = false;
            if (num[i][j] != 0)
            {
                for (int k = i; k <= 3; k++) // 从上往下
                {
                    if (num[k][j] == num[k + 1][j] && !merge) // 合并
                    {
                        num[k + 1][j] *= 2;
                        num[k][j] = 0;
                        score += num[k + 1][j];
                        merge = true;
                        validMove = true;
                    }
                    else if (num[k + 1][j] == 0) // 向上一格
                    {
                        num[k + 1][j] = num[k][j];
                        num[k][j] = 0;
                        validMove = true;
                    }
                    else break;
                }
            }
        }   
    }
}

void MoveRight() // 向右移动
{   
    validMove = false;
    for (int i = 1; i <= 4; i++) // 行
    {
        for (int j = 4; j >= 1; j--) // 列
        {
            bool merge = false;
            if (num[i][j] != 0)
            {
                for (int k = j; k <= 3; k++) // 从左往右
                {
                    if (num[i][k] == num[i][k + 1] && !merge) // 合并
                    {
                        num[i][k + 1] *= 2;
                        num[i][k] = 0;
                        score += num[i][k + 1];
                        merge = true;
                        validMove = true;
                    }
                    else if (num[i][k + 1] == 0) // 向右一格
                    {
                        num[i][k + 1] = num[i][k];
                        num[i][k] = 0;
                        validMove = true;
                    }
                    else break;
                }
            }
        }   
    }
}

void MoveLeft() // 向左移动
{   
    validMove = false;
    for (int i = 1; i <= 4; i++) // 行
    {
        for (int j = 1; j <= 4; j++) // 列
        {
            bool merge = false;
            if (num[i][j] != 0)
            {
                for (int k = j; k >= 2; k--) // 从右往左
                {
                    if (num[i][k] == num[i][k-1] && !merge) // 合并
                    {
                        num[i][k-1] *= 2;
                        num[i][k] = 0;
                        score += num[i][k-1];
                        merge = true;
                        validMove = true;
                    }
                    else if (num[i][k-1] == 0) // 向右一格
                    {
                        num[i][k-1] = num[i][k];
                        num[i][k] = 0;
                        validMove = true;
                    }
                    else break;
                }
            }
        }   
    }
}

bool gameOver() // 判断游戏是否结束
{
    for (int i = 1; i <= 4; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            if (num[i][j] == 0) return false; // 还有空位
            if (i > 1)
            {
                if (num[i - 1][j] == num[i][j]) // 上下还能合并
                    return false;
            }
            if (j > 1)
            {
                if (num[i][j - 1] == num[i][j]) // 左右还能合并
                    return false;
            }
        }
    }
    
    return true;
}

void drawBoard() // 打印当前表格
{
    // 输出上边框
    cout << "  ╔";
    for (int i = 1; i <= 4 - 1; i++) cout << "════╤";
    cout << "════╗\n";
    // 输出中间部分
    for (int i = 1; i <= 4; i++) // 行
    {
        cout << "  ║";
        for (int j = 1; j <= 4; j++) // 列
        {
            if (num[i][j] > 0)
            {
                if (num[i][j] > score) score = num[i][j];
                cout << setw(4) << num[i][j]; 
            } 
            else cout << "    ";
            if (j != 4)
                cout << "│";
            else
                cout << "║";
        }
        cout << " \n";
        // 输出下边框
        if (i != 4)
        {
            cout << "  ╟";
            for (int i = 1; i <= 4 - 1; i++) cout << "────┼";
            cout << "────╢\n";
        }
        else
        {
            cout << "  ╚";
            for (int i = 1; i <= 4 - 1; i++) cout << "════╧";
            cout << "════╝\n";
        }
    }
}

void Lose() // 游戏失败
{
    cout << "**********************************" << endl;
    cout << "——————————— ROUND:" << rd << " ————————————" << endl;
    cout << "——————————— SCORE:" << score << " ———————————" << endl;
    cout << "——————————— 游戏失败! ————————————" << endl;
    cout << "**********************************" << endl << endl;
    exit(0);
}

void Win() // 玩家获胜
{
    cout << "**********************************" << endl;
    cout << "——————————— ROUND:" << rd << " ————————————" << endl;
    cout << "——————————— SCORE:" << score << " ———————————" << endl;
    cout << "——————————— 玩家获胜! ————————————" << endl;
    cout << "**********************************" << endl << endl;
    exit(0);
}

void full() // 填充数据
{
	int x[20] = {}, y[20] = {}, cur = 0;
    for (int i = 1; i <= 4; i++)
    {
    	for (int j = 1; j <= 4; j++)
    	{
    		if (num[i][j] == 0)
    		{
    			cur++;
    			x[cur] = i;
    			y[cur] = j;
    		}
        }
    }
        
    if (cur > 0) // 还有空位
    {
        srand(time(0));
        int cc = rand() % cur + 1; // 随机从空位中取出一个
        int v =  (rand() % 2 + 1) * 2; // 计算空位应该填写的数据
        num[x[cc]][y[cc]] = v;
    }
    cout << "\033c" << flush;
}

void game() // 开始游戏
{
    cout << "\033c" << flush;
    cout << "**********************************" << endl;
    cout << "—————————— Ready Go~ ————————————" << endl;
    cout << "**********************************" << endl;
    drawBoard(); // 打印棋盘
    
    char ch = 0;
    while (!gameOver()) // 当游戏没结束时
    {
    	
        cout << "Key:" << ch << " Round:" << rd << " Score:" << score << endl;
        cout << "Tips:" << endl;
        cout << "w —— 上移, ";
        cout << "s —— 下移, ";
        cout << "a —— 左移" << endl;
        cout << "d —— 右移, ";
        cout << "e —— 退出游戏, ";
        cout << "r —— 开始新游戏" << endl;
        cout << "选择以上指令!" << endl;
        
        ch = getch(); // 输入操作指令
        rd++; // 记录操作次数
        if (ch == 'w') MoveUp();
        else if (ch == 's') MoveDown();
        else if (ch == 'a') MoveLeft();
        else if (ch == 'd') MoveRight();
        else if (ch == 'e') exit(0);
        else if (ch == 'r') // 开始新游戏
        {
        	score = 0; // 清空分数
			rd = 0; // 清空操作次数
        	memset(num, 0, sizeof(num)); // 清空表格数据
        	return; 
        }
        else
        {
            cout << endl << "———————输入有误,请重新输入指令!———————" << endl << endl;
            continue;
        }
        if (validMove) full(); // 刚刚是有效移动,产生新数据
        cout << "\033c" << flush;
        drawBoard(); // 打印棋盘
    }
    if (score >= 2048) Win();
    else Lose();
}

void intro() // 游戏规则展示
{
    cout << "***********************************************************" << endl;
    cout << "欢迎来到 2048 游戏~" << endl;
    cout << "在这个游戏里会有一个 4*4 的表格,在每个方格中会随机产生 2 或者 4;" << endl;
    cout << "可以选择 w、s、a、d,上下左右的滑动,如果遇到相同的数字,该数字乘 2;" << endl;
    cout << "当 16 个方格全满,无法继续滑动时,游戏结束!" << endl; 
    cout << "1. 当累计分数超过 2048 时,玩家获胜;" << endl; 
    cout << "2. 当累计分数不及 2048 时,游戏失败!" << endl; 
    cout << "点击【ENTER】开始游戏" << endl;
    cout << "点击【ESC】退出游戏" << endl;
    cout << "***********************************************************" << endl;
}

int main()
{
    while (true)
    {
        intro(); // 游戏规则展示
        char ch;
        ch = getch(); // 不回显函数,当用户按下某个字符时,函数自动读取,无需按回车
        if (ch == 13) // CR回车的ascii是13
        {
            full(); // 初始化时表格中就有两个数据
            full();
            game(); // 开始游戏
        }
        else if (ch == 27) // ESC的ascii是27
        {
            cout << "退出游戏!" << endl;
            break;
        }
        else  cout << "输入有误,请再次输入!" << endl;
    }
    return 0;
}

呵呵,各位这次的文章就到这了啦,886啦!!!!!!!!

本站作者已申明原创,禁止转载!

文章内容属作者个人观点,不代表本站立场,如有侵权立删。

   口袋儿题库-青少儿编程自测题库