diff --git a/commands/2048.c b/commands/2048.c new file mode 100644 index 0000000..5d6c766 --- /dev/null +++ b/commands/2048.c @@ -0,0 +1,389 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014 Maurits van der Schee + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * + * ============================================================================ + * Name : 2048.c + * Author : Maurits van der Schee + * Description : Console version of the game "2048" for GNU/Linux + * ============================================================================ + */ + +#include +#include +#include +#include + +#define SIZE 4 +static uint32_t score; + +static void getColor(uint16_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 *scheme = original; + uint8_t *background = scheme+0; + uint8_t *foreground = scheme+1; + if (value > 0) while (value >>= 1) { + if (background+2=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; +} + +static bool slideArray(uint16_t array[SIZE]) +{ + bool success = false; + int8_t x,t,stop=0; + + for (x=0;x0) return false; + if (findPairDown(board)) return false; + rotateBoard(board); + if (findPairDown(board)) ended = false; + rotateBoard(board); + rotateBoard(board); + rotateBoard(board); + return ended; +} + +static void addRandom(uint16_t board[SIZE][SIZE]) +{ + int8_t x,y; + int16_t r,len=0; + uint16_t n,list[SIZE*SIZE][2]; + + for (x=0;x0) { + r = rand()%len; + x = list[r][0]; + y = list[r][1]; + n = ((rand()%10)/9+1)*2; + board[x][y]=n; + } +} + +static int test(void) +{ + 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