首页 > 编程语言 >c++编程,对2048小游戏的扩充

c++编程,对2048小游戏的扩充

时间:2023-03-09 19:00:16浏览次数:43  
标签:square int c++ 小游戏 2048 printf 4d include

原游戏网址:(210条消息) C++小游戏(原创)——2048_c++小游戏下载_Ziwen.紫闻的博客-CSDN博客

源代码:

 
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
using namespace std;
int button=0,maxn=0;//游戏开始
int square[6][6];//游戏格子
int aa[6],bb[6];
int score=0,sco=0;
void outt_little(){
    int i,a=0,b=0,c=0,d=0;
    printf("╔════╦════╦════╦════╗\n");
        {i=1;a=square[i][1]; b=square[i][2]; c=square[i][3]; d=square[i][4];}
    printf("║%4d║%4d║%4d║%4d║\n",a,b,c,d);
    printf("╠════╬════╬════╬════╣\n");
        {i=2;a=square[i][1]; b=square[i][2]; c=square[i][3]; d=square[i][4];}
    printf("║%4d║%4d║%4d║%4d║\n",a,b,c,d);
    printf("╠════╬════╬════╬════╣\n");    
        {i=3;a=square[i][1]; b=square[i][2]; c=square[i][3]; d=square[i][4];}
    printf("║%4d║%4d║%4d║%4d║\n",a,b,c,d);
    printf("╠════╬════╬════╬════╣\n");
        {i=4;a=square[i][1]; b=square[i][2]; c=square[i][3]; d=square[i][4];}
    printf("║%4d║%4d║%4d║%4d║\n",a,b,c,d);
    printf("╚════╩════╩════╩════╝\n");
}
 
void outt_mid(){
    int i,j,a,b,c,d;
    char str[10];
    sprintf(str,"%d",a);        
    printf("╔══════╦══════╦══════╦══════╗\n");
    printf("║      ║      ║      ║      ║\n");
    j=1;
    for(int i=1;i<=3;i++)    
        if(square[j][i]==0)  printf("║      "); 
                        else printf("║%5d ",square[j][i]);
    if(square[j][4]==0) printf("║      ║\n");
                        else printf("║%5d ║\n",square[j][4]);
    printf("╠══════╬══════╬══════╬══════╣\n");
    printf("║      ║      ║      ║      ║\n");
    j=2;
    for(int i=1;i<=3;i++)    
        if(square[j][i]==0)  printf("║      "); 
                        else printf("║%5d ",square[j][i]);
    if(square[j][4]==0) printf("║      ║\n");
                        else printf("║%5d ║\n",square[j][4]);
    printf("╠══════╬══════╬══════╬══════╣\n");
    printf("║      ║      ║      ║      ║\n");
    j=3;
    for(int i=1;i<=3;i++)    
        if(square[j][i]==0)  printf("║      "); 
                        else printf("║%5d ",square[j][i]);
    if(square[j][4]==0) printf("║      ║\n");
                        else printf("║%5d ║\n",square[j][4]);
    printf("╠══════╬══════╬══════╬══════╣\n");
    printf("║      ║      ║      ║      ║\n");
    j=4;
    for(int i=1;i<=3;i++)    
        if(square[j][i]==0)  printf("║      "); 
                        else printf("║%5d ",square[j][i]);
    if(square[j][4]==0) printf("║      ║\n");
                        else printf("║%5d ║\n",square[j][4]);
    printf("╚══════╩══════╩══════╩══════╝\n");
 
}
 
void intt(){
    int n,m,numb;
    memset(square,0,sizeof(square));
    srand(time(NULL));
    n = rand() % 3 + 1;
    m = rand() % 3 + 1;
    while(square[n][m]!=0)
    {
        n = rand() % 4 + 1;
        m = rand() % 4 + 1;
    }
    numb=rand() % 2 + 1;
    button++;
    square[n][m]=numb;
}
 
void add_num(){
    int n,m,numb;
    n = rand() % 3 + 1;
    m = rand() % 3 + 1;
    while(square[n][m]!=0)
    {
        n = rand() % 4 + 1;
        m = rand() % 4 + 1;
    }
    numb=rand() % 2 + 1;
    square[n][m]=numb;
    button++;
}
char check(){
    int ch;
    while( (ch=getch())!=0x1B ) /* Press ESC to quit... */
    {
      switch(ch)
      {
      case 0xE0:
         switch(ch=getch())
         {
            case 72:  return('u');break;
            case 80:  return('d');break;
            case 75:  return('l');break;
            case 77:  return('r');break;
            default:
               break;
         }
         break;
      default:
         break;
      }
   }
}
 
void run(char x){
    int count1,count;//sco分数; 
    sco=0;
    memset(aa,0,sizeof(aa));
    memset(bb,0,sizeof(bb));
    if(x=='l'||x=='r')
    for(int i=1;i<=4;i++)
    {
        count1=0;
        for(int j=1;j<=4;j++) 
            if (square[i][j]!=0) 
            {
                count1++;
                aa[count1]=square[i][j];
            }
            
        int l=1;
        int r=1;
        count=0;
        while(l<=count1)
        {
            if (aa[r]==aa[r+1]) r++;
            count++;
            bb[count]=(r-l+1)*aa[l];
            maxn=max(maxn,bb[count]);
            if(r>l) sco=sco+bb[count];
            l=r+1;
            r++;
        }
        for(int j=1;j<=4;j++)
            square[i][j]=0;
        if (x=='l') 
            for(int j=1;j<=count;j++)
                square[i][j]=bb[j];
        if (x=='r') 
            for(int j=1;j<=count;j++)
                square[i][4-j+1]=bb[count-j+1];    
        button=button-count1+count;            
    }
    if(x=='u'||x=='d')
    for(int i=1;i<=4;i++)
    {
        count1=0;
        for(int j=1;j<=4;j++) 
            if (square[j][i]!=0) 
            {
                count1++;
                aa[count1]=square[j][i];
            }
            
        int l=1;
        int r=1;
        count=0;
        while(l<=count1)
        {
            if (aa[r]==aa[r+1]) r++;
            count++;
            bb[count]=(r-l+1)*aa[l];
            maxn=max(maxn,bb[count]);
            if(r>l) sco+=bb[count];
            l=r+1;
            r++;
        }
        for(int j=1;j<=4;j++)
            square[j][i]=0;
        if (x=='u') 
            for(int j=1;j<=count;j++)
                square[j][i]=bb[j];
        if (x=='d') 
            for(int j=1;j<=count;j++)
                square[4-j+1][i]=bb[count-j+1];    
        button=button-count1+count;        
    }
    score=sco+score;
    
} 
 
bool check_fail(){
    for(int i=1;i<=4;i++)
        for(int j=1;j<=3;j++)
            if (square[i][j]==square[i][j+1]) return true;
    for(int i=1;i<=4;i++)
        for(int j=1;j<=3;j++)
            if (square[j][i]==square[j+1][i]) return true;
    return(false);
}
 
int main(){
    system("mode con cols=30 lines=16");
    system("COLOR 73");
    intt();
    while (button<=16)
    {
        if (button==16)
            if(!(check_fail())) break;
        outt_mid();//输出 
        cout<<"max = "<<maxn<<endl;
        cout<<"score = "<<score<<endl;
        if(maxn>=2048) cout<<"Congratulations! You are win!"<<endl;
        char ch=check();//读入方向键 
        
        switch(ch){
            case 'l':run('l');break;
            case 'r':run('r');break;
            case 'd':run('d');break;
            case 'u':run('u');break;
        }
        if(button<16) add_num();
        system("cls");//清除
    }
    
    if (button==16) {
        outt_mid();
        cout<<"Sorry!You are failed."<<endl;
    }
    while(1);
    return 0;
}

 

 

 

 

鉴于本学生太过于菜了始终没有玩到2048 只能截到失败的页面

这种小游戏规则简单并不能在做着原本程序上再做拓展。所以我只在原有基础上进行了难度的选择和规则的说明

int main(){
    system("mode con cols=40 lines=20");
    system("COLOR 73");
    int n,m=1;
    cout<<"游戏规则:游戏规则很简单,每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。系统给予的数字方块不是2就是4,玩家要想办法在这小小的16格范围中凑出“2048”这个数字方块。"<<endl; 
        cout<<"请选择游戏难度:2048 1024 512 256"<<endl;
        int x;
        cin>>x;
        intt();
        while (button<=16)
        {
            if (button==16)
                if(!(check_fail())) break;
            outt_mid();//输出 
            cout<<"max = "<<maxn<<endl;
            cout<<"score = "<<score<<endl;
            if(maxn>=x) cout<<"Congratulations! You are win!"<<endl;
            char ch=check();//读入方向键 
            
            switch(ch){
                case 'l':run('l');break;
                case 'r':run('r');break;
                case 'd':run('d');break;
                case 'u':run('u');break;
            }
            if(button<16) add_num();
            system("cls");//清除
        }
        
        if (button==16) {
            outt_mid();
            cout<<"Sorry!You are failed."<<endl;
        }
    
    while(1);
    return 0;
}

 

 

 

 

标签:square,int,c++,小游戏,2048,printf,4d,include
From: https://www.cnblogs.com/a-b714610/p/17201073.html

相关文章

  • c++ 代码技巧
    数学运算性能大多数数据运算不存在性能问题,但是相对来说,整型的除法运算还是比较昂贵的。参考下面的例子:uint32_tBM_S1(uint64_tv){uint32_tresult=0;do{......
  • C++常用遍历算法 for_each
    #include<iostream>#include<vector>#include<functional>#include<algorithm>usingnamespacestd;//遍历函数for_each//普通函数voidprint1(intval){......
  • c++之虚基类
    1.虚基类在多继承关系中,如果一个派生类的从两个父类那里继承过来,并且这两个父类又恰恰是从一个基类那里继承而来。那这样就麻烦了,因为你可能继承了两份一样的成员!这不仅多......
  • 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 02.AI自定义任务和观察器中断
    斯坦福课程UE4C++ActionRoguelike游戏实例教程0.绪论概述本文章对应课程第十一章42节。这篇文章会进一步地为AI添加新功能,创建自定义任务,允许AI发射子弹,并且讲解观......
  • C++笔记--函数、预处理
    1函数1.1函数的介绍1.1.1函数的概述函数是c语言的功能单位。实现一个功能可以封装一个函数来实现。定义函数的时候一切以功能为目的,根据功能去定函数的参数和返回值......
  • c++11区域锁
    unique_lock方法说明详细说明unique_lock()noexcept;默认构造函数默认构造函数新创建的unique_lock对象不管理任何Mutex对象explicitunique_lock(mut......
  • C++ 三路快排 模板
    前言:今天被大作业的快速排序折磨的焦头烂额,原C++sort选手发现简洁的快排竟然如此难写(边界要注意的点好多qwq)。我原先的快排长这样:题解P1177【【模板】快速排序】......
  • c++常见的几种锁
    std::mutex(C++11),普通互斥锁,可以阻塞式等锁(lock())也可以非阻塞式上锁(try_lock())std::timed_mutex(C++11),互斥锁的加时版本,如果在一段时间内(try_lock_for())或是在某个时间......
  • 【C++】网上购书平台完善
    问题描述收到了一份室友已经完成的网上购书平台程序,对其功能使用和熟悉后,进行了部分功能的添加,使其更加完善。程序概况书店登录界面  部分功能展示    ......
  • c++移动构造函数
    一.介绍1.1 定义【源对象资源的控制权全部移交给目标对象】有些复制构造是必要的,我们确实需要另外一个副本;而有些复制构造是不必要的,我们可能只是希望这个对象换个地方,......