Best practice needed for implementing an AI for a turn-based game

Started by
5 comments, last by DarrylStein 11 years, 3 months ago

Hi,

I am dabbling into game making by implementing a boardgame (Hive) I have at home. I am doing this in C++ (with Qt) and have more less the basics I need for a player vs player game.

I will now try to make an AI for the game, but I've come to face with a couple of decisions and I wonder whether you guys could give me some advice?

My question boils to the fact whether I should make a separate "board" for the AI to evaluate and test its moves or to use some kind of undo function (so make all the moves and then move all the pieces to their original position).

Making a separate move does not appeal to me because it involves making deep copies of the objects (I only keep pointers to them, and the game logic depend on the pointers in lists). However, using an "undo" function, requires me to tell the game the AI is thinking, because right now any change to the internal game causes the GUI part to move as well.

What is common practice in this case?

Thanks in advance,

t3685

Advertisement

Your game state should be fairly simple to encapsulate; if it isn't, you should do that before you attempt any sort of AI. The complete representation of a board game's state should be a simple, relatively shallow object (ie. containing at most some arrays of basic data types) with a copy constructor.

For instance, a checkers game can at its simplest be represented with three 32-bit fields; the 8x4 valid board positions is 32 places, and the three fields store black's pieces, red's pieces, and kinged pieces. Your AI will need this "gamestate" object to evaluate options, and if your AI is recursive (some not-mutually-exclusive alternatives being strategy-based, heuristic-based, and table-based; but you can combine all four types easily) it will need to make at least one copy of this gamestate to recurse upon and evaluate its choices.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
If you are going to use some sort of adversarial search (minimax/alphabeta + extensions, etc) then it is common to have an undo move. This is because adversarial search is expensive and the cost of recreating the board for each node is higher than applying a move/undo on a single board
Having an `undo move' doesn't mean that you do the thinking on the same board structure that you use to represent the position to display to the user, and I actually don't recommend you do it that way: Whatever representation is convenient for displaying might not be optimal for the AI, so I would allow them to be different.

The best architecture I know for this problem is what chess programs have settled for after many years: The GUI and the AI (a.k.a., the "engine") run on separate processes, which communicate via a simple text-based protocol. This allows interoperability between GUIs and engines, so people can pick whatever combination suits them. It also makes debugging much easier, and you can easily run automated matches between engines by using a program that pretends to be the GUI, as far as the engines are concerned. Check out the Universal Chess Interface specification, to get a feel for what the protocol looks like (there is a competing protocol to use with XBoard, but I really don't like it).

Even if you don't go all the way to separating the AI into its own process, you should clearly specify what the interface to the AI is, and have the AI have its own copy of the board.

I should mention that besides minimax and its variations, there is another major class of algorithms that can be used for board games, generically known as Monte Carlo Tree Search. MCTS has been developed in the last decade and is currently the best approach to computer go and to several other games (I believe it's particularly well suited to connectivity games like hex).

One thing that makes MCTS very interesting to me is that it works well with a much larger class of games than minimax: It can take multiple players, games with randomness and games with simultaneous decisions with no problem, while these things are pretty much show stoppers for minimax. Dealing with hidden information is trickier, but even that can be accommodated.

I have heard of Hive, although I have never played it. So I don't know if MCTS is appropriate for this game or not.
Álvaro makes some very good points about architecting your AI.<br /><br />It certainly would be interesting to see how MCTS stacks up to minimax for Hive. Having played Hive, my gut feel is that minmax would still be preferable in this instance because the branching factor is not too large to be prohibitive (something that MCTS tends to handle better), but more importantly Hive can have quite tight tactical lines and I think this is the one area that MCTS can fall down in (one of the reasons MCTS hasn't stood up well to minimax in games like chess and arimaa).
Thanks for the advice guys. I will make a separate board object for the AI to work on. I'll probably do this by updating this object while updating the real board. Making a copy constructor is rather messy, because I have lists of pointers (for convenience sake) as data members. I will also implement a "undo" function (shouldn't be too hard :)).

I haven't thought it through, but I inclining toward minimax right now (that's the one they use for chess right? [edit: yes, yes it is]). From what I understand of Monte Carlo methods is that they can fickly in terms of parameters of the randomness. I'll keep you guys posted :)
One thing that I guess should have been mentioned as a big difference between MCTS and minimax is that minimax relies upon a good evaluation function and MCTS doesn't. This is the reason why MCTS excels in GO where it is difficult to evaluate a board position. I'm not sure what a good evaluation fucntion is going to be for hive (besides the obvious # pieces surrounding queen). Something to think about.

This topic is closed to new replies.

Advertisement