Chess in SQL

(dbpro.app)

23 points | by upmostly 2 days ago

5 comments

  • eelinki 28 minutes ago
    You could take this even further and add triggers to see if your move is legal or not. Or delete row with a conflict when you capture a piece.
  • upmostly 2 days ago
    Author here.

    I had the idea of building a working Chess game using purely SQL.

    The chess framing is a bit of a trojan horse, honestly. The actual point is that SQL can represent any stateful 2D grid. Calendars, heatmaps, seating plans, game of life. The schema is always the same: two coordinate columns and a value. The pivot query doesn't change.

    A few people have asked why not just use a 64-char string or an array type. You could! But you lose all the relational goodness: joins, aggregations, filtering by piece type. SELECT COUNT(*) FROM board WHERE piece = '♙' just works.

    • eastbound 1 minute ago
      SQL can make 2D data, but it extremely bad at it. It’s a good opportunity to wonder whether this part can be improved.

      “Pivot tables”: I often have a list of dates, then categories that I want to become columns. SQL can’t do that so there is a technique of spreading values to each column then doing a MAX of each value per date. It is clumsy and verbose but works perfectly… as long as categories are known in advance and fixed. There should be an SQL instruction to pivot those rows into columns.

      I’ll provide an example here in a few minutes.

      This thought should be extended for more than 2 dimensions.

  • landsman 29 minutes ago
    Tool looks nice, but I would prefer such a tool written in a better (native?) language than JavaScript. Security is also important to me, so I only use open-source tools. I’m going to stick with DBeaver and DataGrip.
  • grimm8080 4 minutes ago
    Amazing, how do I play it?
  • FergusArgyll 29 minutes ago
    Very cool! I think the dragon is missing a white rook - ascii chess pieces are heard to see...