| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#define _GNU_SOURCE |
|---|
| 20 |
#include <stdio.h> |
|---|
| 21 |
#include <stdlib.h> |
|---|
| 22 |
#include <string.h> |
|---|
| 23 |
#include <ctype.h> |
|---|
| 24 |
|
|---|
| 25 |
#include "assert.h" |
|---|
| 26 |
|
|---|
| 27 |
#include "gemmes.h" |
|---|
| 28 |
|
|---|
| 29 |
#define GEOF (size_t)(-1) |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
#define HELP_MSG "Moves are on three positions:\n" \ |
|---|
| 33 |
" - First char specifies the column to move from\n" \ |
|---|
| 34 |
" - Second one specifies the line to move from\n" \ |
|---|
| 35 |
" - Third one specifies the direction: Up, Down, Left, Right\n" \ |
|---|
| 36 |
"Examples: c4u (from c4, up); d7l (from d7, left)\n\n" \ |
|---|
| 37 |
"The goal is to switch gems so that you get 3 or more aligned.\n" |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
int gemmes_process_command(board_t b, char * line, int read, int * stop); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
void gemmes_start_ihm(board_t b) |
|---|
| 46 |
{ |
|---|
| 47 |
char * line = NULL; |
|---|
| 48 |
size_t len = 0; |
|---|
| 49 |
size_t read = 0; |
|---|
| 50 |
|
|---|
| 51 |
int stop = 0; |
|---|
| 52 |
int entree; |
|---|
| 53 |
|
|---|
| 54 |
while( read != GEOF && !stop ) |
|---|
| 55 |
{ |
|---|
| 56 |
board_print(b); |
|---|
| 57 |
|
|---|
| 58 |
if(!b->silent && b->last_seg_count) |
|---|
| 59 |
printf("This move created %d segment(s).\n", b->last_seg_count); |
|---|
| 60 |
|
|---|
| 61 |
entree = 0; |
|---|
| 62 |
while(!entree && read != GEOF && !stop) |
|---|
| 63 |
{ |
|---|
| 64 |
if(!b->silent) |
|---|
| 65 |
puts("\nWhat's your move? (or ? for help, h for hint, d for dump, a for autoplay, q to quit)"); |
|---|
| 66 |
|
|---|
| 67 |
read = getline(&line, &len, stdin); |
|---|
| 68 |
|
|---|
| 69 |
if(read != GEOF) |
|---|
| 70 |
{ |
|---|
| 71 |
char * cmd = line; |
|---|
| 72 |
unsigned int i; |
|---|
| 73 |
for(i = 0; i < read; ++i) |
|---|
| 74 |
{ |
|---|
| 75 |
line[i] = tolower(line[i]); |
|---|
| 76 |
|
|---|
| 77 |
if(line[i] == ' ' || i == read - 1) |
|---|
| 78 |
{ |
|---|
| 79 |
entree = gemmes_process_command(b, cmd, line + i - cmd, &stop) || entree; |
|---|
| 80 |
if(stop) |
|---|
| 81 |
break; |
|---|
| 82 |
|
|---|
| 83 |
if(i + 1 < read) |
|---|
| 84 |
cmd = line + i + 1; |
|---|
| 85 |
|
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
if (line) |
|---|
| 94 |
free(line); |
|---|
| 95 |
|
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
int gemmes_process_command(board_t b, char * line, int read, int * stop) |
|---|
| 99 |
{ |
|---|
| 100 |
c_assert(stop); |
|---|
| 101 |
|
|---|
| 102 |
if(read == 0) |
|---|
| 103 |
return 0; |
|---|
| 104 |
else if(read == 1) |
|---|
| 105 |
{ |
|---|
| 106 |
switch(line[0]) |
|---|
| 107 |
{ |
|---|
| 108 |
case 'd': |
|---|
| 109 |
{ |
|---|
| 110 |
gemmes_dump(b); |
|---|
| 111 |
return 1; |
|---|
| 112 |
} |
|---|
| 113 |
case 'h': |
|---|
| 114 |
{ |
|---|
| 115 |
coord_t c = board_get_hint(b); |
|---|
| 116 |
|
|---|
| 117 |
if(!b->silent && c.x != -1) |
|---|
| 118 |
printf("Have a look at %c%d\n", 'a' + c.x, 1 + c.y); |
|---|
| 119 |
|
|---|
| 120 |
return 0; |
|---|
| 121 |
} |
|---|
| 122 |
case 'a': |
|---|
| 123 |
gemmes_autoplay(b); |
|---|
| 124 |
*stop = 1; |
|---|
| 125 |
return 1; |
|---|
| 126 |
break; |
|---|
| 127 |
case 't': |
|---|
| 128 |
b->silent = 1; |
|---|
| 129 |
gemmes_autoplay_createtest(b); |
|---|
| 130 |
*stop = 1; |
|---|
| 131 |
return 1; |
|---|
| 132 |
break; |
|---|
| 133 |
case '?': |
|---|
| 134 |
if(!b->silent) |
|---|
| 135 |
puts(HELP_MSG); |
|---|
| 136 |
return 1; |
|---|
| 137 |
break; |
|---|
| 138 |
case 'q': |
|---|
| 139 |
*stop = 1; |
|---|
| 140 |
return 1; |
|---|
| 141 |
default: |
|---|
| 142 |
if(!b->silent) |
|---|
| 143 |
fputs("Invalid command\n", stderr); |
|---|
| 144 |
return 0; |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
else if(read == 3 || read == 4) |
|---|
| 148 |
{ |
|---|
| 149 |
dir_t dir; |
|---|
| 150 |
|
|---|
| 151 |
switch(line[2 + read - 3]) |
|---|
| 152 |
{ |
|---|
| 153 |
case 'u': dir = up; break; |
|---|
| 154 |
case 'd': dir = down; break; |
|---|
| 155 |
case 'l': dir = left; break; |
|---|
| 156 |
case 'r': dir = right; break; |
|---|
| 157 |
default: |
|---|
| 158 |
if(!b->silent) |
|---|
| 159 |
fprintf(stderr, "Invalid direction specifier (%c): must be one of 'u' (up), 'd' (down), 'r' (right), 'l' (left)\n\n", line[2]); |
|---|
| 160 |
return 0; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
int l = read == 3 ? line[1] - '1' : (line[1] - '0') * 10 + line[2] - '1'; |
|---|
| 164 |
|
|---|
| 165 |
int ret = !board_move(b, line[0] - 'a', l, dir); |
|---|
| 166 |
|
|---|
| 167 |
if(ret && board_get_hint(b).x == -1) |
|---|
| 168 |
{ |
|---|
| 169 |
if(!b->silent) |
|---|
| 170 |
fputs("Game over!\n", stderr); |
|---|
| 171 |
|
|---|
| 172 |
*stop = 1; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
return ret; |
|---|
| 176 |
|
|---|
| 177 |
} |
|---|
| 178 |
else |
|---|
| 179 |
{ |
|---|
| 180 |
if(!b->silent) |
|---|
| 181 |
fputs("Invalid command\n", stderr); |
|---|
| 182 |
return 0; |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|