eleven/eleven.term.c

102 lines
2.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <termios.h>
#include <unistd.h>
#include "eleven.h"
const char* tochar(int val){
// int i = 'A'-2;
const char* chars[] = //{"①","②","③","④","⑤","⑥","⑦","⑧","⑨","⑩","⑪","⑫","⑬","⑭","⑮","⑯"} ;
{" ","","","","","","","","","","","","","","",""};//,"⓰","⓱","⓲","⓳","⓴"
return chars[val & 0xF];
}
void format_cell_2(char* buf,U8 val){
sprintf(buf,"%s", tochar(val));
}
void format_cell_256(char* buf,U8 val){
// const U8 col[9]={196,1,161,126,91,62,27,27,27};
//const U8 col[9]={196,207,171,135,99,63,27,27,27};
//const U8 col[9]={202,219,183,147,111,75,39,39,39};
const U8 col[3]={33,141,196};
U32 icol = val>>4;
sprintf(buf,"\033[38;5;%dm%s", col[icol],tochar(val));
}
void print(U8* tiles){
int i,j;
char buf[32];
// printf("╭────────╮\n");
// printf("\033[38;5;249m --------\n");
for (j = SIZE - 1; j >= 0; --j) {
printf(" ");
for (i = 0; i < SIZE; ++i) {
format_cell_256(buf,tiles[i*SIZE+j]);
printf("%s ", buf);
}
// printf("\033[m");
// for (i = 0; i < SIZE; ++i) printf("%X ", (tiles[i*SIZE+j]));
printf("\n");
}
printf("\033[38;5;242m --------\n\033[m");
//printf("╰────────╯\n");
}
int read_move(void){
char keys[MOVES] = {'k','i','j','l'};
int c;
int i;
while (isspace(c = getchar()));
if (c == EOF)
return c;
for (i = 0; i < MOVES; ++i)
if (c == keys[i])
return i;
return EOF;
}
static struct termios backup;
static struct termios current;
void term_start(void){
tcgetattr(STDIN_FILENO, &backup);
current = backup;
current.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &current);
}
void term_end(void){
tcsetattr(STDIN_FILENO, TCSANOW, &backup);
}
#include <time.h>
int main(){
int moves=0;
int c;
U8 tiles[] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
srand(time(0));
init(tiles);
print(tiles);
term_start();
while ((c = read_move()) != EOF) {
if(move(tiles, c)){
moves++;
print(tiles);
}
else {
// zeros++;
// if(zeros>4)
// printf("Ready to give up?\n");
}
}
printf("%d moves\n",moves);
term_end();
return 0;
}