127
|
1 import std.stdio;
|
|
2 import std.traits;
|
128
|
3 import std.range;
|
|
4
|
|
5 enum Color {
|
|
6 White, Black
|
|
7 }
|
|
8
|
|
9 Color invert(Color c) {
|
|
10 return c == Color.White ? Color.Black : Color.White;
|
|
11 }
|
127
|
12
|
|
13 enum Piece {
|
|
14 King, Queen, Rook, Bishop, Knight, Pawn
|
|
15 }
|
|
16
|
128
|
17 string toString(Color color, Piece piece) {
|
127
|
18 string[6][2] table =
|
|
19 [
|
|
20 [ "\u2654", "\u2655", "\u2656", "\u2657", "\u2658", "\u2659" ],
|
|
21 [ "\u265A", "\u265B", "\u265C", "\u265D", "\u265E", "\u265F" ]
|
|
22 ];
|
|
23 return table[color][piece];
|
|
24 }
|
|
25
|
|
26 struct Square {
|
|
27 this(Color color_, Piece piece_) {
|
|
28 occupied = true;
|
|
29 color = color_;
|
|
30 piece = piece_;
|
|
31 }
|
|
32
|
|
33 bool occupied = false;
|
|
34 Color color;
|
|
35 Piece piece;
|
|
36 }
|
|
37
|
|
38 enum File {
|
|
39 _A, _B, _C, _D, _E, _F, _G, _H
|
|
40 }
|
|
41
|
128
|
42 char toChar(File f) {
|
|
43 char[8] table = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ];
|
|
44 return table[f];
|
|
45 }
|
|
46
|
127
|
47 enum Rank {
|
|
48 _1, _2, _3, _4, _5, _6, _7, _8
|
|
49 }
|
|
50
|
128
|
51 char toChar(Rank r) {
|
|
52 char[8] table = [ '1', '2', '3', '4', '5', '6', '7', '8' ];
|
|
53 return table[r];
|
|
54 }
|
|
55
|
|
56 struct Coord {
|
|
57 File file;
|
|
58 Rank rank;
|
|
59 }
|
|
60
|
|
61 struct Move {
|
|
62 Coord source;
|
|
63 Coord dest;
|
|
64 }
|
|
65
|
127
|
66 struct Board {
|
128
|
67 this(Board board, Move move) {
|
|
68 squares = board.squares;
|
|
69 apply(move);
|
|
70 }
|
|
71
|
|
72 Square square(Coord coord) const {
|
|
73 return squares[coord.file][coord.rank];
|
127
|
74 }
|
|
75
|
|
76 private:
|
128
|
77 Square * square(Coord coord) {
|
|
78 return &squares[coord.file][coord.rank];
|
|
79 }
|
|
80
|
|
81 void apply(Move move) {
|
|
82 auto sq1 = square(move.source);
|
|
83 auto sq2 = square(move.dest);
|
|
84
|
|
85 if (sq1.piece == Piece.Pawn && move.dest.file != move.source.file) {
|
|
86 // en-passant
|
|
87 }
|
|
88 else if (sq1.piece == Piece.King && move.dest.file - move.source.file > 1) {
|
|
89 // castle
|
|
90 }
|
|
91 else {
|
|
92 *sq2 = *sq1;
|
|
93 sq1.occupied = false;
|
|
94 }
|
|
95 }
|
|
96
|
127
|
97 Square[8][8] squares =
|
|
98 [
|
128
|
99 [ Square(Color.White, Piece.Rook), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Rook) ],
|
127
|
100 [ Square(Color.White, Piece.Knight), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Knight) ],
|
|
101 [ Square(Color.White, Piece.Bishop), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Bishop) ],
|
128
|
102 [ Square(Color.White, Piece.Queen), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Queen) ],
|
|
103 [ Square(Color.White, Piece.King), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.King) ],
|
127
|
104 [ Square(Color.White, Piece.Bishop), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Bishop) ],
|
|
105 [ Square(Color.White, Piece.Knight), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Knight) ],
|
128
|
106 [ Square(Color.White, Piece.Rook), Square(Color.White, Piece.Pawn), Square(), Square(), Square(), Square(), Square(Color.Black, Piece.Pawn), Square(Color.Black, Piece.Rook) ]
|
127
|
107 ];
|
|
108 }
|
|
109
|
128
|
110 void dump(in Board board) {
|
|
111 Color color = Color.White;
|
127
|
112
|
128
|
113 foreach_reverse(r; EnumMembers!Rank) {
|
|
114 write(toChar(r));
|
127
|
115 foreach(f; EnumMembers!File) {
|
128
|
116
|
|
117 if (color == Color.White) {
|
|
118 write("\033[47m");
|
127
|
119 }
|
|
120 else {
|
128
|
121 write("\033[40m");
|
|
122 }
|
|
123
|
|
124 Square square = board.square(Coord(f, r));
|
|
125 if (square.occupied) {
|
|
126 write(toString(square.color, square.piece));
|
127
|
127 }
|
128
|
128 else {
|
|
129 write(" ");
|
|
130 }
|
|
131
|
|
132 color = invert(color);
|
127
|
133 }
|
128
|
134 writeln("\033[0m");
|
|
135
|
|
136 color = invert(color);
|
127
|
137 }
|
|
138
|
128
|
139 write(" ");
|
127
|
140 foreach(f; EnumMembers!File) {
|
128
|
141 writef("%s", toChar(f));
|
127
|
142 }
|
|
143 writeln("");
|
|
144 }
|
|
145
|
|
146 void main(string[] args) {
|
|
147 Board board;
|
128
|
148 dump(board);
|
127
|
149 }
|