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.
 
Hmm it looks like a Uni course application you're developing. You'll need to work out the algorithm yourself as we all have todo that at some point :D

To help you to help yourself - try breaking the AI description into a set of numbered steps to make it easier to work with.
 
There are other (more simple) ways to make a crude AI for Noughts and Crosses. In short:

Check to see if your opponent has two in a row and the third empty. (i.e. They could win next turn). If so block then. Otherwise pick a random place to go.

Thats a crap AI, but a few improvements and you can make it learn for itself! Just remember the random choices and bias to them for example. Maybe even genetic algorthms! Loads of fun :)
 
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 did exactly the same thing last term in my course! We had to make a 2 player game and also a single player one.

PM my trust and I could show you my coding to get an idea. My method is slightly different to yours because of the way we were taught but our lecturer gave us loads of help.
 
Last edited:
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] != ' ')
 
Last edited:
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