- Connect Four AI
- 25 Dec 2015 11:19:39 am
- Last edited by lirtosiast on 25 Dec 2015 08:44:21 pm; edited 1 time in total
My interest in Connect Four was captured by the recent contest by jonbush and earthnite. Although I wasn't able to compete due to finals, I'm still planning to make an AI in pure 84 BASIC, and I want it to be really good—it probably won't play perfectly, but it should be fairly close.
Right now I'm still at the planning stage, and trying to find out approach is best.
Earlier, I had planned a simple minimax algorithm to 3 ply, with a heuristic based on the number and quality of threats (threes-in-a-row). With 7 options for each move, that's 343 board position; if I want the AI to take about one minute per move, I'd need to evaluate each board position in 175 ms—and that's not counting overhead. That's a very tight time constraint, and I don't want my AI to take more than about a minute per move. Precomputation could help some (for example, remembering the result of the same board position created by making moves in a different order reduces the number of positions to 238). However, it's probably not possible to make this approach work.
The other approach I was thinking about was entirely rule-based, like jonbush's test AI for the contest. Rather than searching through a game tree, the AI would just entirely follow rules like "move in the center", or "block the opponent's threat". However, this seems like it would be a difficult model to use to cover all possible strategies, and although it wouldn't be as much of a waste of processing power as minimax, it would be a pain to write.
I'm looking for something in the middle, with advantages of both approaches—something that I can improve by giving it more processing time or speeding up the algorithm, but that isn't so ad-hoc or tedious.
Regardless of the approach, my current strategy looks like this:
* Take a winning move
* Block the other player
* Keep the other player from having a forced win in 3 moves...
* Create an odd threat (threat in an odd row), by taking any even squares available to make an odd threat
* Keep the opponent from getting an even threat in the same column as, and lower than, my odd threat. Do this by grabbing squares that the opponent can possibly use to establish an even threat.
* Move in the center column, because every nonvertical winning possibility requires a center space. Expert Play in Connect Four also states that d3, d4, and d5 are crucial squares.
* Maybe an opening book? I don't know how much it would help, though, seeing as there are plenty of forced wins in the opening that such a simple strategy won't be able to solve anyway.
Oh, I forgot to mention: For simplicity, I'll make this for the 1st player, and then expand the code to play as 2nd player sometime later.
I'd appreciate thoughts or advice.
Right now I'm still at the planning stage, and trying to find out approach is best.
Earlier, I had planned a simple minimax algorithm to 3 ply, with a heuristic based on the number and quality of threats (threes-in-a-row). With 7 options for each move, that's 343 board position; if I want the AI to take about one minute per move, I'd need to evaluate each board position in 175 ms—and that's not counting overhead. That's a very tight time constraint, and I don't want my AI to take more than about a minute per move. Precomputation could help some (for example, remembering the result of the same board position created by making moves in a different order reduces the number of positions to 238). However, it's probably not possible to make this approach work.
The other approach I was thinking about was entirely rule-based, like jonbush's test AI for the contest. Rather than searching through a game tree, the AI would just entirely follow rules like "move in the center", or "block the opponent's threat". However, this seems like it would be a difficult model to use to cover all possible strategies, and although it wouldn't be as much of a waste of processing power as minimax, it would be a pain to write.
I'm looking for something in the middle, with advantages of both approaches—something that I can improve by giving it more processing time or speeding up the algorithm, but that isn't so ad-hoc or tedious.
Regardless of the approach, my current strategy looks like this:
* Take a winning move
* Block the other player
* Keep the other player from having a forced win in 3 moves...
* Create an odd threat (threat in an odd row), by taking any even squares available to make an odd threat
* Keep the opponent from getting an even threat in the same column as, and lower than, my odd threat. Do this by grabbing squares that the opponent can possibly use to establish an even threat.
* Move in the center column, because every nonvertical winning possibility requires a center space. Expert Play in Connect Four also states that d3, d4, and d5 are crucial squares.
* Maybe an opening book? I don't know how much it would help, though, seeing as there are plenty of forced wins in the opening that such a simple strategy won't be able to solve anyway.
Oh, I forgot to mention: For simplicity, I'll make this for the 1st player, and then expand the code to play as 2nd player sometime later.
I'd appreciate thoughts or advice.