简体   繁体   English

井字游戏图设计?

[英]Tic-tac-toe game graph design?

this is a general question from my homework, I am just asking for some ideas or pseudo code. 这是我作业中的一个普遍问题,我只是在问一些想法或伪代码。

Suppose I am constructing a tic-tac-toe game using C++. 假设我正在使用C ++构建井字游戏。 What I already have is a Node class which contains a const member data represents the current game board state, and an const array of pointer to other Nodes that contains every possible next-step state. 我已经拥有一个Node类,其中包含一个const成员数据,代表当前游戏板状态,以及一个指向其他Node的const指针数组,其中包含每个可能的下一步状态。 So this will be a directed graph with out duplicated nodes(every node has its unique game state.). 因此,这将是一个没有重复节点的有向图(每个节点都有其独特的游戏状态)。

I got a trouble when I am trying to generate such a graph. 尝试生成这样的图形时遇到麻烦。 It seems like I need to use recursion because all data members in Node class are const so there's no way to change them around. 似乎我需要使用递归,因为Node类中的所有数据成员都是const,因此无法更改它们。 And I can hardly come up with a good idea to generate such a graph at once without Node duplicated(I mean it will be easy to make it a tree, but it waste lots of space and time.). 而且,如果没有Node的重复,我很难想出立即生成这样一个图的好主意(我的意思是将它做成树很容易,但是会浪费很多空间和时间。) One thing may help is I am able to compare two different game states, and I don't think I am allowed to use any templet other than <set> . 可能有帮助的一件事是,我能够比较两个不同的游戏状态,并且我认为除<set>之外,我不允许使用其他模板。

So if anyone got any idea about this, please free to write down your thought or pseudo code. 因此,如果有人对此有任何想法,请随意写下您的想法或伪代码。 Thank you 谢谢

I think you need is to check for existence: 我认为您需要检查是否存在:

std::set< Node > checked_node;

bool isNodeChecked( Node ){
    return checked_node.find( Node )!=checked_node.end();
}

You need to overload something like operator = and operator < for std::set to work on your class Node ; 您需要重载类似operator =operator <类的东西,以使std::set在您的class Node上工作;

Also std::unordered_map might work. 另外std::unordered_map可能也可以。

( Although as it seems to me that a tic-tac-toe status can be represented by non-negative integers from 0 to 3^9. In this way a std::set would suffice but you need extra encode decode functions. (虽然在我看来,井字游戏状态可以由0到3 ^ 9的非负整数表示。这样,std :: set就足够了,但是您需要额外的编码解码功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM