Updated README: Equal sign not required with `--mode` flag.
[sgk-go] / patterns / compress_fuseki.c
CommitLineData
7eeb782e
AT
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
3 * http://www.gnu.org/software/gnugo/ for more information. *
4 * *
5 * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
6 * 2008 and 2009 by the Free Software Foundation. *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License as *
10 * published by the Free Software Foundation - version 3 or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License in file COPYING for more details. *
17 * *
18 * You should have received a copy of the GNU General Public *
19 * License along with this program; if not, write to the Free *
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
21 * Boston, MA 02111, USA. *
22\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <assert.h>
27
28#define BUFSIZE 160
29#define MAX_STONES 40
30#define MAX_BOARDSIZE 19
31
32#define USAGE "\
33Usage : uncompress_fuseki boardsize filename\n\
34"
35
36/* Write a board point in sgf format. Also do a sanity check. */
37static void
38write_stone(int i, int j)
39{
40 assert(i > 0 && i <= MAX_BOARDSIZE);
41 assert(j > 0 && j <= MAX_BOARDSIZE);
42 printf("%c%c", j + 'a' - 1, i + 'a' - 1);
43}
44
45int
46main(int argc, char *argv[])
47{
48 const char *filename;
49 FILE *input_FILE;
50 char line[BUFSIZE];
51 char name[BUFSIZE];
52 int value;
53 int k;
54 int row = 0;
55 int movei = -1;
56 int movej = -1;
57 int xi[MAX_STONES];
58 int xj[MAX_STONES];
59 int oi[MAX_STONES];
60 int oj[MAX_STONES];
61 int num_x = 0;
62 int num_o = 0;
63
64 /* Check number of arguments. */
65 if (argc != 2) {
66 fprintf(stderr, USAGE);
67 return EXIT_FAILURE;
68 }
69
70 filename = argv[1];
71
72 input_FILE = fopen(filename, "r");
73 if (!input_FILE) {
74 fprintf(stderr, "compress_fuseki: Cannot open file %s\n", filename);
75 return EXIT_FAILURE;
76 }
77
78 while (fgets(line, BUFSIZE, input_FILE)) {
79 if (sscanf(line, "Pattern %s", name) == 1) {
80 /* A new name has been picked up.
81 * Reset the row counter and the lists of stones.
82 */
83 row = 0;
84 num_x = 0;
85 num_o = 0;
86 }
87 else if (line[0] == ':') {
88 /* The colon line ends the pattern. First get the move value. */
89 if (sscanf(line, ":8,-,value(%d)", &value) != 1) {
90 fprintf(stderr, "compress_fuseki: Misformed colon line \"%s\"\n",
91 line);
92 return EXIT_FAILURE;
93 }
94
95 /* Write the compressed description of this pattern.
96 * Pad the stone list with passes (tt) if unbalanced colors.
97 */
98 printf("%s %d ", name, value);
99 write_stone(movei, movej);
100 while (num_x > 0 || num_o > 0) {
101 if (num_x > 0) {
102 num_x--;
103 write_stone(xi[num_x], xj[num_x]);
104 }
105 else if (num_o > 0)
106 printf("tt");
107 if (num_o > 0) {
108 num_o--;
109 write_stone(oi[num_o], oj[num_o]);
110 }
111 else if (num_x > 0)
112 printf("tt");
113 }
114 printf("\n");
115 }
116 else if (line[0] == '|') {
117 /* Found a diagram line. */
118 row++;
119 for (k = 1; line[k] && line[k] != '|'; k++) {
120 if (line[k] == '*') {
121 movei = row;
122 movej = k;
123 }
124 else if (line[k] == 'X') {
125 xi[num_x] = row;
126 xj[num_x] = k;
127 num_x++;
128 assert(num_x < MAX_STONES);
129 }
130 else if (line[k] == 'O') {
131 oi[num_o] = row;
132 oj[num_o] = k;
133 num_o++;
134 assert(num_o < MAX_STONES);
135 }
136 }
137 }
138 }
139
140 return EXIT_SUCCESS;
141}
142
143/*
144 * Local Variables:
145 * tab-width: 8
146 * c-basic-offset: 2
147 * End:
148 */