-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.h
More file actions
132 lines (109 loc) · 2.4 KB
/
chess.h
File metadata and controls
132 lines (109 loc) · 2.4 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
#ifndef __CHESS
#define __CHESS
#include "arduino.h"
#include "controller.h"
#include "input_processor.h"
#include "display.h"
#include "menu.h"
enum PieceType {
none = 0,
pawn,
knight,
bishop,
rook,
queen,
king,
__count,
} __attribute__((packed));
enum Side {
white = 0,
black = 1,
} __attribute__((packed));
struct Position {
int8_t x() {
return x_;
}
int8_t y() {
return y_;
}
void set_x (int8_t val) {
x_ = val;
}
void set_y (int8_t val) {
y_ = val;
}
bool operator==(Position other) {
return (other.x() == x() && other.y() == y());
}
bool operator!=(Position other) {
return !(operator==(other));
}
Position(int8_t x, int8_t y) {
x_ = x;
y_ = y;
}
private:
int8_t x_;
int8_t y_;
};
struct Piece {
Side side() {
return (Side)((data >> 4) & 0x1);
}
PieceType type() {
return (PieceType)(data & 0xf);
}
uint8_t color() {
return type();
}
Piece(Side side, PieceType type) {
data = ((side & 0x1) << 4) | (type & 0xf);
}
Piece() : Piece(Side::white, PieceType::none) {}
private:
uint8_t data;
};
struct Board {
Piece board[8][8];
Position origin = Position(1,6);
Side turn = Side::white;
bool game_over = false;
int8_t en_passant_column = -1;
uint8_t castling_pieces_moves = 0; //bit vector
int animation_time = 2000; //milliseconds
float max_dim = 0.6;
int width(){
return sizeof(*board)/sizeof(**board);
}
int height(){
return sizeof(board)/sizeof(*board);
}
Piece piece(Position pos) {
return board[pos.y()][pos.x()];
}
bool square_threatened(Position pos);
bool valid_castle(int column);
bool valid_move(Position start, Position end);
bool line_is_empty(Position start, Position end);
void move(Position start, Position end);
void draw(Display& disp);
Board();
};
class Chess: public InputProcessor {
public:
Display& disp;
Controller *controllers;
int controller_count;
Chess(Display& disp, Controller *controllers, uint8_t controller_count);
static MenuOption menuOption();
static void setPalette(Display& disp);
static bool run(Display& disp, Controller *controllers, uint8_t controller_count);
bool play();
private:
bool handle_button_down(Controller::Button button, uint8_t controller_index);
Board board;
bool selected = false;
Position selected_pos = Position(0,0);
Position cursor_pos = Position(4,4);
};
#endif