Now the criticism:
In the role of the script interpreter, you are using the third version of Python. At the same time, your code contains the xrange function built into the second version of python. In the third version, this function has been removed and replaced with the range function. Hence, the code must end in error.
The second feature, which cannot leave you indifferent, is simply prohibitively huge declaration of global variables. You are simply abusing the "global" function.
This fact indicates a low level of the programmer.
Computer science and programming are exact disciplines. Use as little ambiguity as possible in your code. Your code is similar to the paragraph from the history textbook, but not computer science.
Almost all of the code is puzzling.