标题:自己写的2048小游戏的程序,为什么数字多的时候运行的很慢,要等一两秒才能 ...
取消只看楼主
ccm1952
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2016-11-7
 问题点数:0 回复次数:2 
自己写的2048小游戏的程序,为什么数字多的时候运行的很慢,要等一两秒才能变过来
程序代码:
#include<iostream>
#include<conio.h>
#include<math.h>
#include<Windows.h>
#include<time.h>
#include<iomanip>
using namespace std;
int pan[4][4];
void showpan()//显示棋盘
{
    system("cls");
    system("color f9");
    cout << setw(50) << "木木的2048" << endl;
    cout << setw(60) << "--------------------" << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << setw(39) << " " << "|";
        for (int j = 0; j < 4; j++)
        {
            if (pan[i][j] == 0)
                cout << setw(4) << " " << "|";
            else
            {
                cout << setw(4) << pan[i][j]<<"|" ;
            }
        }
        cout  << endl;
        cout << setw(60) << "--------------------" << endl;
        
    }
}
void newgame()//开始一个新游戏
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            pan[i][j] = 0;
    srand((unsigned)time(NULL));
    int m = rand() % 4;
    int n = rand() % 4;
    pan[m][n] = 2;
    showpan();

}
int moveup()//上移
{
        int flag = 0;
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0, k = 1; k < 4; k++)
            {
                if (pan[k][j] > 0)
                {
                    if (pan[i][j] == 0)
                    {
                        pan[i][j] = pan[k][j];
                        pan[k][j] = 0;
                    }
                    else
                    {
                        if (pan[i][j] == pan[k][j])
                        {
                            pan[i][j] += pan[k][j];
                            pan[k][j] = 0;
                        }

                        else
                        {
                            pan[i + 1][j] = pan[k][j];
                            if (k != i + 1)
                                pan[k][j] = 0;
                            i += 1;
                        }

                    }
                }
            }
        }
        return flag;
}
int movedown()//下移
{
    int flag = 0;
    for (int j = 0; j < 4;j++)
    {
        for (int i=3, k = 2; k>=0; k--)
            {
                if (pan[k][j] > 0)
                {
                    if (pan[i][j] == 0)
                    {
                        pan[i][j] = pan[k][j];
                        pan[k][j] = 0;
                    }
                    else
                        if (pan[i][j] == pan[k][j])
                        {
                            pan[i][j] += pan[k][j];
                            pan[k][j] = 0;
                        }
                        else
                        {
                            if (k != (i - 1))
                            {
                                pan[i - 1][j] = pan[k][j];
                                pan[k][j] = 0;
                            }
                            i--;
                        }
                            
                }
            }
    }return flag;
}
int moveleft()//左移
{
    int flag = 0;
    for (int i = 0; i< 4; i++)
    {
        
            for (int j=0,k =1; k <= 3; k++)
            {
                if (pan[i][k] > 0)
                {
                    if (pan[i][j] == 0)
                    {
                        pan[i][j] = pan[i][k];
                        pan[i][k] = 0;
                    }
                    else
                        if (pan[i][j] == pan[i][k])
                        {
                            pan[i][j] += pan[i][k];
                            pan[i][k] = 0;
                        }
                        else
                        {
                            if ((j + 1) != k)
                            {
                                pan[i][j + 1] = pan[i][k];
                                pan[i][k] = 0;
                            }j++;

                        }
                }
            

        }

    }return flag;
}
int moveright()//右移
{
    int flag = 0;
    for (int i = 0; i< 4; i++)
    {
        
            for (int k =2,j=3; k >= 0; k--)
            {
                if (pan[i][k] > 0)
                {
                    if (pan[i][j] == 0)
                    {
                        pan[i][j] = pan[i][k];
                        pan[i][k] = 0;
                    }
                    else
                        if (pan[i][j] == pan[i][k])
                        {
                            pan[i][j] += pan[i][k];
                            pan[i][k] = 0;
                        }
                        else
                        {
                            if (k != (j - 1))
                            {
                                pan[i][j - 1] = pan[i][k];
                                pan[i][k] = 0;
                            }j--;
                        }
                }
            }
    
    }return flag;
}

int panmax()//检测最大数
{
    int max = 0;
    for (int i = 0; i < 4;i++)
    {
        for (int j = 0; j < 4;j++)
        { if (max<pan[i][j])
        {
            max = pan[i][j];
        }
        }
    }
    return max;
}
int if2n(int x)
{
    int flag = 0;
    for (int n = 1; n < 12; n++)
    {
        if (x == pow(2, n))
            flag = 1;
    }return flag;
}

int ifwin()//检测是否游戏胜利
{
    int flag = 0;
    if (panmax() == 2048)
    {
        flag = 1;
        cout << setw(40) << "可以啊,小伙子,你赢了" << endl;
    }return flag;

}
int testup()//检测能否上移
{
    int flag = 0;
    for (int j = 0;j< 4;j++)
    {
        for (int i = 0; i <3; i++ )
        {
            
            
                if (if2n(pan[i][j] + pan[i + 1][j])&& (pan[i+1][j]))
                    flag = 1;
            
        }
    }return flag;
}
int testdown()//检测能否下移
{
    int flag = 0;
    for (int j = 0; j < 4; j++)
    {
        for (int i = 3; i > 0; i--)
        {
        

            if (if2n(pan[i][j] + pan[i - 1][j] )&&pan[i-1][j] != 0)
                flag = 1;
        
        }
        
    }return flag;
}
int testleft()//检测能否左移
{
    int flag = 0;
    for (int i = 0; i< 4; i++)
    {
        for (int j = 0;j < 3; j++)
        {
            if ((if2n(pan[i][j] + pan[i][j+1])==1 )&&(pan[i][j+1]!= 0))
                    flag = 1;
        }
    }return flag;
}
int testright()//检测能否右移
{
    int flag = 0;
    for (int i = 0; i < 4; i++)
    {
        for (int j = 3 ; j >0; j--)

            //if (pan[k][i] > 0)
        {
            if (if2n(pan[i][j] +pan[i][j-1])&& pan[i][j-1] != 0)
                flag = 1;
            ;
        }
        
    }return flag;
}

int gameover()//判断游戏是否结束
{
    int flag = 0;
    if ((testup() + testdown() + testleft() + testright()) == 0)
    {
        flag = 1;
        cout << setw(30) << "不行啊,小伙子,继续努力" << endl;
    }
    return flag;
}

void add()
{
    int flag = 1;
    while (flag)
    {
        srand((unsigned)time(NULL));
        
        int m = rand() % 4;
        int n = rand() % 4;
        if (pan[m][n] == 0)
        {
            pan[m][n] = 2 ;
            flag = 0;

        }
    }
}
        
int GetDirection() //读取方向
{
    int ret = 0;
    do
    {
        int ch= _getch();
        
        if (isascii(ch))
            continue;
        ch = _getch();
        switch (ch)
        {
        case 72:
            ret = 2; // top
            break;
        case 75:
            ret = 1; // left 
            break;
        case 77:
            ret = 3; // right
            break;
        case 80:
            ret = 4; // down
            break;
        default:
            break;
        }
    } while (ret == 0);
    return ret;
}


int main()
{
    int  makesure = 1;
    while (makesure)
    {
        system("cls");
        system("color f9");
        newgame();
        while ((ifwin() + gameover())==0)
        {
            int c = GetDirection();
            switch (c)
            {
            case 2:
                if (testup() == 1)
                {
                    moveup();
                    add();
                    system("cls");
                    showpan();
                }
                break;
            case 4:
                if (testdown() == 1)
                {
                    movedown();
                    add();
                    system("cls");
                    showpan();

                }

                break;
            case 1:
                if (testleft() == 1)
                {
                    moveleft();
                    add();
                    system("cls");
                    showpan();
                }
                break;
            case 3:
                if (testright() == 1)
                {
                    moveright();
                    add();
                    system("cls");
                    showpan();
                }
                break;
            default:
                break;
            }

        }
        int max = panmax();
        cout << "小伙子,你才拿了" << max << "" << endl;
        cout << "如果要再来一盘就输入1,不玩了就输入0" << endl;
        cout    << "来:" << endl;
        cin >> makesure;
        if (makesure != 1 && makesure != 0)
        {
            cout << "小伙子,别瞎写,重来:" << endl;
            cin >> makesure;
        }
    }
}
搜索更多相关主题的帖子: 小游戏 
2016-11-07 14:09
ccm1952
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2016-11-7
得分:0 
思路。就是用变量x=1来while循环,同时等待操作键的输入,有输入就调用相应的处理函数,同时检测是否胜利或者失败。
如果游戏结束,让x=0.终止while循环。
相应的处理函数只有不多的for循环,也没有设置延时,但就是数字一多了,调用函数的时间就会变长。
时间好像会长达一到两秒。。。很奇怪啊,求大神解释下
2016-11-08 22:12
ccm1952
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2016-11-7
得分:0 
因为函数是Int,要返回一个值。本来想的是用返回值来判断是不是函数调用成功。
后来想了下无所谓,就没改。
2016-11-09 13:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470828-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.185115 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved