-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArvore.cpp
More file actions
184 lines (161 loc) · 5.16 KB
/
Copy pathArvore.cpp
File metadata and controls
184 lines (161 loc) · 5.16 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Arvore.hpp"
#include <iostream>
#include <limits>
#include <fstream>
#include <memory>
#include <sstream>
using namespace std;
ArvoreEstados::ArvoreEstados(const EstadoJogo &estado_inicial)
{
raiz = new NoArvore(estado_inicial);
no_atual = raiz;
hashes_conhecidos.insert(raiz->estado.gerar_hash());
cout << "Arvore de estados iniciada. Estado inicial registrado.\n";
}
ArvoreEstados::~ArvoreEstados()
{
destruir_recursivamente(raiz);
}
void ArvoreEstados::destruir_recursivamente(NoArvore *no)
{
if (!no)
return;
for (NoArvore *filho : no->filhos)
{
destruir_recursivamente(filho);
}
delete no;
}
const EstadoJogo &ArvoreEstados::get_estado_atual() const
{
return no_atual->estado;
}
bool ArvoreEstados::adicionar_jogada(const EstadoJogo &novo_estado)
{
size_t hash_novo = novo_estado.gerar_hash();
if (hashes_conhecidos.count(hash_novo))
{
cout << "Aviso: Esta jogada resulta em um estado que já existe na arvore. Acao nao permitida.\n";
return false;
}
NoArvore *novo_no = new NoArvore(novo_estado, false);
novo_no->pai = no_atual;
no_atual->filhos.push_back(novo_no);
no_atual = novo_no;
hashes_conhecidos.insert(hash_novo);
return true;
}
bool ArvoreEstados::voltar_jogada()
{
if (no_atual->pai != nullptr)
{
no_atual = no_atual->pai;
cout << "Jogada desfeita. Retornando ao estado anterior.\n";
return true;
}
cout << "Nao e possivel voltar. Você esta no estado inicial da rodada.\n";
return false;
}
void ArvoreEstados::exportar_no_recursivamente(NoArvore *no, ofstream &arquivo) const
{
if (!no)
return;
const EstadoJogo &estado = no->estado;
const Jogador &jogador_da_vez = (estado.turno % 2 == 0) ? estado.jogador1 : estado.jogador2;
stringstream label_stream;
label_stream << "Turno: " << estado.turno << " (" << jogador_da_vez.nome << ")\\n"
<< "--------------------------\\n"
<< estado.jogador1.nome << " | Mão: " << estado.jogador1.mao.size()
<< " | Camelos: " << estado.jogador1.camelo_count() << " | Pts: " << estado.jogador1.pontos
<< " | Bônus: " << estado.jogador1.fichas_bonus
<< " | Selos: " << estado.jogador1.selos_excelencia << "\\n";
label_stream << " Mão: [";
for (size_t i = 0; i < estado.jogador1.mao.size(); ++i)
{
label_stream << estado.jogador1.mao[i].nome()[0];
if (i < estado.jogador1.mao.size() - 1)
{
label_stream << ", ";
}
}
label_stream << "]\\n";
label_stream << estado.jogador2.nome << " | Mão: " << estado.jogador2.mao.size()
<< " | Camelos: " << estado.jogador2.camelo_count() << " | Pts: " << estado.jogador2.pontos
<< " | Bônus: " << estado.jogador2.fichas_bonus
<< " | Selos: " << estado.jogador2.selos_excelencia << "\\n";
label_stream << " Mão: [";
for (size_t i = 0; i < estado.jogador2.mao.size(); ++i)
{
label_stream << estado.jogador2.mao[i].nome()[0];
if (i < estado.jogador2.mao.size() - 1)
{
label_stream << ", ";
}
}
label_stream << "]\\n";
label_stream << "--------------------------\\n"
<< "Mercado: ";
for (const auto &carta : estado.mercado.getCartas())
{
label_stream << carta.nome()[0] << " ";
}
label_stream << "\\n"
<< "Baralho: " << estado.baralho.tamanho() << " cartas";
if (no->eh_hipotetico)
{
arquivo << " \"" << no << "\" [label=\"" << label_stream.str() << "\", style=dashed, color=gray];\n";
}
else
{
if (no == no_atual)
{
arquivo << " \"" << no << "\" [label=\"" << label_stream.str() << "\", style=filled, color=lightblue];\n";
}
else
{
arquivo << " \"" << no << "\" [label=\"" << label_stream.str() << "\"];\n";
}
}
for (NoArvore *filho : no->filhos)
{
if (filho->eh_hipotetico)
{
arquivo << " \"" << no << "\" -> \"" << filho << "\" [style=dashed, color=gray];\n";
}
else
{
arquivo << " \"" << no << "\" -> \"" << filho << "\";\n";
}
exportar_no_recursivamente(filho, arquivo);
}
}
void ArvoreEstados::exportar_para_dot(const string &nome_arquivo) const
{
ofstream arquivo(nome_arquivo);
if (!arquivo.is_open())
{
cout << "Erro ao abrir o arquivo para exportacao." << endl;
return;
}
arquivo << "digraph ArvoreDeEstados {\n";
arquivo << " node [shape=box];\n";
exportar_no_recursivamente(raiz, arquivo);
arquivo << "}\n";
arquivo.close();
cout << "Arvore exportada para " << nome_arquivo << endl;
}
void ArvoreEstados::adicionar_filhos_hipoteticos(const vector<EstadoJogo> &estados_filhos)
{
for (const auto &estado : estados_filhos)
{
size_t hash_novo = estado.gerar_hash();
if (hashes_conhecidos.count(hash_novo) == 0)
{
NoArvore *novo_no = new NoArvore(estado, true);
novo_no->pai = no_atual;
no_atual->filhos.push_back(novo_no);
hashes_conhecidos.insert(hash_novo);
}
}
cout << estados_filhos.size() << " caminhos alternativos foram explorados e adicionados a arvore.\n";
}