updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / popeye / gengmarr.c
blob7567520747aa426f1b84e6cc434b67a727f33c82
1 #include "pyposit.h"
3 #include <stdio.h>
4 #include <stdlib.h>
6 /* Standalone program that writes the initializer for the position
7 * object containing the game array.
8 */
11 /* Write information to the output file about how the output file was
12 * generated.
13 * @param dest destination stream (output file)
14 * @param argv0 name of executable produced from this file (i.e. that
15 * produces the outfile)
17 static void write_generation_info(FILE *dest, char const *argv0)
19 fprintf(dest,"/******** IMPORTANT INFORMATION ****************\n");
20 fprintf(dest,
21 "This file is generated using the program %s -- DON'T CHANGE.\n",
22 argv0);
23 fprintf(dest,"%s was compiled from %s.\n",argv0,__FILE__);
24 fprintf(dest,"***********************************************/\n");
27 /* Write board initialiser to output file
28 * @param dest destination stream (output file)
29 * @param board initialized board to dump
31 static void dump_board_initialiser_to_stream(FILE *dest, echiquier const board)
33 unsigned int i;
34 size_t const nr_squares = maxsquare+4;
35 unsigned int column = 0;
37 fprintf(dest," { /* board */\n ");
38 for (i = 0; i+1<nr_squares; ++i)
40 fprintf(dest,"%2d,",board[i]);
41 ++column;
42 if (column==onerow)
44 fprintf(dest,"\n ");
45 column = 0;
48 fprintf(dest,"%2d\n",board[i]);
49 fprintf(dest," }");
52 /* Write piece specs initialiser to output file
53 * @param dest destination stream (output file)
54 * @param spec initialized specs to dump
56 static void dump_spec_initialiser_to_stream(FILE *dest, Flags const spec[])
58 unsigned int i;
59 size_t const nr_squares = maxsquare+4;
60 unsigned int column = 0;
62 fprintf(dest," { /* spec */\n ");
63 for (i = 0; i+1<nr_squares; ++i)
65 fprintf(dest,"%lu,",spec[i]);
66 ++column;
67 if (column==onerow)
69 fprintf(dest,"\n ");
70 column = 0;
73 fprintf(dest,"%lu\n",spec[i]);
74 fprintf(dest," }");
77 /* Write king square initialisers to output file
78 * @param dest destination stream (output file)
79 * @param rb position of white king to dump
80 * @param rn position of black king to dump
82 static void dump_royal_initialisers_to_stream(FILE *dest, square rb, square rn)
84 fprintf(dest," %u,%u /* king positions */",rb,rn);
87 /* Write imitator initialiser to output file
88 * @param dest destination stream (output file)
89 * @param inum initial number of imitators
90 * @param isquare initial position of imitators
92 static void dump_imitator_initialisers_to_stream(FILE *dest,
93 unsigned int inum,
94 imarr const isquare)
96 unsigned int i;
97 fprintf(dest," %u, ",inum);
98 fprintf(dest,"{ ");
99 for (i = 0; i+1<maxinum; ++i)
100 fprintf(dest,"%u,",isquare[i]);
101 fprintf(dest,"%u } /* imitators */",isquare[i]);
104 /* Write piece count initialiser to output file
105 * @param dest destination stream (output file)
106 * @param nr_piece array containing initial piece counts
108 static void dump_nr_piece_initialisers_to_stream(FILE *dest, position const *pos)
110 piece p;
111 unsigned int column = 0;
113 fprintf(dest," { /* numbers of pieces */\n ");
114 for (p = dernoi; p+1<derbla; ++p)
116 fprintf(dest,"%u,",nr_piece(*pos)[p]);
117 ++column;
118 if (column==20)
120 fprintf(dest,"\n ");
121 column = 0;
124 fprintf(dest,"%u\n",nr_piece(*pos)[p]);
125 fprintf(dest," }");
128 /* Write position initialiser to output file
129 * @param dest destination stream (output file)
130 * @param pos array of position object containing initial position
132 static void dump_position_initialiser_to_stream(FILE *dest, position const *pos)
134 fprintf(dest,"#include \"pyposit.h\"\n");
135 fprintf(dest,"position const game_array =\n");
136 fprintf(dest,"{\n");
137 dump_board_initialiser_to_stream(dest,pos->board);
138 fprintf(dest,",\n");
139 dump_spec_initialiser_to_stream(dest,pos->spec);
140 fprintf(dest,",\n");
141 dump_royal_initialisers_to_stream(dest,pos->rb,pos->rn);
142 fprintf(dest,",\n");
143 dump_imitator_initialisers_to_stream(dest,pos->inum,pos->isquare);
144 fprintf(dest,",\n");
145 dump_nr_piece_initialisers_to_stream(dest,pos);
146 fprintf(dest,"\n};\n");
149 /* Write position initialiser to output file (yet to be opened);
150 * produce error message if output file can't be opened.
151 * @param pos array of position object containing initial position
152 * @param argv0 first element of parameter argv (i.e. name of executable)
154 static void dump_position_initialiser(position const *pos,
155 char const *argv0,
156 FILE *dest)
158 write_generation_info(dest,argv0);
159 dump_position_initialiser_to_stream(dest,pos);
162 int main(int argc, char *argv[])
164 FILE * const output_stream = argc<=1 ? stdout : fopen(argv[1],"w");
165 if (output_stream==NULL)
167 perror("Error opening output file");
168 return EXIT_FAILURE;
170 else
172 position game_array;
173 initialise_game_array(&game_array);
175 dump_position_initialiser(&game_array,argv[0],output_stream);
177 if (fclose(output_stream)==EOF)
179 perror("Error closing output file");
180 return EXIT_FAILURE;
182 else
183 return EXIT_SUCCESS;