-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameboard.h
256 lines (220 loc) · 6.26 KB
/
gameboard.h
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
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) 2005 by SilverSoft.Net
* All rights reserved
*
* $Id: gameboard.h,v 0.1 2005/01/08 13:00:57 denis Exp $
*
* Author: Denis Kozadaev (denis@silversoft.net)
* Description:
*
* See also: style(9)
*
* Hacked by:
*/
#ifndef __GAME_BOARD_H__
#define __GAME_BOARD_H__
#include <qwidget.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <q3pointarray.h>
#include <qdialog.h>
#include <qlineedit.h>
#include <q3listbox.h>
#include <qtimer.h>
//Added by qt3to4:
#include <QCloseEvent>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QFocusEvent>
#include <QMouseEvent>
#include <QListWidgetItem>
#include <stdlib.h>
#include "ui_gameboard.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 GameProtocol;
class GameBoard:public QWidget
{
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 QString &, QWidget *parent = NULL,
const char *name = NULL);
GameBoard(const QString &h, QWidget *parent = NULL, const char *name = NULL);
~GameBoard();
void saveImage();
GameType type()const{return (gt);}
QString caption;
public slots:
void receiveData(const QString& data);
private:
Drawer *drw;
GameType gt;
FigureType *map;
QString hst;
QTimer *tmr, *tmr2;
int sock_tout;
GameProtocol* protocol;
Ui::gameboard* gmb;
void initMap();
void parseString(const QString&);
void updateChat(const QString&);
void updateHistory(const QString&, bool);
void updateHistory(int, bool);
protected:
void closeEvent(QCloseEvent *);
private slots:
void showHostFound();
void sockConnected();
void sockRead(const QString& data);
void sockClosed();
void sendMove(const QString&);
void sendText();
void sendFigure(const QString&, GameBoard::FigureType);
void gameover(int);
signals:
void sendData(const QString& data);
};
//-----------------------------------------------------------------------------
class Drawer:public QWidget
{
Q_OBJECT
public:
Drawer(QWidget *parent = NULL, const char *name = NULL);
~Drawer();
void setDrawer(GameBoard::FigureType *, GameBoard::GameType *);
void makeMove(const QString&);
void newFigure(const QString&, int);
private:
int top_margin, left_margin, hl;
int x_brd, y_brd, cs;
int tfx, tfy;
QPixmap fig[12];
GameBoard::FigureType *map;
GameBoard::GameType *gt;
bool km, lcm, rcm, kk;
void drawBoard(QPainter *, int, int);
void drawMap(QPainter *, 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(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
signals:
void touchFigure(int, int);
void moved(const QString&);
void newFigure(const QString&, GameBoard::FigureType);
void gameover(int);
};
//-----------------------------------------------------------------------------
class FigureDialog:public QDialog
{
Q_OBJECT
public:
FigureDialog(const QPixmap *, const GameBoard::GameType,
QWidget *parent = NULL, const char *name = NULL);
~FigureDialog();
GameBoard::FigureType figure()const{return (fr);}
private:
GameBoard::GameType gt;
const QPixmap *fig;
QString str;
int step, fh;
GameBoard::FigureType fr;
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
};
//-----------------------------------------------------------------------------
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(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListWhitePawn(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListBlackPawn(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListCastle(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListBishop(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListKing(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListQueen(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static void moveListKnight(Q3PointArray&, GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static bool hasPoint(const Q3PointArray&, int, int);
static bool hasKingsMeeting(GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static bool validPoint(GameBoard::GameType,
GameBoard::FigureType *, int, int, bool);
static QString map2str(int, int);
static void str2map(const QString&, int *, int *);
static int checkKing(GameBoard::GameType, GameBoard::FigureType *,
bool, Q3PointArray&, bool);
};
//-----------------------------------------------------------------------------
class GameProtocol:public QDialog
{
Q_OBJECT
public:
void send(const QString&);
void setGameType(GameBoard::GameType);
void acceptGame();
void sendMove( const QString&);
void sendQuit();
void sendText(const QString&);
void sendFigure(const QString&, int);
void sendGameover(const QString&);
signals:
void sendData(const QString& data);
};
#endif /* __GAME_BOARD_H__ */