Estoy tratando de modelar el estado en el que estaríamos y los movimientos que podemos hacer en un árbol binario como el siguiente:
10
/ \
6 14
/ \ / \
5 8 11 18
Utilizo el código dado por Alex Allain aquí. Entonces he hecho el siguiente código:
#define NULL 0
#include <vector>
using namespace std;
struct node
{
int key_value;
node *left;
node *right;
};
//es una tabula de movimientos posibles. Toma un nodo y un nodo de un nodo dado en una Junta
struct Move{
vector<node> moves[2];
};
// Muestra el estado actual de un juego
class Board{
public:
Board();
~Board();
Board(btree,node);
Move getLegalMoves(node); //Número de movimientos que se pueden realizar desde la posición actual
int legalMoves(Move); //movimientos que se pueden realizar desde la posición actual
private:
//member variables
int length;
int score;
node n; // Dónde estamos en el momento de la junta
};
Board::Board(btree b, node n){
length = NULL;
score = n.key_value; //DONE? : numero del node actual
Move moves[2];
if(n->*left != NULL)
moves[0] = n->*left;//tabula de las posiciones izquierda y derecha del estado actual
moves[1] = n->*right;
};
Pero me contesto el compilador lo siguiente :
mike@mike-thinks:~/NestedMonteCarlo/NestedForBinaryTrees$ g++ main.cpp -o NestedMonteCarlo
main.cpp: In constructor ‘Board::Board(btree, node)’:
main.cpp:164:9: error: ‘left’ was not declared in this scope
if(n->*left != NULL)
^
main.cpp:166:18: error: ‘right’ was not declared in this scope
moves[1] = n->*right;
^
main.cpp: In member function ‘int Board::legalMoves(Move)’:
main.cpp:173:9: error: ‘left’ was not declared in this scope
if(n->*left != NULL){// wrong we have to take move into account
^
main.cpp: In member function ‘Move Board::getLegalMoves(node)’:
main.cpp:184:9: error: ‘left’ was not declared in this scope
if(n->*left != NULL)
^
main.cpp:185:3: error: ‘moves’ was not declared in this scope
moves[0] = n->*left;//DONE? array of the left and right positions from curren
^
main.cpp:186:3: error: ‘moves’ was not declared in this scope
moves[1] = n->*right;
^
main.cpp:186:18: error: ‘right’ was not declared in this scope
moves[1] = n->*right;
...
Sin embargo, pensé que me aseguraba de que estuviese dentro del alcance al escribir en un node n
en la partida privada de la clase para mostrar dónde estamos en el árbol binario
El código completo esta en un repositorio GitHub.
Sirve para saber donde puedo ir o cuantas posibilidades tengo desde un nodo. Por ejemplo desde 14
tenemos dos posibilidades pero desde 11
tenemos 0.
// gives the number of leaves we can play : either 2 or 0 in the binary-tree case.
int Board::legalMoves(Move moves){
//If there is leaves there is at least two moves.
if(n.left != NULL){// wrong we have to take move into account
return 2;
}//otherwise there is no possibilities
else{
return 0;
}
};
// gives the number of leaves we can play : either 2 or 0 in the binary-tree case.
Move Board::getLegalMoves(node n){
if(n.left != NULL)
moves[0] = n.left;//DONE? array of the left and right positions from current state //node *left; and node *right;
moves[1] = n.right;
};
main
.node* n
y nonode n
, al intentar llamar a los nodos q apunta simplemente aln->left