Let us say the status of a Connect4 game is stored like 12341, which means player 1 dropped his ball in position 1, and then player 2 in position 2, player 1 in position 3, player 2 in position 4 and player 1 in position 1 again.
In this format, is there an algorithm that can know if a game is won, in a way that is better than converting into a 2-d matrix and using the algorithms that is already listed in SO, like Connect 4 check for a win algorithm?
The characteristics of the game connectivity are such that the most effective method is to convert to the 2D paradigm to check for wins. The fastest method for most people is to check the current move in all directions, to see whether it just created a win.
You can somewhat improve the "intelligence" of the checking, by keeping a list of "live" lines -- possible future wins -- and checking to see which ones the most recent move extends or blocks.
Note that this is a less obvious approach for both coding and maintenance. Also, it's slower than checking the current move in all directions. It's really useful only if used to create an automatic player (AI).
Related
Hi everyone i'm working in a tile-based game engine and i got a bit stuck with units movement. Overall you can think it as a Tower Defense game where units have to follow a path (brown tiles), here comes the example image:
As you can see there are numbers in the Tiles with represent the drawing order. So when the map is initalized i take all the tiles that are of the type 1 (brown) and add them to an ArrayList.
Problem is that those ArrayList Tiles are in the drawing order. So my Units will start moving from tile 58 cause is the first tile from the drawing order.
How would you order the brown tiles starting from left (nº97) till right (nº118)?
I can think about taking the first tile that is the 97 by knowing that is the one with the lowest X position but from there what?
I think you're formatting your information in a strange way to solve this problem. If your units are starting at tile 58, then somewhere you're telling them to start at the first drawn tile? Optimally, if you want to have different maps, wouldn't you want to be able to define a particular point on the grid and tell them to start from there?
(Without knowing more about your code structure, it's hard to say how you'd do that.)
Once you've defined a point for them to start from and a point for them to end on, you could then use A* or D* search to define a path for the entities to take along your grid. That would also give you a lot more options in your design-- you could have the player modify the grid throughout the game and force the units to take a new path.
is there a way of getting the winning move for AI on TicTacToe using for loop. So checking if any two buttons next to each other have the value X or O then make the third make the same value to make the AI win.
Using a for loop to go through array of 3 by 3 buttons and checking if any two buttons next to each other has the same value.
I have tried this but not sure if its correct because it isn;t working the computer doesn't make any winning move.
For easier understanding, you could make multiple loops for row/column/diagonal each:
Count X or O in one row/column/diagonal, if it equals 2, add the third one in the remaining field (if it is empty)
There are MANY ways to accomplish what you want. You could even bruteforce every possible move, count the winning results and chose the one with the most possibilities.
Another easy way would be to write a method which will check for every field if it blocks an opponent winning move and/or results in a winning move for oneself.
A common A.I. algorithm in gaming is The Min/Max Algorithm Basically, a player looks ahead to evaluate the state of the game resulting from every possible sequence of events, and chooses the move such as to maximize their chances of winning.
For tic-tac-toe you may want to consider starting with a player and looking at all of the possible child states that could follow a given state. You could evaluate some score such as whether the move leads you to a state where you have 2 x's lined up. You then propagate this score up your tree, so the current player has an informed decision to make.'
Min-max assumes your opponent is playing perfectly, so you can sometimes encounter problems there.
For a good description of A.I. and the tic-tac-toe problem check out Artificial Intelligence: A Modern Approach Chapter 5, adversarial search, covers gaming and specifically refers to tic-tac-toe.
I'm currently dabbling in Java AI programming, and trying an AI challenge. In the challenge, my AI is given 2 seconds to respond to the new game state. If these two seconds are exceeded without producing a response, my AI forfeits. The game consists of a grid with goals and enemies, each enemy being an independent AI generated by the game. I have implemented a standard A* to find the nearest available goal.
I would like my A* algorithm to increase the cost of squares near an enemy that could potentially prove dangerous, thus avoiding dangerous paths. I am considering a two-dimensional array containing the estimated loss of health for each square, limited to calculating within ~2 squares of each enemy (~5x5). Each turn, for each enemy this array would have a 5x5 square set to 0 and recalculated.
Assuming I write code that only does what it must and moves on... Will a two dimensional array of between 20x20 and 100x100 elements significantly effect execution time? Is a two dimensional array of estimated threat per square a good method of calculating cost in an A* algorithm so as to avoid enemies?
UPDATE:
I got it working absolutely perfectly. The cost function I used:
For each enemy
Calculate manhattan distance
If 0 or 1, cost += absolute(enemy health - health) / 5
Else if 2, cost += absolute(enemy heath - health) / 10
Else cost += 0
Using it, I saw some really impressive pathfinding and moves; the bot would often take calculated risks where there were no other moves to get to a goal, but basically avoided enemies otherwise. I was thoroughly impressed with how insignificant the performance cost was of adding the heuristic. It's not a perfect solution for the game, but it showed me how robust A* can be.
A* is generally used for pathfinding, but I'm going to modify it for the purpose of a game state lookahead. I'm pretty sure that turns it into a minimax algorithm.
If you are only calculating the content of your array once and the calculation
for each cell is something simple like checking a few adjacent cells for enemies then
a 100x100 array is no work at all relative to your time constraints.
Given the information in your post it sounds like a good idea to me.
I have to implement a Reversi game for Android. I have managed to implement all the game, is functional, but the problem is that I don't have an AI. In fact, at every move the computer moves in the position that achieves him the highest number of pieces.
I decided to implement and alpha-beta pruning algorithm. I did a lot of research on the internet about it, but I couldn't come to a final conclusion how to do it. I tried to implement a few functions, but I couldn't achieve the desired behaviour.
My board is stored in class Board (inside this class, the pieces occupied by each player are stored in a bi-dimensional int array). I have attached an small diagram (sorry about the way it looks).
DIAGRAM: https://docs.google.com/file/d/0Bzv8B0L32Z8lSUhKNjdXaWsza0E/edit
I need help to figure out how to use the minimax algorithm with my implementation.
What I understood so far, is that I have to make an evaluation function regarding the value of the board.
To calculate the value of the board I have to account the following elements:
-free corners (my question is that I have to take care only about the free corners, or the one that I can take at the current move?! dilemma here).
-mobility of the board: to check the number of pieces that will be available to move, after the current move.
-stability of the board… I know it means the number of pieces that can't be flipped on the board.
-the number of pieces the move will offer me
I have in plan to implement a new Class BoardAI that will take as an argument my Board object and the dept.
Can you please tell me a logical flow of ideas how I should implement this AI?
I need some help about the recursion while calculating in dept and I don't understand how it calculates the best choice.
Thank you!
First you can check this piece of code for a checkers AI that I wrote years ago. The interesting part is the last function (alphabeta). (It's in python but I think you can look at that like pseudocode).
Obviously I cannot teach you all the alpha/beta theory cause it can be a little tricky, but maybe I can give you some practical tips.
Evaluation Function
This is one of the key points for a good min/max alpha/beta algorithm (and for any other informed search algorithm). Write a good heuristic function is the artistic part in AI development. You have to know well the game, talk with expert game player to understand which board features are important to answer the question: How good is this position for player X?
You have already indicated some good features like mobility, stability and free corners. However note that the evaluation function has to be fast cause it will be called a lot of times.
A basic evaluation function is
H = f1 * w1 + f2 * w2 + ... + fn * wn
where f is a feature score (for example the number of free corners) and w is a corresponding weight that say how much the feature f is important in the total score.
There is only one way to find weights value: experience and experiments. ;)
The Basic Algorithm
Now you can start with the algorithm. The first step is understand game tree navigation. In my AI I've just used the principal board like a blackboard where the AI can try the moves.
For example we start with board in a certain configuration B1.
Step 1: get all the available moves. You have to find all the applicable moves to B1 for a given player. In my code this is done by self.board.all_move(player). It returns a list of moves.
Step 2: apply the move and start recursion. Assume that the function has returned three moves (M1, M2, M3).
Take the first moves M1 and apply it to obtain a new board configuration B11.
Apply recursively the algorithm on the new configuration (find all the moves applicable in B11, apply them, recursion on the result, ...)
Undo the move to restore the B1 configuration.
Take the next moves M2 and apply it to obtain a new board configuration B12.
And so on.
NOTE: The step 3 can be done only if all the moves are reversible. Otherwise you have to find another solution like allocate a new board for each moves.
In code:
for mov in moves :
self.board.apply_action(mov)
v = max(v, self.alphabeta(alpha, beta, level - 1, self._switch_player(player), weights))
self.board.undo_last()
Step 3: stop the recursion. This three is very deep so you have to put a search limit to the algorithm. A simple way is to stop the iteration after n levels. For example I start with B1, max_level=2 and current_level=max_level.
From B1 (current_level 2) I apply, for example, the M1 move to obtain B11.
From B11 (current_level 1) I apple, for example, the M2 move to obtain B112.
B122 is a "current_level 0" board configuration so I stop recursion. I return the evaluation function value applied to B122 and I come back to level 1.
In code:
if level == 0 :
value = self.board.board_score(weights)
return value
Now... standard algorithm pseudocode returns the value of the best leaf value. Bu I want to know which move bring me to the best leaf! To do this you have to find a way to map leaf value to moves. For example you can save moves sequences: starting from B1, the sequence (M1 M2 M3) bring the player in the board B123 with value -1; the sequence (M1 M2 M2) bring the player in the board B122 with value 2; and so on... Then you can simply select the move that brings the AI to the best position.
I hope this can be helpful.
EDIT: Some notes on alpha-beta. Alpha-Beta algorithm is hard to explain without graphical examples. For this reason I want to link one of the most detailed alpha-beta pruning explanation I've ever found: this one. I think I cannot really do better than that. :)
The key point is: Alpha-beta pruning adds to MIN-MAX two bounds to the nodes. This bounds can be used to decide if a sub-tree should be expanded or not.
This bounds are:
Alpha: the maximum lower bound of possible solutions.
Beta: the minimum upper bound of possible solutions.
If, during the computation, we find a situation in which Beta < Alpha we can stop computation for that sub-tree.
Obviously check the previous link to understand how it works. ;)
Firstly, this is AI for PacMan and not the ghosts.
I am writing an Android live wallpaper which plays PacMan around your icons. While it supports user suggestions via screen touches, the majority of the game will be played by an AI. I am 99% done with all of the programming for the game but the AI for PacMan himself is still extremely weak. I'm looking for help in developing a good AI for determining PacMan's next direction of travel.
My initial plan was this:
Initialize a score counter for each direction with a value of zero.
Start at the current position and use a BFS to traverse outward in the four possible initial directions by adding them to the queue.
Pop an element off of the queue, ensure it hasn't been already "seen", ensure it is a valid board position, and add to the corresponding initial directions score a value for the current cell based on:
Has a dot: plus 10
Has a power up: plus 50
Has a fruit: plus fruit value (varies by level)
Has a ghost travelling toward PacMan: subtract 200
Has a ghost travelling away from PacMan: do nothing
Has a ghost travelling perpendicular: subtract 50
Multiply the cell's value times a pecentage based on the number of steps to the cell, the more steps from the initial direction, the closer the value of the cell gets to zero.
and enqueue the three possible directions from the current cell.
Once the queue is empty, find the highest score for each of the four possible initial directions and choose that.
It sounded good to me on paper but the ghosts surround PacMan extremely rapidly and he twitches back and forth in the same two or three cells until one reaches him. Adjusting the values for the ghost presence doesn't help either. My nearest dot BFS can at least get to level 2 or 3 before the game ends.
I'm looking for code, thoughts, and/or links to resources for developing a proper AI--preferably the former two. I'd like to release this on the Market sometime this weekend so I'm in a bit of a hurry. Any help is greatly appreciated.
FYI, this was manually cross-posted on GameDev.StackExchange
If PacMan gets stuck in a position and starts to twitch back and forth then it suggests that the different moves open to him have very similar scores after you run your metric. Then small changes in position by the ghosts will cause the best move to flip back and forth. You might want to consider adding some hysteresis to stop this happening.
Setup: Choose a random move and record it with score 0.
For each step:
Run the scoring function over the available moves.
If the highest score is x% greater than the record score then overwrite the record score and move with this one.
Apply the move.
This has the effect that PacMan will no longer pick the "best" move on each step, but it doesn't seem like a greedy local search would be optimal anyway. It will make PacMan more consistent and stop the twitches.
Have a way to change PacMan into a "path following" mode. The plan is that you detect certain circumstances, calculate a pre-drawn path for PacMan to follow, and then work out early exit conditions for that path. You can use this for several circumstances.
When PacMan is surrounded by ghosts in three of the four directions within a certain distance, then create an exit path that either leads PacMan away from the ghosts or towards a power up. The exit situation would be when he eats the power up or ceases to be surrounded.
When PacMan eats a power up, create a path to eat some nearby ghosts. The exit situation would be when there are no ghosts on the path, recalculate the path. Or if there are no ghosts nearby, exit the mode entirely.
When there are less than half the dots left, or no dots nearby, enter a path to go eat some dots, steering clear of the ghosts. Recalculate the path when a ghost comes nearby, or exit it entirely if several ghosts are nearby.
When there are no situations which warrant a path, then you can revert back to the default AI you programmed before.
You can use Ant Colony Optimisation techniques to find shortest visible path that leads to many icons to eat or can get many score.
I don't know a lot about AI or specific algorithms, but here are some things you could try that might just get you close enough for government work :)
For the problem with ghosts surrounding him quickly, maybe the ghost AI is too powerful? I know that there's supposedly specific behaviors for each ghost in classical Pacman, so if you haven't incorporated that, you may want to.
To eliminate backtracking, you could create an weight penalty for recently traversed nodes, so he's less inclined to go back to previous paths. If that's not enough to kick him in one direction or another, then you can logarithmically increase the attraction penalty, so one path will become significantly more attractive than the other at a very quick rate.
For the problem of him getting caught by ghosts, you might be able to change from a general goal-based algorithm to an evasive algorithm once the ghosts have reached a dangerous node proximity.
You might benefit of knowing how the bots "reason" (as explained in this excellent dossier). For example, knowing the chase/scatter pattern of the ghosts will allow you to get the dots in "dangerous" locations, and so on.
I am adding this answer knowing that it's not the best solution you were looking for (since you wanted to deliver next week..) but maybe will be of use to somebody reading this in the future. Sortof a time capsule :)
You should check out this description of Antiobjects, which is the technique used by the Pacman ghosts to traverse the maze. In particular, note:
Each of these antiobjects or agents
has an identical and simple algorithm
which it runs at every turn of the
game. Instead of making Ghosts smart
enough to solve "shortest path"
problems around the maze, a notion of
"Pac-Man scent" is created instead and
each tile is responsible for saying
how much Pac-Man scent is on its tile.
So you consider a similar scent-based technique to control Pacman, perhaps where Pacman preferred traversing a path with a smaller amount of scent; this would reduce the chance of him going over old ground.