简体   繁体   中英

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++. 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. 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. 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.). 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> .

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 ;

Also std::unordered_map might work.

( 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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