6 /* Standalone program that writes the initializer for the position
7 * object containing the game array.
11 /* Write information to the output file about how the output file was
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");
21 "This file is generated using the program %s -- DON'T CHANGE.\n",
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
)
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
]);
48 fprintf(dest
,"%2d\n",board
[i
]);
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
[])
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
]);
73 fprintf(dest
,"%lu\n",spec
[i
]);
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
,
97 fprintf(dest
," %u, ",inum
);
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
)
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
]);
124 fprintf(dest
,"%u\n",nr_piece(*pos
)[p
]);
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");
137 dump_board_initialiser_to_stream(dest
,pos
->board
);
139 dump_spec_initialiser_to_stream(dest
,pos
->spec
);
141 dump_royal_initialisers_to_stream(dest
,pos
->rb
,pos
->rn
);
143 dump_imitator_initialisers_to_stream(dest
,pos
->inum
,pos
->isquare
);
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
,
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");
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");