Networking and Game State Handling
The provided code establishes a network connection and handles the conversion of game states between JSON and matrix representations.
This includes classes for network communication (Network
) and a converter (Converter
) to facilitate the transformation between JSON and matrix formats.
In order to map each pawn type to a specific value, a set of constants has been defined.
The Pawn
enumeration defines different pawn types, including EMPTY
, WHITE
, BLACK
, KING
, and THRONE
allowing to easily identify the type of each pawn while fetching the game state.
Key components of the implementation are as follows:
Networking Functionality
The Network
class manages the network communication between the game client and server.
It supports connection setup, sending moves, and receiving the current game state.
It utilizes socket programming and JSON encoding/decoding for data exchange.
The connect
method establishes a connection with the server, and the get_state
method retrieves the current game state.
Game State Conversion
The Converter
class handles the conversion between JSON representations received from the server and the matrix format used in the code.
It provides the json_to_matrix
method, converting the JSON state into a 9x9 matrix representing the game board.
This method uses the Pawn
enumeration to map each pawn type to a specific value by using a numpy method called vectorize
which applies a lambda function to each element of the board.
The lambda function maps each pawn type to a specific value, as shown below:
Game State Fetching
The get_state
method of the Network
class is responsible for fetching the current game state from the server and checks the returned state for validity. If the turn is not neither WHITE
nor BLACK
, the method returns WHITEWIN
, BLACKWIN
, or DRAW
depending on the outcome of the game which is calculated by the Java server.