Cops and Robber

Turn-based 2d platform chasing game.

by Exeami

java Game LibGDX

Source Code

https://github.com/AmithaLingala/cops-n-robbers

Background

I did game development back in 2016 for university mini projects. At that time I used Contruct2 which did not require much coding. I enjoyed developing games and wanted to try that again. When I had to decide on the final semester project during masters, It was a simple choice to develop a game. I decided to use Java as I did not work much on it before this project. After few considerations on game develoment framework, I decided to use LibGDX as it is open-source, cross-platform Java game development framework with very good documentation.

Overview

Cops and Robber is a turn-based 2d platform game where multiple cops try to capture a robber. Robber can collect coins to increase the game score and can collect weapons to use them to freeze cops or destroy obstacles. Currently, there are 10 levels with increasing difficulty.

Implementation details

Planning the level generation and cop's algorithm was main in this project. The remaining bits like updating score, weapons, coins, robber's moment were simple to implement.

Cop's Algorithm

I planned to use BFS for the cop’s algorithm but, as we have multiple cops and the shortest path sometimes returns the same path for all the cops, I decided to fix that. The solution was to sort the cops based on the distance from the robber. Traverse through the sorted list of cops and call BFS for the shortest path to the first cop. From the next cop, we remove the path of the first cop from our graph and continue. This way, the nearest cop will move toward the robber and other cops will take a detour.

Level generation

As I planned to have increased difficulty for each level and do want to randomize the map every time we play. I have to figure out the position of obstacles, coins, robber and cop.

The idea is to increase the number of obstacles as we level up. But after few levels, as the gird size stays same, the game had too many obstacles and less room to move. So, I decided to increase them only till level 5. To implement the above, I have taken variable "prob" which is the minimum of threshold level (5) and the current level. Then, I randomly selected an integer from 1 to 100 and check if it is less than two times of variable ”prob”. If yes, then the cell will be an obstacle. The above makes sure the number of obstacles is random, increasing for every level until level 5 and won’t cover more than 10% of the map. Now, I have set walls on all sides and set the gate in the top left corner of the map.

For generating cops and Robber, I have divided the grid map into five divisions. The robber generator will randomly select an empty cell (not an obstacle) in the given division. For the first six levels, the robber position will be in the middle division and from level seven to level ten, that will be in the bottom right to increase the game difficulty. The current level number and cop number (two cops for the first nine levels and three cops for the 10th level) decides the division for the cop. We also have a check to avoid the case where the cop gets blocked by obstacles.

The number of coins is twice the current level number and, the number of weapons is a random number from 1 to the current level number. The above makes sure that the items are spread throughout the map and motivates to increase the count of collectables for later levels.

Conclusion

Overall, It was a very good experience and interesting project. I enjoyed playing it in my mobile. I plan to add more features and levels to this game in future.

Screenshots

Game Screen
Frozen Cop
Highlighted cells where robber can drop bomb
Mobile View
Next level screen
Game end screen
Comment via email