命令行之2048

命令行之2048

大家好,又见面了,我是全栈君。

命令行之2048
2048挺火的游戏,在命令行实现是什么样呢?小尝试了下,这里感谢css大神给debug,还有就是游戏空格处採用特殊字符,因此程序在linux下跑可能会出现乱码,能够手动调整

游戏截图:

命令行之2048


命令行之2048


命令行之2048


代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#define Coord_x 15
#define Coord_y 15
#define Height  20
#define Width  16

using namespace std;

class Game
{
    public:
        int score;
        int num[5][5];

    public:
        Game(int s = 0);
        bool control(char ch);
        void init();
        void make();
}G;

class Console
{
    public:
        void gotoxy(HANDLE hOut, int x, int y);
        void enter_game();
        void window();
        void show();
        void start_game();
        void end_game();

    friend class Game;
}C;

Game::Game(int s)
{
    score = s;
    memset(num,0,sizeof(num));
}

void Console::gotoxy(HANDLE hOut, int x, int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOut, pos);
}

void Console::enter_game()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    system("title 2048 by Tc");
    gotoxy(hOut,Coord_x+Width-1,Coord_y-12);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
    cout<<"  欢迎进入 2 0 4 8";
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    gotoxy(hOut,Coord_x+Width-25,Coord_y-8);
    cout << "        *  *           *  *            *        *        *  *         " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-7);
    cout << "     *        *     *        *        *        *       *      *            " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-6);
    cout << "    *        *     *          *      *        *         *    *            " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-5);
    cout << "           *       *          *     *        *             *                     " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-4);
    cout << "         *         *          *    *  *  *  *  * *      *      *     " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-3);
    cout << "       *           *          *            *          *          *                 " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-2);
    cout << "     *              *        *            *            *        *               " << endl;
    gotoxy(hOut,Coord_x+Width-25,Coord_y-1);
    cout << "  *  *  *  *  *        *  *              *                *  *      " << endl;
    gotoxy(hOut,Coord_x+Width+1,Coord_y+3);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED);
    cout << "按回车键进入游戏\n"<< endl;;
    while(1)
    {
        char c;
        if(kbhit())
        {
            c = getch();
            if(c == 13)
            {
                system("CLS");
                C.start_game();
            }
        }
    }
    if(getch() == 27)
    {
        C.end_game();
    }
}


void Console::end_game()
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    system("CLS");
    gotoxy(hOut,Coord_x+Width+5,Coord_y-10);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED);
    cout << "游戏结束";
    gotoxy(hOut,Coord_x+Width+5,Coord_y-7);
    cout << "终于得分 : " << G.score << endl;
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
    exit(0);
}

void Console::window()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    system("title 2 0 4 8 by Tc");
    system("color 0C");
    gotoxy(hOut,Coord_x+Width-2,Coord_y-13);
    cout << " *******";
    gotoxy(hOut,Coord_x+Width-2,Coord_y-12);
    cout << " 2 0 4 8";
    gotoxy(hOut,Coord_x+Width-2,Coord_y-11);
    cout << " *******";
    gotoxy(hOut,Coord_x+2*Width+3,Coord_y+6);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
    gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 8);
    cout << "w键 : 向上  " << "s键 : 向下";
    gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 6);
    cout << "a键 : 向左  " << "d键 : 向右";
    gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 4);
    cout << "Esc :退出";
}

void Console :: show()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);
    gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 10);
    cout << "                   ";
    gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 10);
    cout << "分数 : " << G.score;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            if(G.num[i][j] != 0)
            {
                cout << " ";
                SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED);
                gotoxy(hOut, Coord_x + 6 + j * 8,Coord_x - 8 + i * 4);
                cout << G.num[i][j];
            }
            else
            {
                cout << " ";
                SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN);
                gotoxy(hOut, Coord_x + 6 + j * 8,Coord_x - 8 + i * 4);
                cout << "■";
            }
        }
        cout << endl;
    }

}

bool Game::control(char dir)
{
    int num2[4][4];
    for(int n = 0; n < 4; n++)
        for(int m = 0; m < 4; m++)
            num2[n][m] = num[n][m];
    bool IsMove = false;
    if(dir == 'w' || dir == 'W')
    {
        for(int j = 0; j < 4; j++)
        {
            int ii = 0, temp = 5;
            for(int i = 0; i < 4; i++)
            {
                if(num[i][j] == 0)
                    continue;
                else
                {
                    temp = i;
                    break;
                }
            }
            if(temp == 5)
                continue;
            num[ii][j] = num[temp][j];
            for(int i = temp + 1; i < 4; i++)
            {
                if(num[i][j] == 0)
                    continue;
                else if(num[i][j] != 0 && num[i][j] == num[ii][j])
                {
                    num[ii][j] *= 2;
                    G.score += num[ii][j];
                    num[i][j] = 0;
                }
                else if(num[i][j] != 0 && num[i][j] != num[ii][j])
                    num[++ii][j] = num[i][j];
            }
            while(ii < 4)
                num[++ii][j] = 0;
            for(int n = 0; n < 4; n++)
            {
                for(int m = 0; m < 4; m++)
                {
                    if(num2[n][m] != num[n][m])
                    {
                        IsMove = true;
                        break;
                    }
                }
                if(IsMove)
                    break;
            }
        }
        return IsMove;
    }
    else if(dir == 's' || dir == 'S')
    {
        for(int j = 0; j < 4; j++)
        {
            int ii = 3, temp = 5;
            for(int i = 3; i >= 0; i--)
            {
                if(num[i][j] == 0)
                    continue;
                else
                {
                    temp = i;
                    break;
                }
            }
            if(temp == 5)
                continue;
            num[ii][j] = num[temp][j];
            for(int i = temp - 1; i >= 0; i--)
            {
                if(num[i][j] == 0)
                    continue;
                else if(num[i][j] != 0 && num[i][j] == num[ii][j])
                {
                    num[ii][j] *= 2;
                    G.score += num[ii][j];
                    num[i][j] = 0;
                }
                else if(num[i][j] != 0 && num[i][j] != num[ii][j])
                    num[--ii][j] = num[i][j];
            }
            while(ii >= 0)
                num[--ii][j] = 0;
            for(int n = 0; n < 4; n++)
            {
                for(int m = 0; m < 4; m++)
                {
                    if(num2[n][m] != num[n][m])
                    {
                        IsMove = true;
                        break;
                    }
                }
                if(IsMove)
                    break;
            }
        }
        return IsMove;
    }
    else if(dir == 'a' || dir == 'A')
    {
        for(int i = 0; i < 4; i++)
        {
            int jj = 0, temp = 5;
            for(int j = 0; j < 4; j++)
            {
                if(num[i][j] == 0)
                    continue;
                else
                {
                    temp = j;
                    break;
                }
            }
            if(temp == 5)
                continue;
            num[i][jj] = num[i][temp];
            for(int j = temp + 1; j < 4; j++)
            {
                if(num[i][j] == 0)
                    continue;
                else if(num[i][j] != 0 && num[i][j] == num[i][jj])
                {

                    num[i][jj] *= 2;
                    G.score += num[i][jj];
                    num[i][j] = 0;
                }
                else if(num[i][j] != 0 && num[i][j] != num[i][jj])
                {

                    num[i][++jj] = num[i][j];
                }
            }
            while(jj < 4)
            {
                num[i][++jj] = 0;
            }
            for(int n = 0; n < 4; n++)
            {
                for(int m = 0; m < 4; m++)
                {
                    if(num2[n][m] != num[n][m])
                    {
                        IsMove = true;
                        break;
                    }
                }
                if(IsMove)
                    break;
            }
        }
        return IsMove;
    }
    else if(dir == 'd' || dir == 'D')
    {
        for(int i = 0; i < 4; i++)
        {
            int jj = 3, temp = 5;
            for(int j = 3; j >= 0; j--)
            {
                if(num[i][j] == 0)
                    continue;
                else
                {
                    temp = j;
                    break;
                }
            }
            if(temp == 5)
                continue;
            num[i][jj] = num[i][temp];
            for(int j = temp - 1; j >= 0 && jj > 0; j--)
            {
                if(num[i][j] == 0)
                    continue;
                else if(num[i][j] != 0 && num[i][j] == num[i][jj])
                {
                    num[i][jj] *= 2;
                    G.score += num[i][jj];
                    num[i][j] = 0;
                }
                else if(num[i][j] != 0 && num[i][j] != num[i][jj])
                    num[i][--jj] = num[i][j];
            }
            while(jj > 0)
                num[i][--jj] = 0;
            for(int n = 0; n < 4; n++)
            {
                for(int m = 0; m < 4; m++)
                {
                    if(num2[n][m] != num[n][m])
                    {
                        IsMove = true;
                        break;
                    }
                }
                if(IsMove)
                    break;
            }
        }
        return IsMove;
    }
    return false;
}

void Game :: init()
{
    srand((unsigned int) time(NULL));
    int x = rand() % 4;
    int y = rand() % 4;
    G.num[x][y] = 2;
    int xx = rand() % 4;
    int yy = rand() % 4;
    while(1)
    {
        if(xx != x && yy != y)
        {
            G.num[xx][yy] = 2;
            break;
        }
        xx = rand() % 4;
        yy = rand() % 4;
    }
}

void Game :: make()
{
    int count = 0;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            if(num[i][j])
                count ++;
        }
    }
    if(count == 16)
        C.end_game();
    int xx = rand() % 4;
    int yy = rand() % 4;
    while(1)
    {
        if(num[xx][yy] == 0)
        {
            num[xx][yy] = 2;
            break;
        }
        xx = rand() % 4;
        yy = rand() % 4;
    }
}

void Console :: start_game()
{
    G.init();
    window();
    show();
    char dir;
    while(true)
    {
        bool flag = false;
        dir = getch();
        if(dir == 27)
            end_game();
        if(dir == 'w' || dir == 'W' || dir == 'a' || dir == 'A' || dir == 's' || dir == 'S' || dir == 'D' || dir == 'd')
        {
            flag = G.control(dir);
            window();
            if(flag)
                G.make();
            show();
        }
    }
}

int main()
{
    C.enter_game();
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116044.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 点滴的积累—J2SE学习小结

    点滴的积累—J2SE学习小结

    2021年12月14日
  • 虚拟机搭建web服务器

    虚拟机搭建web服务器虚拟机搭建web服务器描述:把主机的项目部署到虚拟机Linux系统下,并且可以在主机上访问得到一、首先下载虚拟机链接:https://blog.csdn.net/felix__h/article/details/82853501二、下载Linux镜像文件链接:https://pan.baidu.com/s/12vA9yELUWV_nRNDHJaOmWw密码:a1yz三、打开下载好的…

  • tomcat java_tomcat和maven的区别

    tomcat java_tomcat和maven的区别复杂的sql查询环境搭建CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8INSERT INTO teacher(id, name) VALUES (1,’秦老师’); CREATE TABLE `student` ( `id` INT(10) NOT NULL

  • 易经六十四卦详解与应用_六十四卦推演方法

    易经六十四卦详解与应用_六十四卦推演方法文章目录六十四卦图卦象工具/原料方法/步骤注意事项六十四卦图卦象工具/原料一块钱的硬币 三枚方法/步骤第一,占卜前要把手洗干净,然后心里要虔诚,其他的事情不要胡思乱想,心里就想着自己要占卜问的事情。第二,取出三枚一元的硬币,有一元字体的为正面,花的那面为反面。然后将三枚硬币放在手中,双手合拢摇晃几下,最后开出来。结果的话有四种情况。三个全是正面的记作老阳——x,只有一个正面…

  • 国内最好用的dns地址_dns的服务器地址设置

    国内最好用的dns地址_dns的服务器地址设置对于DNS我想我们大部分人都会使用运营商自动推荐使用他们自己DNS服务器,使用它们的DNS服务器容易出现被劫持。所以今天我在这里推荐几个国内安全稳定的DNS服务器供大家使用……1、中国互联网络信息中心公共DNS服务器(CNNIC’sDNS)首选地址:1.2.4.8备用地址:210.2.4.82、百度公共DNS(BAIDU’SDNS)地址:180.76.76.763、阿里公共DNS服务器(Ali…

  • Django(44)drf序列化源码分析「建议收藏」

    Django(44)drf序列化源码分析「建议收藏」序列化与反序列化一般后端数据返回给前端的数据格式都是json格式,简单易懂,但是我们使用的语言本身并不是json格式,像我们使用的Python如果直接返回给前端,前端用的javascript语言是识

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号