-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_generate_output.cpp
More file actions
68 lines (54 loc) · 1.84 KB
/
Copy path5_generate_output.cpp
File metadata and controls
68 lines (54 loc) · 1.84 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
#include <string>
#include <fstream>
#include <iostream>
#include "include/move.h"
std::string move_to_uci(move m) {
std::string lookup_files = "abcdefgh";
// sanity checks to avoid out-of-bounds indexing and badly-formatted output
auto in_bounds = [](int x) { return 0 <= x && x < 8; };
if (!in_bounds(m.from_col) || !in_bounds(m.to_col) || !in_bounds(m.from_row) || !in_bounds(m.to_row)) {
return ""; // invalid move coordinates
}
// 1. Convert columns (files)
char letter_from = lookup_files[m.from_col];
char letter_to = lookup_files[m.to_col];
// 2. Convert rows (ranks: 0..7 → '1'..'8')
char rank_from = '1' + m.from_row;
char rank_to = '1' + m.to_row;
// 3. Build base string "e2e4"
std::string uci;
uci += letter_from;
uci += rank_from;
uci += letter_to;
uci += rank_to;
// 4. Add promotion letter if needed
if (m.promotion != NONE) {
switch (m.promotion) {
case QUEEN: uci += 'q'; break;
case ROOK: uci += 'r'; break;
case BISHOP: uci += 'b'; break;
case KNIGHT: uci += 'k'; break; // Project-specific: 'k' for knight (per prof's spec)
default: return ""; // Unknown promotion code
}
}
return uci;
}
bool write_move_to_file(move m, std::string path) {
// 1. Convert move to UCI string
std::string uci = move_to_uci(m);
if (uci.empty()) {
std::cerr << "Error: Could not encode move to UCI (invalid move)." << std::endl;
return false;
}
// 2. Open the output file (overwrite mode)
std::ofstream file(path);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << path << std::endl;
return false;
}
// 3. Write the move
file << uci << std::endl;
// 4. Close the file
file.close();
return true;
}