From 2e7ec71c5c38ed59d38d204c6c3dd92bb9a1d84e Mon Sep 17 00:00:00 2001 From: Letter Date: Sat, 31 Jul 2021 13:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E6=B8=B8=E6=88=8Fdemo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/x86-gcc/CmakeLists.txt | 3 + extensions/game/2048/2048.c | 425 ++++++++++++++++++++++++++++++++++++ extensions/game/game.c | 21 ++ extensions/game/readme.md | 14 ++ 4 files changed, 463 insertions(+) create mode 100644 extensions/game/2048/2048.c create mode 100644 extensions/game/game.c create mode 100644 extensions/game/readme.md diff --git a/demo/x86-gcc/CmakeLists.txt b/demo/x86-gcc/CmakeLists.txt index bd40aee..1680c1a 100644 --- a/demo/x86-gcc/CmakeLists.txt +++ b/demo/x86-gcc/CmakeLists.txt @@ -11,6 +11,9 @@ add_executable(LetterShell ../../extensions/fs_support/shell_fs.c ../../extensions/log/log.c ../../extensions/shell_enhance/shell_passthrough.c + ../../extensions/shell_enhance/shell_cmd_group.c + ../../extensions/game/game.c + ../../extensions/game/2048/2048.c ) target_include_directories(LetterShell PUBLIC diff --git a/extensions/game/2048/2048.c b/extensions/game/2048/2048.c new file mode 100644 index 0000000..fedf291 --- /dev/null +++ b/extensions/game/2048/2048.c @@ -0,0 +1,425 @@ +/* + ============================================================================ + Name : 2048.c + Author : Maurits van der Schee + Description : Console version of the game "2048" for GNU/Linux + ============================================================================ + */ + +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include "shell.h" + +#define printf(...) shellPrint(shell2048, ##__VA_ARGS__) +#define getchar() shellGetChar(shell2048) + +#define SIZE 4 +uint32_t score=0; +uint8_t scheme=0; + +Shell *shell2048 = NULL; + +char shellGetChar(Shell *shell) { + char data; + if (shell->read(&data, 1) == 1) { + return data; + } else { + return -1; + } +} + +void sleep(int ms) { + int time = SHELL_GET_TICK(); + while (SHELL_GET_TICK() - time < ms) ; +} + +void 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+20) return false; + if (findPairDown(board)) return false; + rotateBoard(board); + if (findPairDown(board)) ended = false; + rotateBoard(board); + rotateBoard(board); + rotateBoard(board); + return ended; +} + +void addRandom(uint8_t board[SIZE][SIZE]) { + static bool initialized = false; + uint8_t x,y; + uint8_t r,len=0; + uint8_t n,list[SIZE*SIZE][2]; + + if (!initialized) { + srand(SHELL_GET_TICK()); + 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; + } +} + +void initBoard(uint8_t board[SIZE][SIZE]) { + uint8_t x,y; + for (x=0;x "); +// for (i=0;i "); +// for (i=0;i