1 /* output.c
2 
3    GNU Chess frontend
4 
5    Copyright (C) 2001-2017 Free Software Foundation, Inc.
6 
7    GNU Chess is based on the two research programs
8    Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
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, either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 
23    Contact Info:
24      bug-gnu-chess@gnu.org
25      cracraft@ai.mit.edu, cracraft@stanfordalumni.org, cracraft@earthlink.net
26 */
27 
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include "common.h"
32 
33 #define MAX_BOARD_RANGE 65
34 #define PIECE_SIZE 8
35 const char w_pieces[6][PIECE_SIZE] = {"\u2654 ", "\u2655 ", "\u2656 ", "\u2657 ", "\u2658 ", "\u2659 "};
36 const char b_pieces[6][PIECE_SIZE] = {"\u265A ", "\u265B ", "\u265C ", "\u265D ", "\u265E ", "\u265F "};
37 const unsigned ui_king = 0;
38 const unsigned ui_queen = 1;
39 const unsigned ui_rook = 2;
40 const unsigned ui_bishop = 3;
41 const unsigned ui_knight = 4;
42 const unsigned ui_pawn = 5;
43 
44 const char white_square[] = "\033[7;37m";
45 const char black_square[] = "\033[7;35m";
46 const char default_console[] = "\033[0m";
47 const char blank_place[] = "  ";
48 
49 
50 /**************************************************************************
51  * Print out the board in a new style way.
52  **************************************************************************/
53 static void ShowStylishBoard(const char *boardmap);
54 
55 /**************************************************************************
56  * Fill the array piece with the new style piece code
57  **************************************************************************/
58 static void GetPiece(const char orig_piece, char *piece);
59 
60 /**************************************************************************
61  * Print out the classical gnuchess board.
62  **************************************************************************/
63 static void ShowClassicalBoard(const char *boardmap);
64 
65 
ShowTime(void)66 void ShowTime (void)
67 /**************************************************************************
68  *
69  *  Print out the time settings.
70  *
71  **************************************************************************/
72 {
73 }
74 
ShowMoveList(int ply)75 void ShowMoveList (int ply)
76 /**************************************************************************
77  *
78  *  Print out the move list.
79  *
80  **************************************************************************/
81 {
82    leaf *node;
83    int i = 0;
84 
85    for (node = TreePtr[ply]; node < TreePtr[ply+1]; node++)
86    {
87       SANMove (node->move, ply);
88       printf ("%5s %3d\t", SANmv, SwapOff(node->move));
89       if (++i == 5)
90       {
91          printf ("\n");
92          i = 0;
93       }
94    }
95    printf ("\n");
96 }
97 
98 
ShowBoard(void)99 void ShowBoard (void)
100 /*****************************************************************************
101  *
102  *  Display the board.  Not only that but display some useful information
103  *  like whether enpassant is legal and castling state.
104  *
105  *****************************************************************************/
106 {
107    int r, c, sq;
108 
109    fprintf (ofp, "\n");
110 
111    if (coords == 1) {
112       fprintf(ofp, "  ");
113    }
114 
115    if (board.side == white)
116       fprintf (ofp, "white  ");
117    else
118       fprintf (ofp, "black  ");
119 
120    if (board.flag & WKINGCASTLE)
121       fprintf (ofp, "K");
122    if (board.flag & WQUEENCASTLE)
123       fprintf (ofp, "Q");
124    if (board.flag & BKINGCASTLE)
125       fprintf (ofp, "k");
126    if (board.flag & BQUEENCASTLE)
127       fprintf (ofp, "q");
128 
129    if (board.ep > -1)
130       fprintf (ofp, "  %s", algbr[board.ep]);
131 
132    fprintf (ofp, "\n");
133 
134    char arr_board[MAX_BOARD_RANGE];
135    unsigned bIndex = 0;
136    memset(arr_board, '\0', MAX_BOARD_RANGE);
137 
138    for (r = 56; r >= 0; r -= 8)
139    {
140       for (c = 0; c < 8; c++)
141       {
142          sq = r + c;
143          if (board.b[white][pawn]   & BitPosArray[sq])
144             arr_board[bIndex++] = 'P';
145          else if (board.b[white][knight] & BitPosArray[sq])
146             arr_board[bIndex++] = 'N';
147          else if (board.b[white][bishop] & BitPosArray[sq])
148             arr_board[bIndex++] = 'B';
149          else if (board.b[white][rook]   & BitPosArray[sq])
150             arr_board[bIndex++] = 'R';
151          else if (board.b[white][queen]  & BitPosArray[sq])
152             arr_board[bIndex++] = 'Q';
153          else if (board.b[white][king]   & BitPosArray[sq])
154             arr_board[bIndex++] = 'K';
155          else if (board.b[black][pawn]   & BitPosArray[sq])
156             arr_board[bIndex++] = 'p';
157          else if (board.b[black][knight] & BitPosArray[sq])
158             arr_board[bIndex++] = 'n';
159          else if (board.b[black][bishop] & BitPosArray[sq])
160             arr_board[bIndex++] = 'b';
161          else if (board.b[black][rook]   & BitPosArray[sq])
162             arr_board[bIndex++] = 'r';
163          else if (board.b[black][queen]  & BitPosArray[sq])
164             arr_board[bIndex++] = 'q';
165          else if (board.b[black][king]   & BitPosArray[sq])
166             arr_board[bIndex++] = 'k';
167          else
168             arr_board[bIndex++] = '.';
169       }
170    }
171 
172    if ( graphicmodeoutput == 1) {
173      ShowStylishBoard(arr_board);
174    } else {
175      ShowClassicalBoard(arr_board);
176    }
177 
178   fprintf (ofp, "\n");
179 }
180 
181 
ShowStylishBoard(const char * boardmap)182 void ShowStylishBoard(const char *boardmap)
183 {
184   unsigned i = 0;
185   unsigned row = 8;
186   const char column[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
187   int b_white = 1;
188   char tmp_piece[PIECE_SIZE];
189   memset(tmp_piece, '\0', PIECE_SIZE);
190 
191   if ( coords == 1 ) fprintf(ofp, "%d ", row);
192   row--;
193 
194   for (i =0; i < MAX_BOARD_RANGE; ++i)
195     {
196         fprintf(ofp, "%s", default_console);
197 
198         if ((i > 0) && (i % 8 == 0))
199             {
200             fprintf(ofp, "\n");
201             b_white = !b_white;
202 
203             if (row >=1) {
204               if( coords == 1 ) fprintf(ofp, "%d ", row);
205               row--;
206             }
207             else
208                 break;
209             }
210 
211         GetPiece(boardmap[i], tmp_piece);
212 
213         if (b_white)
214             {
215             fprintf(ofp, "%s%s", white_square, tmp_piece);
216             }
217         else
218             {
219             fprintf(ofp, "%s%s", black_square, tmp_piece);
220             }
221 
222         b_white = !b_white;
223    }
224    fprintf(ofp, "%s  ", default_console);
225 
226    if ( coords == 1 ) {
227      for (i = 0; i < 8; ++i) {
228           fprintf(ofp, "%c ", column[i]);
229      }
230    }
231 }
232 
233 
GetPiece(const char orig_piece,char * piece)234 void GetPiece(const char orig_piece, char *piece)
235 {
236     if (orig_piece == 'p')
237         strcpy(piece, b_pieces[ui_pawn]);
238     else if (orig_piece == 'P')
239         strcpy(piece, w_pieces[ui_pawn]);
240     else if (orig_piece == 'n')
241         strcpy(piece, b_pieces[ui_knight]);
242     else if (orig_piece == 'N')
243         strcpy(piece, w_pieces[ui_knight]);
244     else if (orig_piece == 'b')
245         strcpy(piece, b_pieces[ui_bishop]);
246     else if (orig_piece == 'B')
247         strcpy(piece, w_pieces[ui_bishop]);
248     else if (orig_piece == 'r')
249         strcpy(piece, b_pieces[ui_rook]);
250     else if (orig_piece == 'R')
251         strcpy(piece, w_pieces[ui_rook]);
252     else if (orig_piece == 'q')
253         strcpy(piece, b_pieces[ui_queen]);
254     else if (orig_piece == 'Q')
255         strcpy(piece, w_pieces[ui_queen]);
256     else if (orig_piece == 'k')
257         strcpy(piece, b_pieces[ui_king]);
258     else if (orig_piece == 'K')
259         strcpy(piece, w_pieces[ui_king]);
260     else if (orig_piece == '.')
261         strcpy(piece, blank_place);
262 }
263 
264 
ShowClassicalBoard(const char * boardmap)265 void ShowClassicalBoard(const char *boardmap)
266 {
267   unsigned i = 0;
268   const char column[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
269   unsigned row = 8;
270 
271   for (i =0; i < MAX_BOARD_RANGE; ++i) {
272     if ((i >= 0) && (i % 8 == 0)) {
273       fprintf(ofp, "\n");
274 
275       if ( coords == 1 ) {
276         if (row >=1)
277           fprintf(ofp, "%d ", row--);
278         else
279           break;
280       }
281 
282     }
283 
284     fprintf(ofp, "%c ", boardmap[i]);
285   }
286 
287   if ( coords == 1 ) {
288     fprintf(ofp, "  ");
289     for (i = 0; i < 8; ++i) {
290       fprintf(ofp, "%c ", column[i]);
291     }
292   }
293 }
294 
295 
ShowCBoard(void)296 void ShowCBoard (void)
297 /*****************************************************************************
298  *
299  *
300  *
301  *****************************************************************************/
302 {
303    int r, c;
304 
305    for (r = 56; r >= 0; r -= 8)
306    {
307       for (c = 0; c < 8; c++)
308       {
309          printf ("%2c ", cboard[r + c] ? notation[cboard[r+c]] : '.');
310       }
311       printf ("\n");
312    }
313    printf ("\n");
314 }
315 
316 
ShowMvboard(void)317 void ShowMvboard (void)
318 /*****************************************************************************
319  *
320  *  Print the Mvboard[] array.
321  *
322  *****************************************************************************/
323 {
324    int r, c;
325 
326    for (r = 56; r >= 0; r -= 8)
327    {
328       for (c = 0; c < 8; c++)
329       {
330          printf ("%2d ", Mvboard[r + c]);
331       }
332       printf ("\n");
333    }
334    printf ("\n");
335 }
336 
ShowGame(void)337 void ShowGame (void)
338 {
339   int i;
340 
341 /* *********************************************
342    * We must handle the special case of an EPD *
343    * game where the first move is by black     *
344    ********************************************* */
345 
346   if ( GameCnt >= 0 )
347   {
348 
349     printf ("      White   Black\n");
350 
351     if ( ( board.side == white && GameCnt % 2 == 1 ) ||
352          ( board.side == black && GameCnt % 2 == 0 ))
353     {
354 
355       for (i = 0; i <= GameCnt; i += 2)
356         {
357           printf ("%3d.  %-7s %-7s\n", i/2 + 1, Game[i].SANmv,
358     	      Game[i + 1].SANmv);
359         }
360     }
361     else {
362 
363       printf ("  1.          %-7s\n", Game[0].SANmv);
364 
365       for (i = 1; i <= GameCnt; i += 2)
366         {
367           printf ("%3d.  %-7s %-7s\n", i/2 + 2, Game[i].SANmv,
368     	      Game[i + 1].SANmv);
369         }
370     }
371     printf ("\n");
372   }
373 }
374