-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.cpp
More file actions
231 lines (181 loc) · 5.11 KB
/
Chess.cpp
File metadata and controls
231 lines (181 loc) · 5.11 KB
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
#include "conio.hh"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "Chess.h"
#include "colors.h"
const int A = 97;
const int D = 100;
const int W = 119;
const int S = 115;
const int quit = 81;
const int P = 112;
const int O = 111;
const int SPACE = 32;
const int kBoardSize = 10;
const int kFieldSize = 8;
const int kBorderDist = 2;
const int delay = 1000 / 25; // 25 fps
#define null 0
struct Vector
{
int x;
int y;
bool operator==(const struct Vector &v) const {
return v.x == this->x && v.y == this->y;
}
};
bool isQuit = false;
int kWhiteTeam = 0;
int kBlackTeam = 1;
int kPawn = 1;
int kBishop = 2;
int kKnight = 3;
int kRook = 4;
int kQueen = 5;
int kKing = 6;
typedef struct Unit{
int side; // 0(white) or 1(black)
int type; // Pawn, Bishop, kNight, Rook, Queen, King
const char* emoji;
int getTeamColor() {
return side == kWhiteTeam ? kfCyan : kfMagenta;
}
} Unit;
Unit whitePawn = {kWhiteTeam, kPawn, "♟"};
Unit whiteBishop = {kWhiteTeam, kBishop, "♝"};
Unit whiteKnight = {kWhiteTeam, kKnight, "♞"};
Unit whiteRook = {kWhiteTeam, kRook, "♜"};
Unit whiteQueen = {kWhiteTeam, kQueen, "♛"};
Unit whiteKing = {kWhiteTeam, kKing, "♚"};
Unit blackPawn = {kBlackTeam, kPawn, "♙"};
Unit blackBishop = {kBlackTeam, kBishop, "♗"};
Unit blackKnight = {kBlackTeam, kKnight, "♘"};
Unit blackRook = {kBlackTeam, kRook, "♖"};
Unit blackQueen = {kBlackTeam, kQueen, "♕"};
Unit blackKing = {kBlackTeam, kKing, "♔"};
Unit* map[8*8] = {
&whiteRook, &whitePawn, null, null, null, null, &blackPawn, &blackRook,
&whiteKnight, &whitePawn, null, null, null, null, &blackPawn, &blackKnight,
&whiteBishop, &whitePawn, null, null, null, null, &blackPawn, &blackBishop,
&whiteQueen, &whitePawn, null, null, null, null, &blackPawn, &blackQueen,
&whiteKing, &whitePawn, null, null, null, null, &blackPawn, &blackKing,
&whiteBishop, &whitePawn, null, null, null, null, &blackPawn, &blackBishop,
&whiteKnight, &whitePawn, null, null, null, null, &blackPawn, &blackKnight,
&whiteRook, &whitePawn, null, null, null, null, &blackPawn, &blackRook
};
Vector cursor = {0, 0};
int kNormalCursorColor = kbGreen;
int kGrapCusrsorColor = kbYellow;
int isCursorGraping = 0;
bool renderBorder(int x, int y) {
bool isBorder = x == 0 || y == 0 || x == kBoardSize-1 || y == kBoardSize-1;
if (!isBorder) {
return false;
}
int bgColor = !((x/kBorderDist+y/kBorderDist)%2) ? kbBlue : kbCyan;
printf("\x1b[%dm%s\x1b[0m", bgColor, " ");
return true;
}
Unit* getUnitFromBoard(int x, int y) {
return map[(y - 1) * kFieldSize + (x - 1)];
}
bool renderUnit(int x, int y, int bgColor) {
Unit* pUnit = getUnitFromBoard(x, y);
if (pUnit == null) return false;
printf("\x1b[%d;%dm%s\x1b[0m", pUnit->getTeamColor(), bgColor, pUnit->emoji);
return true;
}
bool isEmpty(int x, int y) {
return getUnitFromBoard(x, y) == null;
}
void renderSpace(int x, int y, int bgColor) {
printf("\x1b[%dm%s\x1b[0m", bgColor, " ");
}
long getTimeMs() {
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000 + t.tv_usec/1000;
}
int cursorTime = 10;
int stopTime = getTimeMs();
int getBgColor(int x, int y) {
Vector position = {x, y};
if (position == cursor) {
if (cursorTime > 0){
cursorTime--;
if (cursorTime == 0){
stopTime = getTimeMs();
}
return isCursorGraping ? kGrapCusrsorColor : kNormalCursorColor;
} else if ((getTimeMs() - stopTime) % 1000 < 500){
return isCursorGraping ? kGrapCusrsorColor : kNormalCursorColor;
}
}
return ((x+y)%2) ? kbWhite : kbBlack;
}
void render() {
for (int y=0; y<kBoardSize; y++) {
for (int x=0; x<kBoardSize; x++) {
if (renderBorder(x, y)) continue;
int bgColor = getBgColor(x - 1, y - 1);
if (renderUnit(x, y, bgColor)) continue;
renderSpace(x, y, bgColor);
}
printf("\n");
}
}
int getKeyInput()
{
if (kbhit() == 1)
return getchar();
else
return 0;
}
void waitMs(int milliseconds)
{
usleep(milliseconds * 1000);
}
void updateCursor(int dx, int dy){
struct Vector c = {cursor.x + dx, cursor.y + dy};
// cursor.x -= cursor.x + dx != -1 && cursor.x + dx != kFieldSize && cursor.y + dy != -1 && cursor.y + dy != kFieldSize;
if (0<=c.x && c.x<kFieldSize && 0<= c.y && c.y<kFieldSize) {
cursor.x = c.x;
cursor.y = c.y;
}
cursorTime = 20;
}
int updateKey() {
int key = getKeyInput();
switch (key) {
case A:
updateCursor(-1, 0);
break;
case D:
updateCursor(+1, 0);
break;
case W:
updateCursor(0, -1);
break;
case S:
updateCursor(0, 1);
break;
}
return key;
}
int game(){
do{
if (updateKey() == quit){
isQuit = true;
continue;
}
system("clear");
render();
waitMs(delay);
} while (!isQuit);
return 0;
}
int main()
{
return game();
}