在Github上看到一个荷兰人写的linux控制台版的2048,用的C语言。感觉非常有意思。
原网址在这里。
读了一下他的源代码,感觉写的不错。就厚着脸皮加了一些中文凝视,源代码例如以下:
/*
============================================================================
Name : 2048.c
Author : Maurits van der Schee
Description : Console version of the game "2048" for GNU/Linux
============================================================================
*
Note by Zhengmingpei,China
Time:2014.10.13
Contact:http://Zhengmingpei.github.com
Email:yueyawanbian@gmail.com
*/
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <signal.h>
#define SIZE 4
uint32_t score=0;
uint8_t scheme=0;
// 依据value获取相应的颜色,将包括设置终端颜色的字符串复制给color
void getColor(uint16_t value, char *color, size_t length) {
// 声明三个颜色数组。用一维数组,但每一个奇数位和偶数位组成一个前后景色
// 后两个数组分别相应程序的启动选项"blackwhite","bluered"
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 >>= 1)
// value不断右移一位。直到值变为0。实现每一个二进制一个不同的颜色
{
if (background+2<schemes[scheme]+sizeof(original)) {
background+=2;
foreground+=2;
}
}
//linux下终端及字体颜色设置语句的字符串
snprintf(color,length,"