我如何分配的颜色瓷砖像糖果美眉游戏 [英] How do i allocate colors to tiles in a game like candy crush

查看:172
本文介绍了我如何分配的颜色瓷砖像糖果美眉游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C / C上的部分游戏工作++所在的瓷砖交换得到的在这里
我想现在实施的糖果美眉,我知道绘制糖果会采取很多的努力。只要让瓷砖做的工作。
在指定颜色,瓷砖的部分我不想三种颜色出现在行或列。我如何以有效的方式做到这一点。
我是presently配发颜色随机这样的:

 板[I] [J]。颜色=兰特()%3;

看起来像这样:

是的,我不希望在一行或一列三个电池或瓦片具有相同的颜色,是相同的颜色的不超过两个相邻的瓷砖。
 我的意思是,我不想在这里颜色被分配一次的解决方案,如果三个瓷砖线具有相同的颜色进行检查。如果不是再次生成所有的瓷砖颜色。这将是太天真了,成本高。
另一种解决办法可以是在光栅顺序检查,看是否低于或向左两个区域具有相同的颜色,如果这样分配一些其它颜色分配颜色瓷砖之前。那也是显而易见的。有没有更好的办法?

继天气没落的答案我张贴code正在使用STL的迭代器和set_difference方法做差集操作。我试图从一组颜色我被消除只有那些正在使用一套操作已经重复两次(下或左)的颜色形成这组随机选择了。也有一些是错误的code。我没有STL的很多知识。任何人都可以请指导我如何正确使用它。

 为(J = 0; J< MAXY; J ++)
{为(i = 0; I< MAXX,我++)
{
    诠释第[] = {0,1,2,3},FSIZE,I34;
    的std ::矢量<&INT GT; V(5);
    的std ::矢量<&INT GT;:迭代它;    董事会由[i] [j]的.x0 = X + I * DX + 1;
    板由[i] [j]的.x1 = X0 +第(i + 1)* DX-1;
    董事会由[i] [j]的.y0 = Y0 + J * DY + 1;
    董事会由[i] [j]的.y1 = Y0 +(J + 1)* DY-1;
    如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 0&放大器;及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 0)
    {秒整型[] = {0};
    的std ::排序(第一,+ 4);
    它=性病:: set_difference(第一,+ 4,第二,第二+ 1,v.begin());
    v.resize(IT-v.begin());
    性病::复制(v.begin(),v.end(),第一次);
    }
    FSIZE = v.size();
    如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 1和;及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 1)
    {诠释第二[] = {1};
    的std ::排序(第一,+ FSIZE); // 5 10 15 20 25
    它=性病:: set_difference(第一,+ FSIZE,第二,第二+ 1,v.begin());
                                            // 5 15 25 0 0 0 0 0 0 0
    v.resize(IT-v.begin()); // 5 15 25
    性病::复制(v.begin(),v.end(),第一次);
    }
    FSIZE = v.size();
    如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 2及及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 2)
    {秒整型[] = {2};
    的std ::排序(第一,+ FSIZE); // 5 10 15 20 25
    它=性病:: set_difference(第一,+ FSIZE,第二,第二+ 1,v.begin());
                                       // 5 15 25 0 0 0 0 0 0 0
    v.resize(IT-v.begin()); // 5 15 25
    性病::复制(v.begin(),v.end(),第一次);
    }
    FSIZE = v.size();
    如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 0&放大器;及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 0)
    {秒整型[] = {0};
    的std ::排序(第一,+ FSIZE); // 5 10 15 20 25
    它=性病:: set_difference(第一,+ FSIZE,第二,第二+ 1,v.begin());
                                       // 5 15 25 0 0 0 0 0 0 0
    v.resize(IT-v.begin()); // 5 15 25
    性病::复制(v.begin(),v.end(),第一次);
    }
    FSIZE = v.size();
    如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 1和;及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 1)
    {诠释第二[] = {1};
    的std ::排序(第一,+ FSIZE); // 5 10 15 20 25
    它=性病:: set_difference(第一,+ FSIZE,第二,第二+ 1,v.begin());
                              // 5 15 25 0 0 0 0 0 0 0
    v.resize(IT-v.begin()); // 5 15 25
    性病::复制(v.begin(),v.end(),第一次);
    }
    FSIZE = v.size();
    如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 2及及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 2)
    {秒整型[] = {2};
    的std ::排序(第一,+ FSIZE); // 5 10 15 20 25
                                    // 10 20 30 40 50
    它=性病:: set_difference(第一,+ FSIZE,第二,第二+ 1,v.begin());
                                  // 5 15 25 0 0 0 0 0 0 0
    v.resize(IT-v.begin()); // 5 15 25
    性病::复制(v.begin(),v.end(),第一次);
    }
    //首先=安培; v [0];
    FSIZE = v.size();
    COUT&所述;&下; v.size()&所述;&下;;
    为(I34 = 0,它= v.begin();!它= v.end();它++)
    {COUT<< *它<<;第[I34 ++] = *它;}
    COUT&所述;&下;;
    如果(v.size()大于0&放大器;&放大器;!v.size()= 5)
    板[I] [J]。颜色=第[兰特()%I34]
    否则,如果(v.size()== 5)板[I] [J]。颜色=第[兰特()%4]。
    }
    COUT<<\\ n;
}


解决方案

我想什么来实现减设置操作,其中i preclude在争夺随机选择是那些颜色的重复两次向左或下文。

 为(i = 0; I< MAXX,我++)
        为(J = 0; J< MAXY; J ++)
        {exzero = exone = extwo = exthree = 0;
            董事会由[i] [j]的.x0 = X + I * DX + 1;
            板由[i] [j]的.x1 = X0 +第(i + 1)* DX-1;
            董事会由[i] [j]的.y0 = Y0 + J * DY + 1;
            董事会由[i] [j]的.y1 = Y0 +(J + 1)* DY-1;
            如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 0&放大器;及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 0)
                exzero = 1;
            如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 1和;及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 1)
                exone = 1;
            如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 2及及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 2)
                extwo = 1;
            如果((I-1)> = 0&放大器;&放大器;板[I-1] [j]的。颜色== 3及及(I-2)> = 0&放大器;&放大器;板〔I-2〕〔 J]。颜色== 3)
                            exthree = 1;
            如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 0&放大器;及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 0)
                exzero = 1;
            如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 1和;及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 1)
                exone = 1;
            如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 2及及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2]。颜色== 2)
                extwo = 1;
            如果((J-1)> = 0&放大器;&放大器;板由[i] [J-1]。颜色== 3及及(J-2)及GT; = 0&放大器;&放大器;板由[i] [J- 2] ==。颜色3)
                            exthree = 1;
            compositebin = exzero | exone<< 1 | extwo< 2 | exthree< 3;;
        // COUT&所述;&所述; compositebin&所述;&下;;
            开关(compositebin)
            {
            情况下0:板[I] [J]。颜色=兰特()%4;打破;
            案例1:板[I] [J]。颜色=兰特()%2 1:2;打破;?
            案例2:板[I] [J]。颜色=兰特()%2 0:2;打破;?
            案例3:董事会由[i] [j]的。颜色= 2;打破;
            案例4:电路板[I] [J]。颜色=兰特()%2 1:0;打破;?
            案例5:董事会由[i] [j]的。颜色= 1;打破;
            案例6:董事会由[i] [j]的。颜色= 0;打破;
            案例7:突破;
            案例8:董事会由[i] [j]的。颜色=兰特()%2兰特()%2 0:1:??2;打破;
            案例9:板[I] [J]。颜色=兰特()%2 1:2;打破;?
            案例10:板[I] [J]。颜色=兰特()%2 0:2;打破;?
            案例11:板[I] [J]。颜色= 2;打破;
            案例12:板[I] [J]。颜色=兰特()%2 1:0;打破;?
            案例13:董事会由[i] [j]的。颜色= 1;打破;
            案例14:板[I] [J]。颜色= 0;打破;
            案例15:打破;
            }        }

这件code的使用标志排除为零,排除一个,排除二和排除三排除四种颜色做了整洁的工作。我形成复合二进制数和交换机根据其值设置为随机抽取的一个子集选择{0,1,2,3}。

I have worked on a partial game in C/C++ where the tiles get exchanged Here. I want to now implement candy crush and i know drawing the candies will take lot of effort. Just let the tiles do the job. In the part on assigning colours to tiles i don't want three colours to appear in a row or column. How do i do this in an efficient way. I am presently allotting colours randomly like this:

board[i][j].color=rand()%3;

Which looks like this:

Yes I don't want three cells or tiles in a row or column to have same colour that is no more than two adjacent tiles of same colour. I mean i don't want a solution where colours are allotted once and a check made if three tiles in a line have same colours. If not generate colours for all the tiles again. That would be too naive and costly. Another solution may be before assigning a colour to a tile in raster order checking to see if two tiles below or to the left have same colour if so allot some other colour. That too is obvious. Is there a better way?

Subsequent to Weather Wane's answer I am posting code that is doing the set difference operation using STL iterators and set_difference method. What i am trying is before choosing randomly from a set of colors i am forming that set by eliminating only those colors which are already repeated twice(below or left) using set operation. There is something wrong in the code. I don't have much knowledge of STL. Can anyone please guide how i can use it properly.

for(j=0;j<maxy;j++)
{for(i=0;i<maxx;i++)
{
    int first[] = {0,1,2,3},fsize,i34;
    std::vector<int> v(5);                     
    std::vector<int>::iterator it;

    board[i][j].x0=x0+i*dx+1;
    board[i][j].x1=x0+(i+1)*dx-1;
    board[i][j].y0=y0+j*dy+1;
    board[i][j].y1=y0+(j+1)*dy-1;


    if((i-1)>=0&&board[i-1][j].color==0&&(i-2)>=0&&board[i-2][j].color==0)
    {int second[] = {0};
    std::sort (first,first+4);     
    it=std::set_difference (first, first+4, second, second+1, v.begin());
    v.resize(it-v.begin());                      
    std::copy(v.begin(), v.end(), first);
    }
    fsize=v.size();
    if((i-1)>=0&&board[i-1][j].color==1&&(i-2)>=0&&board[i-2][j].color==1)
    {int second[] = {1};
    std::sort (first,first+fsize);     //  5 10 15 20 25
    it=std::set_difference (first, first+fsize, second, second+1, v.begin());
                                            //  5 15 25  0  0  0  0  0  0  0
    v.resize(it-v.begin());                      //  5 15 25
    std::copy(v.begin(), v.end(), first);
    }
    fsize=v.size();
    if((i-1)>=0&&board[i-1][j].color==2&&(i-2)>=0&&board[i-2][j].color==2)
    {int second[] = {2};
    std::sort (first,first+fsize);     //  5 10 15 20 25
    it=std::set_difference (first, first+fsize, second, second+1, v.begin());
                                       //  5 15 25  0  0  0  0  0  0  0
    v.resize(it-v.begin());                      //  5 15 25
    std::copy(v.begin(), v.end(), first);
    }
    fsize=v.size();
    if((j-1)>=0&&board[i][j-1].color==0&&(j-2)>=0&&board[i][j-2].color==0)
    {int second[] = {0};
    std::sort (first,first+fsize);     //  5 10 15 20 25
    it=std::set_difference (first, first+fsize, second, second+1, v.begin());
                                       //  5 15 25  0  0  0  0  0  0  0
    v.resize(it-v.begin());                      //  5 15 25
    std::copy(v.begin(), v.end(), first);
    }
    fsize=v.size();
    if((j-1)>=0&&board[i][j-1].color==1&&(j-2)>=0&&board[i][j-2].color==1)
    {int second[] = {1};
    std::sort (first,first+fsize);     //  5 10 15 20 25
    it=std::set_difference (first, first+fsize, second, second+1, v.begin());
                              //  5 15 25  0  0  0  0  0  0  0
    v.resize(it-v.begin());                      //  5 15 25
    std::copy(v.begin(), v.end(), first);
    }
    fsize=v.size();
    if((j-1)>=0&&board[i][j-1].color==2&&(j-2)>=0&&board[i][j-2].color==2)
    {int second[] = {2};
    std::sort (first,first+fsize);     //  5 10 15 20 25
                                    // 10 20 30 40 50
    it=std::set_difference (first, first+fsize, second, second+1, v.begin());
                                  //  5 15 25  0  0  0  0  0  0  0
    v.resize(it-v.begin());                      //  5 15 25
    std::copy(v.begin(), v.end(), first);
    }
    //  first=&v[0];
    fsize=v.size();
    cout<<v.size()<<" ";
    for(i34=0,it=v.begin();it!=v.end();it++)
    {cout<<*it<<" "; first[i34++]=*it;}
    cout<<" ";
    if(v.size()>0&&v.size()!=5)
    board[i][j].color=first[rand()%i34];
    else if (v.size()==5) board[i][j].color=first[rand()%4];
    }
    cout<<"\n";
}

解决方案

What i wanted was to implement the minus set operation where i preclude those colours from being in contention for random selection which are repeated twice to the left or below.

for(i=0;i<maxx;i++)
        for(j=0;j<maxy;j++)
        {exzero=exone=extwo=exthree=0;
            board[i][j].x0=x0+i*dx+1;
            board[i][j].x1=x0+(i+1)*dx-1;
            board[i][j].y0=y0+j*dy+1;
            board[i][j].y1=y0+(j+1)*dy-1;
            if((i-1)>=0&&board[i-1][j].color==0&&(i-2)>=0&&board[i-2][j].color==0)
                exzero=1;
            if((i-1)>=0&&board[i-1][j].color==1&&(i-2)>=0&&board[i-2][j].color==1)
                exone=1;
            if((i-1)>=0&&board[i-1][j].color==2&&(i-2)>=0&&board[i-2][j].color==2)
                extwo=1;
            if((i-1)>=0&&board[i-1][j].color==3&&(i-2)>=0&&board[i-2][j].color==3)
                            exthree=1;
            if((j-1)>=0&&board[i][j-1].color==0&&(j-2)>=0&&board[i][j-2].color==0)
                exzero=1;
            if((j-1)>=0&&board[i][j-1].color==1&&(j-2)>=0&&board[i][j-2].color==1)
                exone=1;
            if((j-1)>=0&&board[i][j-1].color==2&&(j-2)>=0&&board[i][j-2].color==2)
                extwo=1;
            if((j-1)>=0&&board[i][j-1].color==3&&(j-2)>=0&&board[i][j-2].color==3)
                            exthree=1;
            compositebin=exzero|exone<<1|extwo<<2|exthree<<3;
        //  cout<<compositebin<<" ";
            switch(compositebin)
            {
            case 0:board[i][j].color=rand()%4;break;
            case 1:board[i][j].color=rand()%2?1:2;break;
            case 2:board[i][j].color=rand()%2?0:2;break;
            case 3:board[i][j].color=2;break;
            case 4:board[i][j].color=rand()%2?1:0;break;
            case 5:board[i][j].color=1;break;
            case 6:board[i][j].color=0;break;
            case 7:break;
            case 8:board[i][j].color=rand()%2?rand()%2?0:1:2;break;
            case 9:board[i][j].color=rand()%2?1:2;break;
            case 10:board[i][j].color=rand()%2?0:2;break;
            case 11:board[i][j].color=2;break;
            case 12:board[i][j].color=rand()%2?1:0;break;
            case 13:board[i][j].color=1;break;
            case 14:board[i][j].color=0;break;
            case 15:break;
            }

        }

This piece of code using flags exclude zero,exclude one, exclude two and exclude three to exclude the four colours does a tidy job. I form a composite binary number and switch according to its value to randomly select from a subset of {0,1,2,3}.

这篇关于我如何分配的颜色瓷砖像糖果美眉游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆