1 /*
2  * 3Dc.h
3  *
4  * Definitions file for 3Dc
5  */
6 /*
7 
8     3Dc, a game of 3-Dimensional Chess
9     Copyright (C) 1995  Paul Hicks
10 
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15 
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20 
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 
25     E-Mail: paulh@euristix.ie
26 */
27 #ifndef __3Dc_H
28 #define __3Dc_H
29 
30 #include <X11/Intrinsic.h> /* For all other X stuff */
31 #include "local.h"
32 #include "machine.h"
33 
34 /*****************************************************************************
35  * A miscellany of useful tidbits
36  */
37 #ifndef ABS
38 #define ABS(a) ((a) < 0 ? -(a) : (a))
39 #endif /* ABS */
40 
41 /* Returns from pieceMayMove */
42 #define CASTLE 2
43 #define EnPASSANT 3
44 #define PROMOTE 4
45 
46 typedef enum
47 {                                          /* Misnomers (but handy) */
48   king, queen, bishop, knight, rook,       /* Royalty  */
49   prince, princess, abbey, cannon, galley, /* Nobility */
50   pawn, none
51 } Title;
52 #define TITLES 11
53 Global int titleCount[TITLES];
54 #define PIECES 48
55 
56 typedef enum
57 {
58   NOCOL = -1, WHITE, BLACK
59 } Colour;
60 #define COLOURS 2
61 
62 typedef struct
63 {
64   unsigned
65     xFile  :3,
66     yRank  :3,
67     zLevel :2;
68 } Coord;
69 
70 typedef int File;
71 #define FILES 8
72 typedef int Rank;
73 #define RANKS 8
74 typedef int Level;
75 #define LEVELS 3
76 
77 /* Directions */
78 typedef int Dir;
79 #define LEFT  -1
80 #define RIGHT  1
81 #define FORW   1  /* if colour is black => -1 */
82 #define BACK  -1  /* if colour is black => +1 */
83 #define UP     1
84 #define DOWN  -1
85 #define NODIR  0
86 #define RANDDIR() ((random()%3)-1)
87 
88 #define HORZ(x, y) ( ((x)==0) ^ ((y)==0) )
89 #define DIAG(x, y) ((x)!=0 && (ABS(x)==ABS(y)))
90 #define HORZ2D(x, y, z) (HORZ(x, y) && (z)==0)
91 #define DIAG2D(x, y, z) (DIAG(x, y) && (z)==0)
92 /* Don't have to check for z==0 in 2nd line below bcs at least one of
93  * x,y is 0 so the check is implicit */
94 #define HORZ3D(x, y, z) ((HORZ(x, y) && \
95                           ((ABS(z)==ABS(x)) || \
96                            (ABS(z)==ABS(y)) || ((z)==0))) || \
97                          ((x)==0 && (y)==0 && (z)!=0))
98 #define DIAG3D(x, y, z) (DIAG(x, y) && \
99                           ((z)==0 || ABS(z)==ABS(x)))
100 
101 /*
102  * End miscellany
103  ****************************************************************************/
104 
105 /*****************************************************************************
106  * Piece definitions for all pieces
107  */
108 typedef struct
109 {
110   Coord xyzPos;
111   Colour bwSide;
112   Title nName;
113   unsigned bVisible :1,
114            bHasMoved:1; /* For king, rook, pawn only */
115 } Piece;
116 
117 Global Piece *SQUARE_INVALID, *SQUARE_EMPTY;
118 Global Boolean IsMoveLegal( const Piece *, const Piece *);
119 Global Boolean PieceMayMove(Piece *, const File, const Rank, const Level);
120 Global Boolean PieceMove(Piece *, const File, const Rank, const Level);
121 Global Boolean PieceUndo(void);
122 Global Piece *SquareThreatened(Colour, const File, const Rank, const Level);
123 Global Boolean IsKingChecked(Colour);
124 Global Boolean FakeMoveAndIsKingChecked( Piece *,
125                                         const File, const Rank, const Level);
126 Global Piece *TraverseDir(const Piece *, Dir, Dir, Dir, unsigned);
127 Global Piece *PieceNew(const Title, const File, const Rank, const Level,
128                        const Colour);
129 Global void PieceDelete(Piece *);
130 /*
131  * End piece definitions
132  ****************************************************************************/
133 
134 /*****************************************************************************
135  * The move-stack (for undos, checking for en passant, etc)
136  */
137 
138 typedef struct
139 {
140   Coord xyzBefore;
141   Coord xyzAfter;
142   Piece *pVictim;
143   /*  Status of bHasMoved before move.  Relevant to Kings, Rooks, and Pawns.
144    *  TRUE, FALSE, PROMOTE, CASTLE or EnPASSANT. */
145   int nHadMoved;
146 } Move;
147 
148 struct stack_el
149 {
150   Move *mvt;
151   struct stack_el *below;
152 };
153 
154 typedef struct
155 {
156   int nSize;
157   struct stack_el *top;
158 } stack;
159 
160 Global stack *StackNew(void);
161 Global void StackDelete(stack *);
162 Global void StackPush(stack *, const Move *);
163 Global Move *StackPop(stack *);
164 Global Move *StackPeek(stack *, int); /* Returns move n places down stack.  Do not free! */
165 #ifdef DEBUG
166 Global void StackDump(stack *);
167 #endif /* DEBUG */
168 
169 Global stack *FindAllMoves(Piece *);
170 
171 Global stack *MoveStack;
172 /*
173  * End of the move stack
174  ****************************************************************************/
175 
176 /****************************************************************************/
177 Global Piece *Board[LEVELS][RANKS][FILES];
178 Global Piece *Muster[COLOURS][PIECES]; /* Database of all pieces */
179 Global Colour bwToMove;
180 #include "x3Dc.h"
181 #include "3DcErr.h"
182 
183 /* And finally the function prototypes for the game itself */
184 Global Boolean Init3Dc(void);
185 Global int MusterIdx(const Title, const int);
186 Global char *Piece2String( Piece * );
187 Global Colour Computer(void);
188 Global void PauseGame(void);
189 Global void ResumeGame(void);
190 Global Boolean IsGamePaused(void);
191 Global Boolean IsGameFinished(void);
192 Global void FinishGame(Colour);
193 Global void PrintMove( const Move * );
194 
195 /* ComputerPlay stuff */
196 Global Boolean    GenMove(const Colour, Move **);
197 Global Boolean GenAltMove(const Colour, Move **);
198 
199 #endif /* __3Dc_h */
200