root/gemmes_text.c

Revision 106, 4.3 kB (checked in by ng, 5 years ago)

mise a jour licence

Line 
1 /***************************************************************************
2  *   This file is part of the 'gemmes' project                             *
3  *                                                                         *
4  *                                                                         *
5  *   Copyright (C) 2007 by                                                 *
6  *         GARCH Soufiane                                                  *
7  *         GUILLAUME Nicolas <ng@ngsoft-fr.com>                            *
8  *                                                                         *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; version 2 of the License only.          *
13  *   See the COPYING file.                                                 *
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) /* tant que pas d'entree correct */
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); /* une ligne peut contenir plusieurs commandes */
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) /* c'est peut ĂȘtre une commande */
105     {
106         switch(line[0])
107         {
108         case 'd': /* dump */
109         {
110             gemmes_dump(b);
111             return 1;
112         }
113         case 'h': /* hint */
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': /* autoplay */
123             gemmes_autoplay(b);
124             *stop = 1;
125             return 1;
126             break;
127         case 't': /* autoplay & create a test*/
128             b->silent = 1;
129             gemmes_autoplay_createtest(b);
130             *stop = 1;
131             return 1;
132             break;
133         case '?': /* help */
134             if(!b->silent)
135                 puts(HELP_MSG);
136             return 1;
137             break;
138         case 'q': /* quit */
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) /* c'est peut ĂȘtre un coup */
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) /* partie finie */
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 }
Note: See TracBrowser for help on using the browser.