-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_game.h
150 lines (112 loc) · 3.56 KB
/
chess_game.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
#ifndef __CHESS_GAME_H__
#define __CHESS_GAME_H__
#include "chess_vector.h"
#include "chess_md2.h"
#include "chess_piece.h"
#include "chess_board.h"
// game states
const char WAIT_STATE = 0x01; // waiting for input
const char MOVING_STATE = 0x02; // moving a piece
const char CAPTURE_STATE = 0x04; // capturing a piece
const char KILL_STATE = 0x08; // killing a piece
const char SWITCH_STATE = 0x10; // switching player
//const char OPPSITE_WAIT = 0x20; // waiting for opposite user input
class ChessGame {
private:
ChessBoard* chessBoard;
ChessPiece pieces[32];
MD2Data whitePawn;
MD2Data blackPawn;
MD2Data whiteRook;
MD2Data blackRook;
MD2Data whiteKnight;
MD2Data blackKnight;
MD2Data whiteBishop;
MD2Data blackBishop;
MD2Data whiteQueen;
MD2Data blackQueen;
MD2Data whiteKing;
MD2Data blackKing;
int selectedPieceIdx;
int currentRow;
int currentCol;
int pieceMovingIdx; // current moving piece index
int pieceCaptureIdx; // current piece being captured index
char currentMoveColor; // color with the current move
char checkColor; // color of king in check, if any (NO_COLOR is possible)
char gameColor;
bool isTurn;
char gameState;
char oppsiteState;
float pieceMoveStartRow; // starting position of a piece move
float pieceMoveStartCol;
float pieceMoveEndRow; // ending position of a piece move
float pieceMoveEndCol;
float pieceMoveCurrentRow;
float pieceMoveCurrentCol;
float pieceMoveDistance; // distance the piece needs to move in world coordinates
float pieceRotateAngle;
Vector pieceVelocity; // velocity vector for movement
Vector piecePos; // position vector for movement
float pieceCaptureDeadTime;
bool pieceKilled;
bool pieceDying;
void setupWhitePieces();
void setupBlackPieces();
void movePiece(int pieceIdx, int destRow, int destCol, bool captureMove = false);
void movePieceImmediate(int pieceIdx, int destRow, int destCol);
void capturePiece(int pieceIdx, int destRow, int destCol);
bool isPieceInPlay(int r, int c);
bool isPieceCaptured(char color, int r, int c);
bool isValidPawnMove(int pieceIdx, int newRow, int newCol);
bool isValidRookMove(int pieceIdx, int newRow, int newCol);
bool isValidKnightMove(int pieceIdx, int newRow, int newCol);
bool isValidBishopMove(int pieceIdx, int newRow, int newCol);
bool isValidQueenMove(int pieceIdx, int newRow, int newCol);
bool isValidKingMove(int pieceIdx, int newRow, int newCol);
bool isCastlingMove(int pieceIdx, int newRow, int newCol);
void updatePieceMovement(float dt);
void updatePieceCapture(float dt);
public:
ChessGame();
virtual ~ChessGame();
void initialize();
void update(float dt);
void render();
void release();
ChessBoard *getBoard() {
return chessBoard;
}
void onSelection(float row, float col);
void onSelection(float row, float col, bool flag);
bool getBoardHighlight(int row, int col) {
return chessBoard->getHighlight(row, col);
}
bool isValidMove(int pieceIdx, int newRow, int newCol);
void setCurrentState(char state) {
gameState = state;
};
char getCurrentState() {
return gameState;
}
char getOppState() {
return oppsiteState;
};
void setOppState(char state) {
oppsiteState = state;
};
char getCurrentMoveColor() { return currentMoveColor; };
void setCurrentMoveColor(char color) {
currentMoveColor = color;
};
void setGameColor(char color) {
gameColor = color;
};
char getGameColor() {
return gameColor;
}
bool getIsTure() {
return isTurn;
};
};
#endif // __CHESS_GAME_H__