#include #include #include #include #include #include #include #include #include #include #include #include class GameWindow :public Window { protected: USHORT score; USHORT scheme; USHORT SIZE; std::vector >board; TextView*tfscore; public: GameWindow(int x,int y,int w,int h):Window(x,y,w,h) { tfscore=new TextView("0",200,40); tfscore->setTextSize(30); addView(tfscore).setPos(800,50); } void getColor(uint8_t value, char *color, size_t length); void initBoard(uint16_t size); void drawBoard(Canvas&canvas); uint8_t findTarget(std::vector array,uint8_t x,uint8_t stop); bool slideArray(std::vector& array); void rotateBoard(); bool moveUp(); bool moveDown(); bool moveLeft(); bool moveRight(); bool findPairDown(); uint8_t countEmpty(); void addRandom(); bool gameEnded(); void setScore(int s); virtual bool onKeyUp(int ,KeyEvent&k)override; virtual void onDraw(Canvas&canvas)override { Window::onDraw(canvas); canvas.reset_clip(); drawBoard(canvas); } }; void GameWindow::getColor(uint8_t value, char *color, size_t length) { uint8_t original[] = {8,255,1,255,2,255,3,255,4,255,5,255,6,255,7,255,9,0,10,0,11,0,12,0,13,0,14,0,255,0,255,0}; uint8_t blackwhite[] = {232,255,234,255,236,255,238,255,240,255,242,255,244,255,246,0,248,0,249,0,250,0,251,0,252,0,253,0,254,0,255,0}; uint8_t bluered[] = {235,255,63,255,57,255,93,255,129,255,165,255,201,255,200,255,199,255,198,255,197,255,196,255,196,255,196,255,196,255,196,255}; uint8_t *schemes[] = {original,blackwhite,bluered}; uint8_t *background = schemes[scheme]+0; uint8_t *foreground = schemes[scheme]+1; if (value > 0) while (value--) { if (background+2 array,uint8_t x,uint8_t stop) { uint8_t t; if (x==0) {// if the position is already on the first, don't evaluate return x; } for(t=x-1;; t--) { if (array[t]!=0) { if (array[t]!=array[x]) {// merge is not possible, take next position return t+1; } return t; } else { // we should not slide further, return this one if (t==stop) { return t; } } } // we did not find a return x; } bool GameWindow::slideArray(std::vector& array) { bool success = false; uint32_t x,t,stop=0; for (x=0; x0) return false; if (findPairDown()) return false; rotateBoard(); if (findPairDown()) ended = false; rotateBoard(); rotateBoard(); rotateBoard(); return ended; } void GameWindow::addRandom() { static bool initialized = false; uint8_t x,y; uint8_t r,len=0; uint8_t n,list[SIZE*SIZE][2]; if (!initialized) { srand(time(NULL)); initialized = true; } for (x=0; x0) { r = rand()%len; x = list[r][0]; y = list[r][1]; n = (rand()%10)/9+1; board[x][y]=n; } invalidate(NULL); } void GameWindow::setScore(int s) { char buf[64]; sprintf(buf,"SCORE:%d",s); score=s; tfscore->setText(buf); } void GameWindow::initBoard(uint16_t size) { uint16_t x,y; board.resize(size); for (x=0; xinitBoard(4); return app.exec(); }