Storing hashes as game states

Started by
6 comments, last by Kylotan 6 years, 9 months ago

Hi guys, is it a good idea to store hashes in a database as game states? Let's say I have a chess game, and a colomn of my database stores things like 1A1B1C... and each pair represents the position of some piece (first pair(1A) represents the position of 1st rock, second pair- position of the king etc...). I've read that Guild Wars uses databases as I said and every player in the game is a bunch of data stored in hashes. I know that a lot of developers use sockets, but I find it easier using a database to store states. Can you tell me the advantages and the disadvantages? Thank you.

Advertisement

Hashes, Databases, Sockets, States...

These are all *completely unrelated things*.  I'm worried that you're misusing terminology.  Can you clarify your question by describing what you think those terms mean so that we don't misunderstand what you're trying to ask?

Hashes are a lossy conversion of data.  You can't get the original data back.  So, no, you should not use them to store game states.  You can use them to quickly FIND game states, but you should not use a hash for the state itself.

Ok, I misused hashes, because I thought you can get the original data back. Still, I think the example explains pretty well what I wanted to say. 

 

Consider two players playing chess online, on a website. The game starts and player one(A) plays with white, his colomn that stores his pieces positions (in the database) is like this 1A1B1C1D1E1F1G1H2A2B2C2D2E2F2G2H. second player(B) plays with black pieces, his colomn is like this 8A8B8C8D8E8F8G8H7A7B7C7D7E7F7G7H. Let's say A moves his first(from left to right) horse to 3A, his column will be updated and it will be like this 1A3A1C1D1E1F1G1H2A2B2C2D2E2F2G2H (notice the 1B -> 3A). The two strings that I wrote will be always initialized with 1A1B1C1D1E1F1G1H2A2B2C2D2E2F2G2H and 8A8B8C8D8E8F8G8H7A7B7C7D7E7F7G7H, based on the players color. These strings are the gamestates that I refered to, because based on them, the pieces are printed on the screen.

 

Sorry for not being explicit enought, I hope you'll understand now, here's a chess table:

is_chess.jpg

Yes, that's a totally fine way to serialize the board state of a game of chess. I'm assuming that when pieces leave the board, you'll mark their slots with something like "**" instead of a board position. You can also concatenate both of the strings into a long string; you don't need to separate each of the players' states into separate strings.

To support queen promotion to more than one queen, you may need to also store state for which pieces are now "queens." This is necessary to render the board correctly, so it's part of "board state." Additionally, you need to store state for whether each player has castled or not (because you can only castle once,) and which pawns have moved two spaces (because of enpassant capture.) These states are NOT visible on the board, and are thus only GAME state, not BOARD state, so they are important to enforce rules but not render the board.

Finally, "sockets" and "databases" are totally different. "Sockets" are how remote players get messages. "Databases" are how you store persistent state so it stays around if the server reboots. All network traffic, no matter what the layer (UDP, ping, HTTPS, SSH, VoIP, ...) uses sockets as the programming API. When a player connects to your web server, that's a socket. When your web server makes a query to a database, that's typically also a socket connection, over which the persisted database data is transferred.

enum Bool { True, False, FileNotFound };

I would use a 2D array, because the way you're doing it it seems like you'd have to sent that entire string back and forth ? maybe not.

Also with 2D arrays you can use the first dimension to represent a piece on the board, and the second dimension values to represent the piece type, coordinates, can it move(bools) and link it to its movement type. 

2 of those and you'd have one for yourself and one for your opponent. 

9 hours ago, AlluMihu said:

is it a good idea to store hashes in a database as game states? ...  I find it easier using a database to store states. Can you tell me the advantages and the disadvantages? Thank you.

Apart from the mixed terminology already used, if you're looking to store values in a database, there are advantages and disadvantages (pros and cons) that all depend on what you are doing with the data.

If you're using a relational database, you'll want to have a format that allows you to follow your relationships and join whatever it is you need joining with.  If you are serializing data into fields that can be encoded in to records, then it may be a good fit.

But others may be a bad fit. If your serialization can't be used for relational purposes and you're trying to store them in a relational database's rows, then it may be a terrible fit.

You may be looking to build tables to solve the game, or to communicate with other players about the game, or to record checkpoints along the game.  You may be looking at ways to bill users every month for their play. You may be looking at something else entirely. Each goal may have different pros and cons for different storage systems.

 

7 hours ago, AlluMihu said:

Consider two players playing chess online...

For chess specifically there are two widely-used encodings.  One is called 0X88 that takes 128 bytes, the other is a bitboard that fits within 64 bits.  Both have their own pros and cons.

The 0X88 method is usually faster to manipulate on today's processors even though it is longer than a bitboard. The bitboard is great because of its small size.

For one useful example, if you're trying to communicate the state of the board a bitboard can use base64 encoding to encode it as an eleven character value. Base64 encoding uses plain text values, so someone could type in a board like "B4bmNvZGUgY" (made up example) and everyone in the world could use it to know the state of the board.

 

There already exists a standard way to notate a whole chess board state: https://en.wikipedia.org/wiki/Forsyth–Edwards_Notation

Alternatively you can use a different notation to store the whole game including past moves: https://en.wikipedia.org/wiki/Portable_Game_Notation

Both are compact enough that you can store them in a single database column without problem.

This topic is closed to new replies.

Advertisement