How to apply A* algorithm to a Tic Tac Toe game? - algorithm

My teacher asked me to write a Tic Tac Toe game using A* algorithm. I don't know how I can use A* in my game. Is it possible to check winner using A*? Or can it be used for something else?

you should extend you game,the grid graph should not be 3x3,may be 20x20.
and give a initial situation,you should quickly work out the least steps that you can win the game,its like a dfs problem,then you can use A*,because A* is for optimization problem.

Related

How to find the complexity of decision trees in Tic Tac Toe by hand?

I know the upper bound for the size of the game tree is 9! = 362,880 in a 3X3 Tic Tac Toe. After deducted the invalid case and the rotation and reflection, only 26,830 possible games are left. So complexity of decision trees in 3X3 Tic Tac Toe is 5 which is the number of digit of the leaf nodes (26,830). Am I conclusion right?
If so, how can i calculate a decision tree complexity of 4X4 Tic Tac Toe without drawing out a full decision tree?
Sorry for my dump question
You probably want to use some kind of model-checker, that can count the solutions, e.g. a #SAT solver https://en.wikipedia.org/wiki/Sharp-SAT. I don't believe there is any trick possible beyond exploring the state space, apart from using symmetries (but that only alleviates the exploration).
This is like the N-queens problem https://en.wikipedia.org/wiki/Eight_queens_puzzle there is no analytical solution for the number of solutions when you scale the board up.

Best approach for an optimal score in game

In the Android app store there is a two player game called Box Me which is what we used to play when growing up with pen and paper. Each player takes turns to join adjacent dots vertically or horizontally. The objective is to connect a grid of adjacent dots to form a box, while not allowing your opponent to form a box. In the game we play against a bot. For example, a board of 8x8 dots can form 7x7 boxes, 49. I'm trying to implement an approach (and would appreciate any help) to a high score(maximum number of boxes). Or is there a way to prove that this is futile against a knowledgeable opponent.
A few things that you must know if you are starting with game (bot player) programming:
Basic graph algorithms (BFS, DFS, Top Sort etc)
Min - Max algorithm
Alpha Beta Pruning
Concept of heuristics to approximate (and learn to improve) a solution.
As I guess, you must be knowing basic graph algorithms, and for the use case that you mention here, Min-Max (using heuristics of course), should solve your problem.
Alpha Beta Pruning is (kind of) an advanced concept for improving time complexity after implementation of Min-Max.
There are other important concepts to Game Programming that include machine learning, recording played games and using those results etc. You can learn about them later. :)

Pathfinding Algorithms

I am new to game programming I currently am using unity's 2D power to remake an 2D game for sake of practice. So I am just little confuse about one thing, so far every enemy in my game was dumb(they move on predefined paths) but this enemy, alien-copter, flies and follow the player wherever the player goes. My question is should I implement any pathfinding algorithm? I had studied A* but I trying to figure out if A* is gonna be helpful in my envoirnment because player will be moving and the enemy have to keep on looking for shortest path. I tried to write code for this AI my code was working perfect when there were no obstacles but my game has obstacles so I want something efficient so what do you people think should I implement A* or any other algorithm or not?
As regards AI, if you have obstacles in your game, you will need to implement pathfinding of some sort. Note, that just taking the shortest block (node) is not an option because such algorithm is not complete, i.e. the path may not be found even if there is one. Imagine a going after b:
(wall)(wall)(wall)
(free)(free)a (wall) b
(wall)(wall)(wall)
The shortest (best) path is to the left since up, down and right are blocked. On the next move however, the best move is to go right because it's closer. That's how a will get trapped in a loop. So yes you are right, you do need to get the path from a to b. It will typically be in the form of a list of nodes. Then you just select head (aka element at index 0) of the list.
A* is the perfect option in your case. It is complete, efficient and it will even give you the shortest path. Path recomputation shouldn't be an issue for a small game. Furthermore, if obstacles in your game grid are static for the duration of the game you can precompute paths and cache them. For instance, path from (x,y) to (x1,y1) is [(a,b), (c,d) ...]. So you can store it in some sort of map data structure where key is the two points - start, target and value - the path. This will of course depend on whether you want to classify an enemy as an obstacle for other enemies and other gameplay factors

Minimax and tic tac toe - is my algorithm correct?

I implemented a minimax algorithm for TTT. When I make the AI player make the first move, it evaluates all minimax values of the possible moves as 0. That means it can choose any square on the grid as the first move. However, any Tic Tac Toe guide will tell you that choosing a corner or center square when making the first move is the better choice since there is a higher chance of winning.
Why is my algorithm not reflecting this?
EDIT: To clarify, what I'm trying to ask is: is this a limitation of the minimax algorithm or is my implementation incorrect?
Your algorithm does not have to reflect this: if you try out all starting positions, you would find that the corners and the center give you more path to win than other cells.
With tic-tac-toe's lack of complexity, the minimax can look ahead all the way through the end of the game, starting with the very first move. The number of available moves goes down quickly as you progress through the game, so the full search finishes pretty fast.
With more complex games (othello, checkers, chess), the so-called "opening books" become more important. The number of available moves in the beginning of the game is enormous, so a traditional approach is to pick a move from the "book" of openings, and stay with pre-calculated "book moves" for the first three to six plays. Moves outside the book are not considered, saving a lot of CPU on moves that remain essentially the same.

8-direction path finding algorithm

I'm having trouble finding the right pathfinding algorithm for some AI I'm working on.
I have players on a pitch, moving around freely (not stuck to a grid), but they are confined to moving in 8 directions (N NE E etc.)
I was working on using A*, and a graph for this. But I realised, every node on the graph is equally far apart, and all the edges have the same weight - since the pitch is rectangular. And the number of nodes is enormous (being a large pitch, with them able to move between 1 pixel and another)
I figured there must be another algorithm, optimised for this sort of thing?
I would break the pitch down into 10x10 pixel grid. Your routing does not have to be as finely grained as the rest of your system and it makes the algorithm take up far less memory.
As Chris suggests above, choosing the right heuristic is the key to getting the algorithm working right for you.
If players move in straight lines between points on your grid, you really don't need to use A*. Bresenham's line algorithm will provide a straight line path very quickly.
You could weight a direction based on another heuristic. So as opposed to weighting the paths based on actual distance you could weight or scale that based on another factor such as "closeness to another player" meaning players will favour routes that will not collide with other players.
The A* algorithm should work well like this.
I think you should try Jump Point Search. It is a very fast algorithm for path finding on 8-dire.
Here is a blog describing Jump Point Search shortly.
And, this is its academic paper <Online Graph Pruning for Pathfinding on Grid Maps>
Besides, there are some interesting videos on Youtube.
I hope this helps.

Resources