C++ Noughts and Crosses AI?

Associate
Joined
17 Nov 2008
Posts
1,680
I coded this game today, and I want to code in an AI which would play against the player, but have no idea how to go about it. I did a quick search and came across this, trouble is I don't know how to code it.

Here is my game without the AI. Thanks for any assistance.
 
I missed a key step, first do the same check I described but to check if you can win, if so do that. Then check to see if they can win and block, otherwise random.

Start with something real simple. Take it slow and make sure you have old versions you can go back to, so you can test improvements. Start stupidly simple, you have a "Have I won/lost" function and so make a "Place random" function. Take it from there. Try a "Am I about to win/lose" function. Then think about the weighted system talked about in that link.

From there you can get AI systems to play against each other which is always fun!

EDIT: Just looked at your code, I think there is a bug in the check_win function. Your third if statement reads:

if (board[0][0] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != ' ')

and it should be:

if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != ' ')
Yup I noticed this just before I handed it in thankfully :D. Thanks for the guidance.
 
Back
Top Bottom