summaryrefslogtreecommitdiffstats
path: root/qnetchess/src/gameboard.h
blob: 2db232ac557146c07ece02773d86e76b8b98e1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * $Id: gameboard.h,v 0.1 2005/01/08 13:00:57 denis Exp $
 *
 * Author: Denis Kozadaev (denis@tambov.ru)
 * Description:
 *
 * See also: style(9)
 *
 * Hacked by:
 */

#ifndef	__GAME_BOARD_H__
#define	__GAME_BOARD_H__

#include <ntqwidget.h>
#include <ntqpainter.h>
#include <ntqpixmap.h>
#include <ntqpointarray.h>
#include <ntqdialog.h>
#include <ntqsocket.h>
#include <ntqgroupbox.h>
#include <ntqlineedit.h>
#include <ntqlistbox.h>
#include <ntqtimer.h>
#include <stdlib.h>

#define	MAX(a, b)	(((a) > (b))?(a):(b))
#define	SEP		' '
#define	EOL		'\n'
#define	LONG_XCHG	"@-@"
#define	SHORT_XCHG	"o-o"
#define	SOCK_WAIT	900
#define	GAMEOVER_TXT	"****"

class GameBoard;
class Drawer;
class Figure;

class GameBoard:public TQWidget
{
	Q_OBJECT
public:
	enum GameType {
		NOGAME	= 0x0,
		BLACK	= 0x1,
		WHITE	= 0x2
	};

	enum FigureType {
		NONE		= 0x00,
		WHITE_PAWN	= 0x01,
		WHITE_CASTLE	= 0x02,
		WHITE_BISHOP	= 0x03,
		WHITE_KING	= 0x04,
		WHITE_QUEEN	= 0x05,
		WHITE_KNIGHT	= 0x06,
		BLACK_PAWN	= 0x11,
		BLACK_CASTLE	= 0x12,
		BLACK_BISHOP	= 0x13,
		BLACK_KING	= 0x14,
		BLACK_QUEEN	= 0x15,
		BLACK_KNIGHT	= 0x16,
		DUMMY		= 0xFF
	};

	GameBoard(GameType, const TQString &, TQWidget *parent = NULL,
		const char *name = NULL);
	GameBoard(int, TQWidget *parent = NULL, const char *name = NULL);
	~GameBoard();

	void		saveImage();

	GameType	type()const{return (gt);}
	TQString	status()const{return (my_stat);}

private:
	Drawer		*drw;
	GameType	gt;
	FigureType	*map;
	TQString	hst, my_stat;
	TQSocket	*sock;
	TQGroupBox	*box, *hist;
	TQListBox	*lst, *hw, *hb;
	TQLineEdit	*edt;
	TQTimer		*tmr;
	int		sock_tout;

	void	initMap();
	void	parseString(const TQString&);
	void	updateChat(const TQString&);
	void	updateHistory(const TQString&, bool);
	void	updateHistory(int, bool);

protected:
	void	resizeEvent(TQResizeEvent *);
	void	closeEvent(TQCloseEvent *);
	void	focusInEvent(TQFocusEvent *);

private slots:
	void	showHostFound();
	void	sockConnected();
	void	sockRead();
	void	sockClosed();
	void	sendMove(const TQString&);
	void	sendText();
	void	sendFigure(const TQString&, GameBoard::FigureType);
	void	sockTest();
	void	sockError(int);
	void	gameover(int);

signals:
	void	showStatus(const TQString&);
};

//-----------------------------------------------------------------------------

class Drawer:public TQWidget
{
	Q_OBJECT
public:
	Drawer(GameBoard::FigureType *, GameBoard::GameType *,
		TQWidget *parent = NULL, const char *name = NULL);
	~Drawer();

	void	makeMove(const TQString&);
	void	newFigure(const TQString&, int);

private:
	int			top_margin, left_margin, hl;
	int			x_brd, y_brd, cs;
	int			tfx, tfy;
	TQPixmap		fig[12];
	GameBoard::FigureType	*map;
	GameBoard::GameType	*gt;
	bool			km, lcm, rcm, kk;

	void	drawBoard(TQPainter *, int, int);
	void	drawMap(TQPainter *, int, int);
	void	win2map(int&, int&);
	void	map2win(int, int, int&, int&);
	void	takeFigure(int, int);
	void	makeMove(GameBoard::GameType, int, int, int, int, bool, bool);
	bool	xchg(GameBoard::FigureType, GameBoard::FigureType,
			int, int, int, int);
	bool	checkWhiteCastle(int, int, int, int, bool);
	bool	checkBlackCastle(int, int, int, int, bool);

	bool	canTake(int, int);
	bool	hasTakenFigure();
	bool	makeXchg();

protected:
	void	paintEvent(TQPaintEvent *);
	void	mousePressEvent(TQMouseEvent *);

signals:
	void	touchFigure(int, int);
	void	moved(const TQString&);
	void	newFigure(const TQString&, GameBoard::FigureType);
	void	gameover(int);
};

//-----------------------------------------------------------------------------

class FigureDialog:public TQDialog
{
	Q_OBJECT
public:
	FigureDialog(const TQPixmap *, const GameBoard::GameType,
		TQWidget *parent = NULL, const char *name = NULL);
	~FigureDialog();

	GameBoard::FigureType	figure()const{return (fr);}

private:
	GameBoard::GameType	gt;
	const TQPixmap		*fig;
	TQString		str;
	int			step, fh;
	GameBoard::FigureType	fr;

protected:
	void	paintEvent(TQPaintEvent *);
	void	mousePressEvent(TQMouseEvent *);
};

//-----------------------------------------------------------------------------

class Figure
{
public:

	static bool	hasMyFigure(GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static int	hasEnemyFigure(GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static bool	hasFigure(GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static int	map2map(GameBoard::GameType, int, int, bool);
	static int	validMove(GameBoard::GameType, GameBoard::FigureType *,
				int, int, int, int, bool);

	static void	moveList(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListWhitePawn(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListBlackPawn(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListCastle(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListBishop(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListKing(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListQueen(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static void	moveListKnight(TQPointArray&, GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static bool	hasPoint(const TQPointArray&, int, int);
	static bool	hasKingsMeeting(GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static bool	validPoint(GameBoard::GameType,
				GameBoard::FigureType *, int, int, bool);
	static TQString	map2str(int, int);
	static void	str2map(const TQString&, int *, int *);
	static int	checkKing(GameBoard::GameType, GameBoard::FigureType *,
				bool, TQPointArray&, bool);
};

//-----------------------------------------------------------------------------

class GameProtocol
{
public:
	static void	send(TQSocket *, const TQString&);
	static void	setGameType(TQSocket *, GameBoard::GameType);
	static void	acceptGame(TQSocket *);
	static void	sendMove(TQSocket *, const TQString&);
	static void	sendQuit(TQSocket *);
	static void	sendText(TQSocket *, const TQString&);
	static void	sendFigure(TQSocket *, const TQString&, int);
	static void	sendGameover(TQSocket *, const TQString&);
};

#endif	/* __GAME_BOARD_H__ */