From 489cc1221f245480eedd19595c1baa930d1978d5 Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Sun, 6 Apr 2014 13:24:44 -0700 Subject: [PATCH] Bump 2048 to newest remote master --- userspace/extra/2048/2048.c | 255 +++++++++++++++++++++++++----------- 1 file changed, 176 insertions(+), 79 deletions(-) diff --git a/userspace/extra/2048/2048.c b/userspace/extra/2048/2048.c index 43c4b562..ee83b339 100644 --- a/userspace/extra/2048/2048.c +++ b/userspace/extra/2048/2048.c @@ -15,76 +15,109 @@ #include #include #include -#include +#include #define SIZE 4 +uint32_t score=0; void getColor(uint16_t value, char *color, size_t length) { - uint16_t c = 40; - if (value > 0) while (value >>= 1) c++; - snprintf(color,length,"\033[0;41;%dm",c); + 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 *scheme = original; + uint8_t *background = scheme+0; + uint8_t *foreground = scheme+1; + if (value > 0) while (value >>= 1) { + if (background+20 && array[len-1]==0) { - len--; +int8_t findTarget(uint16_t array[SIZE],int8_t x,int8_t stop) { + int8_t t; + // if the position is already on the first, don't evaluate + if (x==0) { + return x; } - return len; + for(t=x-1;t>=0;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 shiftArray(uint16_t array[SIZE],int8_t start,int8_t length) { +bool slideArray(uint16_t array[SIZE]) { bool success = false; - int8_t x,i; - for (x=start;x0) return false; - if (findPairDown(board)) return false; + if (findPairDown(board)) return false; rotateBoard(board); - if (findPairDown(board)) ended = false; - rotateBoard(board); - rotateBoard(board); - rotateBoard(board); - return ended; + if (findPairDown(board)) ended = false; + rotateBoard(board); + rotateBoard(board); + rotateBoard(board); + return ended; } void addRandom(uint16_t board[SIZE][SIZE]) { @@ -229,7 +239,7 @@ void addRandom(uint16_t board[SIZE][SIZE]) { r = rand()%len; x = list[r][0]; y = list[r][1]; - n = (rand()%2+1)*2; + n = ((rand()%10)/9+1)*2; board[x][y]=n; } } @@ -258,24 +268,104 @@ void setBufferedInput(bool enable) { } } -int main(void) { +int test() { + uint16_t array[SIZE]; + uint16_t data[] = { + 0,0,0,2, 2,0,0,0, + 0,0,2,2, 4,0,0,0, + 0,2,0,2, 4,0,0,0, + 2,0,0,2, 4,0,0,0, + 2,0,2,0, 4,0,0,0, + 2,2,2,0, 4,2,0,0, + 2,0,2,2, 4,2,0,0, + 2,2,0,2, 4,2,0,0, + 2,2,2,2, 4,4,0,0, + 4,4,2,2, 8,4,0,0, + 2,2,4,4, 4,8,0,0, + 8,0,2,2, 8,4,0,0, + 4,0,2,2, 4,4,0,0 + }; + uint16_t *in,*out; + uint16_t t,tests; + uint8_t i; + bool success = true; + + tests = (sizeof(data)/sizeof(data[0]))/(2*SIZE); + for (t=0;t "); + for (i=0;i "); + for (i=0;i