Skip to content

Commit d2bad9c

Browse files
optimized positionToString method
1 parent 8978e02 commit d2bad9c

File tree

1 file changed

+76
-74
lines changed

1 file changed

+76
-74
lines changed

src/Util.h

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,85 +6,87 @@
66
#include "Piece.h"
77

88
namespace Util {
9+
using namespace Pieces;
910

10-
using namespace Pieces;
11+
static std::string pieceToString(const Type type, const Color color) {
12+
std::string piece;
13+
switch (type) {
14+
case PAWN:
15+
piece = 'p';
16+
break;
17+
case KNIGHT:
18+
piece = 'n';
19+
break;
20+
case BISHOP:
21+
piece = 'b';
22+
break;
23+
case ROOK:
24+
piece = 'r';
25+
break;
26+
case QUEEN:
27+
piece = 'q';
28+
break;
29+
case KING:
30+
piece = 'k';
31+
break;
32+
case EMPTY:
33+
break;
34+
}
35+
if (color == WHITE) std::ranges::transform(piece, piece.begin(), ::toupper);
36+
return piece;
37+
}
1138

12-
static std::string pieceToString(const Type type, const Color color) {
13-
std::string piece;
14-
switch (type) {
15-
case PAWN:
16-
piece = 'p';
17-
break;
18-
case KNIGHT:
19-
piece = 'n';
20-
break;
21-
case BISHOP:
22-
piece = 'b';
23-
break;
24-
case ROOK:
25-
piece = 'r';
26-
break;
27-
case QUEEN:
28-
piece = 'q';
29-
break;
30-
case KING:
31-
piece = 'k';
32-
break;
33-
case EMPTY:
34-
break;
35-
}
36-
if (color == WHITE) std::ranges::transform(piece, piece.begin(), ::toupper);
37-
return piece;
38-
}
39+
static Square charToPiece(const char c) {
40+
Square p;
41+
const char C = c;
42+
switch (toupper(C)) {
43+
case 'P':
44+
p.type = PAWN;
45+
break;
46+
case 'N':
47+
p.type = KNIGHT;
48+
break;
49+
case 'B':
50+
p.type = BISHOP;
51+
break;
52+
case 'R':
53+
p.type = ROOK;
54+
break;
55+
case 'Q':
56+
p.type = QUEEN;
57+
break;
58+
case 'K':
59+
p.type = KING;
60+
break;
61+
default:
62+
p.type = EMPTY;
63+
break;
64+
}
65+
if (c >= 'a' && c <= 'z') p.color = BLACK;
66+
else p.color = WHITE;
3967

40-
static Square charToPiece(const char c) {
41-
Square p;
42-
const char C = c;
43-
switch (toupper(C)) {
44-
case 'P':
45-
p.type = PAWN;
46-
break;
47-
case 'N':
48-
p.type = KNIGHT;
49-
break;
50-
case 'B':
51-
p.type = BISHOP;
52-
break;
53-
case 'R':
54-
p.type = ROOK;
55-
break;
56-
case 'Q':
57-
p.type = QUEEN;
58-
break;
59-
case 'K':
60-
p.type = KING;
61-
break;
62-
default:
63-
p.type = EMPTY;
64-
break;
65-
}
66-
if(c >= 'a' && c <= 'z') p.color = BLACK;
67-
else p.color = WHITE;
68+
return p;
69+
}
6870

69-
return p;
70-
}
71-
72-
static std::string positionToString(const int_fast8_t position) {
73-
const int_fast8_t rank = 8 - (position / 8);
74-
const int_fast8_t file = position % 8;
75-
76-
const std::array<std::string, 8> files = {"a", "b", "c", "d", "e", "f", "g", "h"};
77-
78-
return files[file] + std::to_string(rank);
79-
}
80-
81-
static int_fast8_t stringToPosition(const std::string&position) {
82-
const int_fast8_t file = position[0] - 'a';
83-
const int_fast8_t rank = 8 - (position[1] - '0');
84-
return rank * 8 + file;
85-
}
71+
const std::array<std::string, 64> positionToStringMap = {
72+
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
73+
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
74+
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
75+
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
76+
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
77+
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
78+
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
79+
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"
80+
};
8681

82+
static std::string positionToString(const int_fast8_t position) {
83+
return positionToStringMap[position];
84+
}
8785

86+
static int_fast8_t stringToPosition(const std::string&position) {
87+
const int_fast8_t file = position[0] - 'a';
88+
const int_fast8_t rank = 8 - (position[1] - '0');
89+
return rank * 8 + file;
90+
}
8891
}
89-
9092
#endif //CHESSENGINE_UTIL_H

0 commit comments

Comments
 (0)