python creating a game from scratch in Pygame
- May 10
Thousands of years have been playing the game. With a large number of moves on the board is greater than the number of atoms in the universe, GO is a game of great complexity and a high level of abstraction arising from a set of simple rules. The Alphago program from DeepMind got into the headlines of newspapers, when in 2016 she beat the best player in the go to the Sedol in a match of five games .. Fulfilling small projects like this was a large part of my own development as a programmer, so I hope that you are that you You will like the article and you will feel inspiration to engage in your own projects! G - a game of seizure of territories.
5 basic rules of the game.
More details can be found here.
Design process
When working on a complex project, it is useful to break the problem into parts. I try to create encapsulated and modular programs with various functions for processing various parts of the task. In the case of Go, I decided that I had the following functions::
I create a Game class, which stores all data about this game, such as the condition of the boards and the number of captured stones for each player. The Game class has a DRAW () function for processing rendering the status of the board in the Pygame graphic interface and has the update () function to check the user actions, such as clicks with mouse and clicking.
The script below contains the full code of the GO game. Please note that this is only a basic implementation in which there are no many functions that you can add yourself.
Creating a game using Pygame
Pygame is a Python library for creating basic games with a graphic interface. It supports drawing figures on the screen, tracking the clicks of the mouse and keystrokes, reproduction of sounds and other operations expected in the game. After Pygame imports, you can use Pygame.init () to initialize the Pygame graphic interface. Other useful functions include pygame.mouse.get_pos () to obtain the current mouse position, Blit () to send the text to the screen and flip () to update the screen with new forms. The code above shows examples of how these Pygame functions work in practice.
Numpy operations magic
The board for playing in the th, as you probably already understood, is a two -dimensional matrix. The Numpy library has many very convenient functions for working with arrays and matrices.
To create evenly located net lines, I used the functions of NP.LinSpace () and NP.Full (). The first function, NP.Linspace (), creates a list of uniformly distributed values between a given initial and final value. Meanwhile, NP.Full () creates an array of the same values of the desired shape. This allows me to quickly generate a set of starting and endpoints for the line segments that form the grid of the Board, which I then convey to the pygame.draw.line () function.
Another extremely useful Numpy function is the NP.Where () function, which returns indices in the array, where a certain condition is met. For example, the line NP.Where (self.board == 1) returns positions on the board that stores value 1 (which means black stone in the rules of my game).
Itertools and collections
I used itertools.product () to generate stones on the board. For example, to quickly create a list of all points of the grid on the GO board (for example, from (0.0) to (18.18)), I can use itertools.product (Range (19), Range (19)).
I used collections.defaultdict () to create a glass counter. String Self.prisoners = collections.defaultdict (int) creates a data structure similar to the Python dictionary, except that when the key should access the dictionary for the first time, it is assigned the appropriate value of 0. I like to use Defaultdicts to make mine The code is more resistant to Keyerrors without the need to write a bunch of special checks.
The fundamental unit of the gaming process is a group of stones .. When a group of stones is completely surrounded, they can be captured immediately. But while one stone in the group has at least one move (the unoccupied neighboring field of the board), the whole group is still alive.
For the purposes of our game program, we have the following mathematical task: the matrix n x n is given, filled with three different values (0 for empty stone, 1 for black stone, 2 for white stone), you need to create a list of groups of stones of each color, where each group is each group Stones are a list of stones. The description can be a little complicated for understanding, therefore, please look at the image below and see if you can determine how many stones of each color exist.
The correct answer: there are 6 groups of black stones and 2 groups of white stones. Let you not be misleaded by 3 black stone, located diagonally - they do not come into contact with each other, because they are not directly connected by the lines of the grid, so each stone is a separate group of stones. I quickly realized that it was not easy to write a program that takes the game field and automatically finds all groups of stones, so I began to wonder if the algorithm can already be used.
I found that the use of the theory of graphs and the NetworkX column library gives a surprisingly elegant solution to the problem of counting groups of stones. Suppose we want to find all groups of black stones. The main idea is that we start with a “mesh column”, which looks exactly like a grid on the board of the th: each peak is connected to the vertices above, lower, left and right. Then we delete all the peaks on which there are no black stone, which leaves us a count in which the components of the connectedness directly correspond to the groups of stones! We can simply use the built -in Connect_components () in NetworkX to return the correct answer! Using this function, I was able to realize the code to find positions in the game. This means that the game will automatically find when a group of stones is surrounded, removes them from the board.
Serious players in the GO, who watched the code, could notice that the game was not completed 100%. The game does not apply the KO rule that does not allow players to repeat the previous game position. It would also be nice to implement an automatic calculation of points at the end of the game, as well as make convenient functions, such as “cancellation”. Finally (and this is a big problem), it would be great to have AI, who can play against one human player, since now the game is designed by two human players.
I hope you liked reading about my experience in creating a GO game on Python, and that you learned several new things along the way. If you have other suggestions or reviews, leave a comment below!
[1] Pygame Documentation:
[2] NetworkX Documentation:
#machinelearning #articalIlinTelling #ai #Datascience #PROGRAMMMING #TECHNOLOGY #DEEPLEARNING #GO