-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.cpp
More file actions
121 lines (96 loc) · 2.74 KB
/
logic.cpp
File metadata and controls
121 lines (96 loc) · 2.74 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "logic.h"
#include <iostream>
namespace gamecore {
Logic::Logic(const int code_color_count, const int color_count_max)
: color_count_(color_count_max),
code_count_(code_color_count)
{
}
const std::vector<int>& Logic::CreateNewCode(const bool unique)
{
// Êîëè÷åñòâî öâåòîâ äîëæíî áûòü áîëüøå êîëè÷åñòâà çíà÷åíèé.
// Äîïóñêàåòñÿ èíîå òîëüêî åñëè öâåòà íå óíèêàëüíûå.
code_.clear();
color_position_.clear();
code_.resize(code_count_, -1);
color_position_.resize(color_count_, -1);
std::mt19937 generator;
// âåêòîð èç ñûðîé ïîñëåäîâàòåëüíîñòè íàïîëíåííîé äóáëèêàòàìè
const std::vector<int> raw_colors = GenerateInt(generator, color_count_ - 1);
int i = 0, j = 0;
while (i < code_count_ && j < raw_colors.size()) {
if (code_[i] == -1 && (unique && color_position_[raw_colors[j]] == -1)) {
color_position_[raw_colors[j]] = i;
code_[i++] = raw_colors[j++];
}
else {
if (unique && (color_position_[raw_colors[j]] != -1)) {
j++;
}
else {
color_position_[raw_colors[j]] = i;
code_[i++] = raw_colors[j++];
}
}
}
if (j == raw_colors.size()) throw std::logic_error::logic_error("Wrong random generator logic.");
return code_;
}
const std::vector<int>& Logic::GetClorPosition() const
{
return color_position_;
}
const std::vector<int>& Logic::GetCode() const
{
return code_;
}
std::vector<Compliance> Logic::GetCodeBreake(const std::vector<int>& code, const bool matching_positions)
{
std::vector<Compliance> result(code_count_, Compliance::None);
for (int i = 0; i < code_count_; i++) {
int color = code[i];
int position = color_position_[color];
if (position == i) {
result[i] = Compliance::Full;
}
else if (position != -1) {
if (matching_positions) {
result[i] = Compliance::Partial;
}
else {
result[i] = Compliance::Partial;
}
}
else {
result[i] = Compliance::None;
}
}
if (!matching_positions) {
std::sort(result.begin(), result.end(), [](const Compliance lhs, const Compliance rhs)
{
return static_cast<int>(lhs) < static_cast<int>(rhs);
});
}
return result;
}
int Logic::GetColorCount()
{
return color_count_;
}
int Logic::GetNumCount()
{
return code_count_;
}
std::vector<int> Logic::GenerateInt(std::mt19937& generator, const int color_count, const bool unique_color)
{
int max = color_count * 10;
std::vector<int> nums(max);
const Clock::time_point start_time_ = Clock::now();
auto msec = std::chrono::duration_cast<std::chrono::microseconds>(start_time_.time_since_epoch());
generator.seed(msec.count());
for (int i = 0; i < max; ++i) {
nums[i] = std::uniform_int_distribution<int>(0, color_count)(generator);
}
return nums;
}
} //namespace gamecore