我试图让一个算法为一个tic tac toe游戏制作DFA。有了我现在的代码,有重复的状态。我创建了一个查找函数,以便在添加它之前查看状态是否已经存在,并将已经创建的状态添加到父状态的子矢量中。
我已经尝试过实现它,但是在我实现它之前,我仍然得到了相同数量的节点,这意味着它永远找不到具有相同位置的另一个状态'数组,它是假设的。我不知道自己做错了什么。它从不返回任何东西。
更新:我知道它很快就会回归并且没有通过所有元素,但我不知道如何让它通过所有状态并让它停止过早返回。
更新2.0:所以我已经改变了查找的位置,它现在在我的Main.cpp文件中。我也改变了它的经历,但它仍然没有正确返回。
有什么想法吗?这是我的代码:
States.cpp
#include "States.hpp"
State::State(){
final = false;
reject = false;
draw = false;
for (int i = 0; i < 9; i++)
_position[i] = 2;
firstPlayerMove = true;
_children = new vector<State *> ();
};
void State::setStateChildren(State *s){
_children->push_back(s);
}
vector<State*> *State::getChildren(){
return _children;
}
void State::setFirstMove(bool b){
firstPlayerMove = b;
}
bool State::firstMove(){
return firstPlayerMove;
}
void State::setPosition(int p[]){
for (int i = 0; i < 9; i++){
_position[i] = p[i];
}
}
int *State::position(){
return _position;
}
bool State::isFinal(){
return final;
}
bool State::isReject(){
return reject;
}
Main.cpp的
#include <iostream>
#include <vector>
#include "States.hpp"
using namespace std;
void dependents(State *root, State *state, bool first);
int size (State *s);
bool win(int p[], int symbol);
bool draw(int p[]);
void CreateDFA(State *root, State *state, int n){
if (n == 0 || state == nullptr)
return;
if (state->isFinal() || state->isReject() || state->isDraw())
return;
dependents(root, state, state->firstMove());
for (vector<State*>::iterator iter = state->getChildren()->begin(); iter != state->getChildren()->end(); iter++){
CreateDFA(root, *iter, n - 1);
}
}
void dependents(State *root, State *state, bool first){
int symbol;
if (first)
symbol = 1;
else
symbol = 0;
int count = 0;
while (count < 9){
if (state->position()[count] == 2 && state->position()[count] != 1 && state->position()[count] != 0){
int check[9];
for(int i = 0; i < 9; i++)
check[i] = state->position()[i];
check[count] = symbol;
State *child = root->find(root, check);
if (child == nullptr){
child = new State();
child->setPosition(state->position());
child->setFirstMove(!(state->firstMove()));
child->position()[count] = symbol;
child->setFinal(win(child->position(), 1));
child->setReject(win(child->position(), 0));
child->setDraw(draw(child->position()));
}
state->setStateChildren(child);
}
count++;
}
}
bool win(int p[], int symbol){
if(p[0] == symbol && p[1] == symbol && p[2] == symbol)
return true;
if(p[0] == symbol && p[4] == symbol && p[8] == symbol)
return true;
if(p[0] == symbol && p[6] == symbol && p[3] == symbol)
return true;
if(p[1] == symbol && p[4] == symbol && p[7] == symbol)
return true;
if(p[2] == symbol && p[4] == symbol && p[6] == symbol)
return true;
if(p[2] == symbol && p[5] == symbol && p[8] == symbol)
return true;
if(p[3] == symbol && p[4] == symbol && p[5] == symbol)
return true;
if(p[6] == symbol && p[7] == symbol && p[8] == symbol)
return true;
return false;
}
bool draw(int p[]){
if(p[0] != 2 && p[1] != 2 && p[2] != 2 && p[3] != 2 && p[4] != 2 && p[5] != 2 && p[6] != 2 && p[7] != 2 && p[8] != 2){
cout << "Set Draw" << endl;
return true;
}
return false;
}
int size (State *s){
if (s == nullptr)
return 0;
int count = 0;
for(vector<State*>::iterator iter = s->getChildren()->begin(); iter != s->getChildren()->end(); iter++)
count = size(*iter) + count;
return count + 1;
}
bool check(int p[], int a[]){
for(int i = 0; i < 9; i++)
if (p[i] != a[i])
return false;
return true;
}
State *find(State * root, int p[]){
if (root->isFinal() || root->isReject() || root->isDraw())
return nullptr;
State * s = nullptr;
for (vector<State*>::iterator iter = root->getChildren()->begin(); iter != root->getChildren()->end(); iter++){
if(check((*iter)->position(), p)){
s = new State (**iter);
break;
}
else
s = find((*iter), p);
}
return s;
}