Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
key_generator.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <bitset>
7#include <iomanip>
8#include <iostream>
9#include <random>
10#include <sstream>
11#include <type_traits>
12
13namespace boardstate {
14
16template <typename IntType>
18public:
20 : seed_{std::random_device{}()}
21 , prng_{seed_} {}
22
24 : seed_{seed}
25 , prng_{seed_} {}
26
32 IntType GenerateKey() {
33 IntType result = 0;
34 auto bits_per_block = 8 * sizeof(uint32_t);
35 auto blocks_per_int = sizeof(IntType) / sizeof(uint32_t);
36 for (auto idx = 0; idx < blocks_per_int; ++idx) {
37 result <<= bits_per_block;
38 result |= static_cast<IntType>(prng_() & 0xFFFFFFFF);
39 }
40 return result;
41 }
42
43 std::random_device::result_type seed() { return seed_; }
44
45private:
46 std::random_device::result_type seed_;
47 std::mt19937 prng_;
48};
49
51template <typename IntType>
53public:
54 IntType GenerateKey() {
55 IntType result = 0;
56 auto bits_per_block = 8 * sizeof(std::random_device::result_type);
57 auto blocks_per_int = sizeof(IntType) / sizeof(std::random_device::result_type);
58 for (auto idx = 0; idx < blocks_per_int; ++idx) {
59 result <<= bits_per_block;
60 result |= static_cast<IntType>(rd_());
61 }
62 return result;
63 }
64
65private:
66 std::random_device rd_;
67};
68
69template <typename IntType>
70std::string IntToHexString(IntType num) {
71 static_assert(
72 std::is_unsigned<IntType>::value,
73 "IntType must be an unsigned integer type."
74 );
75 static_assert(
76 sizeof(IntType) % sizeof(std::random_device::result_type) == 0,
77 "IntType must be a multiple of the size of std::random_device::result_type."
78 );
79
80 constexpr size_t bits_per_block = 8 * sizeof(std::random_device::result_type);
81 constexpr size_t bits_per_hex_digit = 4; // Each hex digit represents 4 bits
82 constexpr size_t hex_digits_per_block =
83 bits_per_block / bits_per_hex_digit; // Hex digits per block
84 constexpr size_t total_blocks =
85 (sizeof(IntType) * 8 + bits_per_block - 1) /
86 bits_per_block; // Calculate the number of 32-bit blocks needed
87
88 std::ostringstream oss;
89
90 oss << "0x";
91 for (int i = total_blocks - 1; i >= 0; --i) {
92 // Extract each 32-bit block
93 uint32_t block = static_cast<uint32_t>(num >> (i * bits_per_block));
94 // Print the block, handling leading zeros within each block
95 oss << std::hex << std::setfill('0') << std::setw(hex_digits_per_block) << block;
96 }
97 return oss.str();
98}
99
100template <typename IntType>
101void PrintHex(IntType num) {
102
103 auto hex_string = IntToHexString(num);
104 std::cout << hex_string << std::endl;
105
106 // constexpr int bits_per_block = 8 * sizeof(std::random_device::result_type);
107 // constexpr int bits_per_hex_digit = 4; // Each hex digit represents 4 bits
108 // constexpr int hex_digits_per_block =
109 // bits_per_block / bits_per_hex_digit; // Hex digits per block
110 // constexpr int total_blocks =
111 // (sizeof(IntType) * 8 + bits_per_block - 1) /
112 // bits_per_block; // Calculate the number of 32-bit blocks needed
113
114 // std::cout << "0x";
115 // for (int i = total_blocks - 1; i >= 0; --i) {
116 // // Extract each 32-bit block
117 // uint32_t block = static_cast<uint32_t>(num >> (i * bits_per_block));
118 // // Print the block, handling leading zeros within each block
119 // std::cout << std::hex << std::setfill('0') << std::setw(hex_digits_per_block)
120 // << block;
121 // }
122 // std::cout << std::dec << std::endl; // Reset to decimal output
123}
124
125} // namespace boardstate
Generates pseudorandom integers.
IntType GenerateKey()
Generates a pseudorandom IntType value using mt19937.
std::random_device::result_type seed_
std::random_device::result_type seed()
Generates random integers (not pseudorandom) using std::random_device.
Calculate / manage board state and associate Minimax results.
std::string IntToHexString(IntType num)
void PrintHex(IntType num)