I have been trying to create a algorithm for the same program for a while now.
You are of course correct that first thing Your program should do, is to check if there is a way to form a 5 and win. And if there is not, the next should be to check if Your opponent can do that, and if yes, then defense.
How much have You played gomoku Yourself? How good grasp You have of the basics?
Ok, next step is to think: how we can get to the positions where we can win? Obviously, to win we must have four in a row. But it we just form four in a row like this:
__________
____XOOOO_
__________
Then opponent can close it.
But if we form "open four", like this:
__________
____OOOO__
__________
Then opponent cannot close both sides and You can win. So forming an open four is one way to win. Now comes the question: how can we form an open four? Surely, if we form "open three", like this:
__________
____OOO___
__________
Then opponent can block us:
___________
____XOOO___
___________
and we are back to the start.
To win, we can form two open threes at the same time:
____________
____OOO_____
_____O______
____O_______
Now if opponent blocks one of them, we can use the other to form an open four:
____________
_______O____
___XOOO_____
_____O______
____O_______
____________
and win:
________O___
_______O____
___XOOO_____
_____O______
____O_______
___X________
In gomoku terms, this is called 3x3, if You make two open threes at the same time.
Notice that both threes must be open: can You understand why?
There are other ways to win, too:
4x3: Do You see the winning move and why it is winning?
____________
__XOOO______
__XXXO______
____OX______
____________
4x4: See the winning move?
____________
__XOOO______
__XXXO______
__OXOX______
___O________
__X_________
These are just the basics of the game. Knowing the tactics helps You to think how to build the AI, so You can hard-code the principles.
Naturally, this is just the beginning. I would appreciate if You could try to implement this and then give feedback to me.
I have been trying to write the program in Java. Would You like to see the code I have done so You can playtest? It is not very good yet, but You could get new ideas from there. Although the comments and variable names are written in Estonian.. it might be very difficult to understand. :(