-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameValidator.cpp
More file actions
83 lines (69 loc) · 2.51 KB
/
Copy pathgameValidator.cpp
File metadata and controls
83 lines (69 loc) · 2.51 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
//
// Created by Benedikt Hornig on 19.09.22.
//
#include <string>
#include <filesystem>
#include <iostream>
#include <fstream>
#include "Agents/ZobristHash.h"
void validateGame(const std::filesystem::directory_entry &path) {
ZobristHash hash = ZobristHash();
Board oldBoard;
Board board = Board();
Hash hashValue = hash.initialHash(board);
std::string line;
std::ifstream gameFile (path);
bool isBoardStr = true;
while (getline(gameFile, line)) {
if (isBoardStr) {
// test board parsing + execution of moves
std::string boardStr = board.getBoardNotation();
if(boardStr != line) {
throw std::runtime_error("Board notation in file " + std::string(path.path()) + " has failed");
}
} else {
// test move parsing
Move move = Move::fromNotation(line);
std::string moveStr = std::string(move);
if(moveStr != line) {
throw std::runtime_error("Move notation in file " + std::string(path.path()) + " has failed");
}
BoardState bs1 = board.getBoardState();
board.makeMove(move);
// test undo move
board.undoMove();
BoardState bs2 = board.getBoardState();
if(bs1 != bs2) {
throw std::runtime_error("Undo operation in file " + std::string(path.path()) + " has failed");
}
board.makeMove(move);
// test hash creation
hashValue = hash.continuousHash(hashValue, oldBoard, move);
Hash initHash = hash.initialHash(board);
if(hashValue != initHash) {
throw std::runtime_error("Hash generation in file " + std::string(path.path()) + " has failed");
}
oldBoard = board;
}
isBoardStr = !isBoardStr;
}
}
int main() {
// validate game moves with games played with a random Ludii agent
std::string gamesDir = "../Game Files/";
int gameNum = 0;
for (const auto &entry: std::filesystem::directory_iterator(gamesDir)) {
if (entry.path().string() == "../Game Files/.DS_Store")
continue;
gameNum++;
if (gameNum % 1000 == 0)
std::cout << "### Game number: " << gameNum << std::endl;
try {
validateGame(entry);
} catch (std::runtime_error& error) {
std::cout << error.what() << std::endl;
}
}
std::cout << "All games validated" << std::endl;
return 0;
}