summaryrefslogtreecommitdiffstats
path: root/qnetchess/src/gameboard.h
diff options
context:
space:
mode:
Diffstat (limited to 'qnetchess/src/gameboard.h')
-rw-r--r--qnetchess/src/gameboard.h245
1 files changed, 245 insertions, 0 deletions
diff --git a/qnetchess/src/gameboard.h b/qnetchess/src/gameboard.h
new file mode 100644
index 00000000..2db232ac
--- /dev/null
+++ b/qnetchess/src/gameboard.h
@@ -0,0 +1,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__ */