go-tui

Une TUI pour jouer au go - retour accueil

git clone git://bebou.netlib.re/go-tui

Log | Files | Refs |

gotui.c (2110B)


      1 #define TB_IMPL
      2 #include "termbox2.h"
      3 #include <stdbool.h>
      4 
      5 #define TAIL 100
      6 #define MAX_CAR_NUMBER 3
      7 #define NB_DEFAULT_PLAYERS 2
      8 
      9 enum color { BLACK, WHITE };
     10 
     11 struct stone {
     12 	int x;
     13 	int y;
     14 	int color;
     15 };
     16 
     17 /*Game function*/
     18 
     19 struct stone create_stone(int x, int y, int color) {
     20 	struct stone s;
     21 	s.x=x; s.y=y; s.color=color;
     22 	return s;
     23 }
     24 
     25 /*Helper functions*/
     26 
     27 int rd(int min, int max) { return (rand()%(max-min+1))+min; }
     28 int remain(int a, int b) { return ((a%b)+b)%b; }
     29 
     30 /*Display functions*/
     31 
     32 void display(struct stone* stone_list, int count) {
     33 	tb_print(0,0,0,0,"\
     34 ┌─┬─┬─┬─┬─┬─┬─┬─┬─┐\n\
     35 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     36 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     37 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     38 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     39 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     40 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     41 ├─┼─┼─┼─┼─┼─┼─┼─┼─┤\n\
     42 └─┴─┴─┴─┴─┴─┴─┴─┴─┘");
     43 	for(int i=0;i<count;i++) {
     44 		if(stone_list[i].color==BLACK) tb_printf(stone_list[i].x,stone_list[i].y,0,0,"○");
     45 		else                           tb_printf(stone_list[i].x,stone_list[i].y,0,0,"●");
     46 		tb_printf(15,15,0,0,"%d",stone_list[i].x);
     47 	}
     48 }
     49 
     50 int main(int argc, char **argv) {
     51 	int input=0;
     52 	tb_init();
     53 	tb_hide_cursor();
     54 	tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
     55 	struct tb_event ev;
     56 	struct stone stone_list[100];
     57 	int stone_count=0;
     58 
     59 	while(1) {
     60 		tb_clear();
     61 		display(stone_list,stone_count);
     62 		tb_present();
     63 		tb_poll_event(&ev);
     64 
     65 		if      (ev.ch!=0)  input=ev.ch;
     66 		else if (ev.key!=0) input=ev.key;
     67 
     68 		struct stone s;
     69 		int color=WHITE;
     70 		switch(input) {
     71 			case 65512: /*MouseLeft*/
     72 				if(stone_count%2) { color=WHITE; } else { color=BLACK; }
     73 				s=create_stone(ev.x,ev.y,color);
     74 				stone_list[stone_count]=s;
     75 				stone_count++;
     76 				break;
     77 			case 65511: /*MouseRight*/
     78 				break;
     79 			case 113:
     80 				tb_shutdown();
     81 				return 0;
     82 				break;
     83 		}
     84 	}
     85 	return 0;
     86 }