#include <iostream>
#include <vector>
#include <random>
#include <cstdint>
#include <conio.h>
#include <algorithm>
#include <string>
typedef std::vector<std::uint64_t> DType;
DType MakeNumber(std::size_t N) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> U(1, 34);
//std::uniform_int_distribution<> U(1, 9);
DType R;
for (std::size_t i = 0; i < N; i++) R.push_back(U(mt));
return R;
}
DType DoRight(DType D) {
std::vector<std::size_t> Idx;
std::random_device rd;
static std::mt19937 mt(rd());
int S[]{ 0,1 };
for (std::size_t i = 0; i < D.size(); i++) {
if (D[i] > 1 && D[i] < 9) Idx.push_back(i);
}
if (Idx.size() == 0) return D;
std::shuffle(Idx.begin(), Idx.end(), mt);
std::shuffle(S, S+2, mt);
D[Idx.front()]--;
D.insert(D.begin() + (Idx.front() + S[0]), 1);
return D;
}
DType DoLeft(DType D) {
std::vector<std::size_t> Idx;
std::random_device rd;
std::mt19937 mt(rd());
int S[]{ -1,1 };
bool F = false;
for (std::size_t i = 0; i < D.size(); i++) {
if (D[i] > 1 && D[i] < 9) Idx.push_back(i);
}
if (Idx.size() == 0) return D;
std::shuffle(Idx.begin(), Idx.end(), mt);
std::shuffle(S, S + 2, mt);
F = false;
for (std::size_t j = 0; j < Idx.size(); j++) {
for (std::size_t i = 0; i < std::size(S); i++) {
if (Idx[j] == 0 && S[i] == -1)continue;
if (Idx[j] == D.size() - 1 && S[i] == 1)continue;
if (D[Idx[j] + S[i]] >= 9)continue;
if (D[Idx[j] + S[i]] == 1)continue;
if (D[Idx[j]] == 0)continue;
if (D[Idx[j]] + D[Idx[j] + S[i]] >= 9)continue;
D[Idx[j]] += D[Idx[j] + S[i]];
D.erase(D.begin() + Idx[j] + S[i]);
F = true;
break;
}
if (F == true)break;
}
return D;
}
bool Show(DType & D, bool F = false) {
std::string s = "0123456789ABCDEFGHIJKLNMOPQRSTUVWXYZ";
for (auto& o : D) {
//std::cout << "["<<o<<"]";
std::cout << s[o];
}
if (F) {
std::cout << " \r";
}
else {
std::cout << std::endl;
}
return true;
}
bool MakeHoge(std::size_t N) {
DType D = MakeNumber(N);
int k = 0;
Show(D);
while (k != 27) {//esc?
k = _getch();
if (k == 's')D = DoRight(D);
if (k == 'a')D = DoLeft(D);
Show(D);
}
return true;
}
int main() {
MakeHoge(16);
return 0;
}