不过,工作上不用,终究是大块的时间不用。三天不练就手生了,这快一年没怎么用,廉颇老否?

  今天加班,主要是跑测试脚本,那跑起来一个慢,等的人无聊。 

  无聊……就找点小游戏玩吧?在Google Desktop上面有一个“Color Junction”的游戏,应该就是消色块,有连到一起的两个或者两个以上相同颜色色块就可以消掉,尽量在最后消除掉所有的色块。点了几下,一样无聊,也觉得,玩游戏是不是有点心虚的感觉?

  既然如此,这个游戏也不难写,不如写一个吧,哈哈哈,正好练习一下生疏已久的C++。

  想法如下:

  第一、能够给人玩的内部逻辑实现出来,界面就算了;

  第二、写一个函数代替人来玩这个游戏;

  第三、以后再写其他函数,逐步改进,尽量能把所有的方块都消除干净。

  要是写一个给人玩的,那写完就没意思了不是?干脆写一个让电脑玩的,这样写了一个还可以写一个其他风格的不是?

  趁着跑脚本的时间,居然很快写出来了,真有宝刀不老之感。哈哈,采用的是标准C++,任何一种支持C++标准的编译器都可以编译出来。就是输出的挺难看,玩游戏的函数用的是最简单的思路,找到一个能消除的马上消除,挺弱智的。仓促写就,有点啦擦,但是结构还是不错的,以后在加班,就在改进吧。代码如下:

#include <iostream>

#include <vector>


const int WID_X = 10;
const int WID_Y = 10;

typedef std::vector< std::vector<int> > CSqure;
typedef int (*CHooker)(CSqure&, int, int); 

int CreateSqure(CSqure& squre, int wid_x, int wid_y);
bool HasSqure(CSqure& squre);
void HandleSqure(const CSqure& squre, int& x, int& y);
int ChangeSqure(CSqure& squre, int x, int y);
int spreadDetect(CSqure& squre, int x, int y, CHooker pHokker = NULL);
int changer(CSqure& squre, int x, int y);
int PrintSqure(const CSqure& squre, int x = -1, int y = -1);

int main()
{
       CSqure squre;
       int err = 0;
       int left = 0;
       err = CreateSqure(squre, WID_X, WID_Y);
       left = PrintSqure(squre);
       int step = 0;
       while(HasSqure(squre))
       {
             int x, y;
             HandleSqure(squre, x, y);
             std::cout << x << "," << y << "\n";
             if (ChangeSqure(squre, x, y) < 0)
             {
                   std::cout << "Fail\n";
                   return 0;
             }
             left = PrintSqure(squre, x, y);
             ++step;
       }
       std::cout << step << "\t" << left << "\n";
}
int CreateSqure(CSqure& squre, int wid_x, int wid_y)
{
       int i, j;
       squre.resize(wid_x);
       for(i = 0; i < wid_x; ++i)
       {
             squre[i].resize(wid_y);
             for(j = 0; j < wid_y; ++j)
                   squre[i][j] = rand() % 4;
       }
       return 0;
}
bool HasSqure(CSqure& squre)
{
       int i, j;
       for(i = 0; i < squre.size(); ++i)
       {
             for(j = 0; j < squre[i].size(); ++j)
             {
                   if (squre[i][j] == -1)
                         continue;
                   if (spreadDetect(squre, i, j) > 0)
                         return true;
             }
       }
       return false;
}
void HandleSqure(const CSqure& squre, int& x, int& y)
{
       int i, j;
       for(i = 0; i < squre.size(); ++i)
       {
             for(j = 0; j < squre[i].size(); ++j)
             {
                   if (squre[i][j] == -1)
                         continue;
                   int counter = 0;
                   if ((i > 0) && (squre[i][j] == squre[i - 1][j]))
                         ++counter;
                   if ((j > 0) && (squre[i][j] == squre[i][j - 1]))
                         ++counter;
                   if ((i < squre.size() - 1) && (squre[i][j] == squre[i + 1][j]))
                         ++counter;
                   if ((j < squre[0].size() - 1) && (squre[i][j] == squre[i][j + 1]))
                         ++counter;
                   if (counter > 0)
                   {
                         x = i;
                         y = j;
                         return;
                   }
                   else
                   {
                         x = -1;
                         y = -1;
                   }
             }
       }
       return;
}

[原文在百度空间已经关闭]