Imagine a game played on a 10x10 grid system where a player can move up down left or right and imagine there are two players on this grid: An enemy and you. In this game, there are walls on the grid which you can't go through. The objective of this game is to block the enemy in so he can't move around the rest of the board and is effectively "trapped".

I want to write an algorithm that detects which nodes on the board I as a player need to put blocks in, in order to trap the enemy. There are also some other considerations to think about. You have to be able to place the blocks before the enemy place can get out of the box. Also note more thing: You can move AND place a block in the position that you're moving to at the same time.

Here's a picture as an example of the game.

enter image description here

EDIT: note that the board in the picture is 5x5, but that's okay for the purposes of the example

In this example, I could go up, then right and place a block, then right and place a block, then up and place a block. If there's more than one way of blocking off the enemy, then I should use the way that's going to give my enemy the least amount of space.

Researching on google couldn't find me anything relevant, although it may have been because I wasn't using relevant search terms. I also thought about using a monte Carlo search tree algorithm for simultaneous games, but I would need to research into that more.

  • $\begingroup$ Any progress on this problem statement as I'm looking for an algorithm to do the same? I've already implemented A* algorithm to find the shortest path to the enemy but not sure how to go about trapping the enemy in its own space. $\endgroup$ Nov 6, 2021 at 15:40
  • $\begingroup$ @SakthiSomaskandan Nope, I ended up giving up on it. It seemed like any solution I would end up with was either a heuristic or its runtime was unacceptable for my time constraints. Good luck with your own work. Please let me know if you find something promising in your research. I'm still curious about this. $\endgroup$
    – Ahmed
    Nov 16, 2021 at 17:24
  • $\begingroup$ The standard algorithm for this kind of game (no different from Chess) is a tree search. $\endgroup$
    – user253751
    Mar 31, 2023 at 18:45

1 Answer 1


If I've understood the logic correctly, the player tries to build a wall such that the enemy cannot reach the player in any way. This can normally be determined by some path-finding algorithms, Dijkstra Shortest Path algorithm being a reasonable choice for the setting and grid-size. This algorithm explores the possible paths from a starting point to one or multiple end points, and usually returns the shortest path to the point(s). If there is no path to the mentioned point, it will not return anything, and you will know the two points are blocked from reaching each other.

Of course, if I've truly understood the rules correctly, the bigger question is how to avoid having the player just build a wall around themselves to block of the enemy instead, which is likely going to be trivially easy. And additionally rule of requiring the player to be in the "room" with the bigger area after a room has been blocked off (Something which coincidentally also could be done by extending the Dijkstra's algorithm a bit with custom logic)

  • $\begingroup$ Dijkstra's would give you the shortest path between the starting point and the end point, but the problem is that you don't know what the end point is. You are actually searching for the end point. Also, OP mentions that the algorithm should block off the enemy by allowing him as little space as possible. If you just build a wall around yourself the you allow the enemy the largest space possible. $\endgroup$
    – pi-tau
    Jul 29, 2023 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .