How do I detect and rotate a square? I have recently written code for detecting path colors in a 2D grid and printing the corresponding path costs and it works fine for me.
Here's my code; I am currently new to OpenCV and know only C++ (no python).

The image that I had for the project was:



Now I may get an image like this for the same task:



I need an algorithm to detect the square in the middle of the arena, rotate it to the angle by which it becomes like the image above, and then crop the rest of the image off.

I am currently at a fix about doing this.
Since you asked for algorithms, I suggest you look at the Hough transform and other techniques for finding lines in an image.  I suspect this will help you determine where the lines are, where the intersections are, and how much to rotate to recover the original image.