Chess Game Coding

My students are trying to create a chess game. They want to be able to click on the piece and then click the square it moves to. I can’t figure out how to code that. This is what we started with. They are so excited to learn to code I want them to be successful. Can you help us get started with the block coding of it? Thanks.

1 Like

Kia ora Mary Beth,

Here’s the way I think about it, in the most simple form:

  1. First, we need to detect the clicked piece - this is an event, so in the Events category
  2. We need to detect the clicked square
  3. We need to move the clicked piece to the clicked square:

The above code is not good for a number of reasons:

  1. It only works for 1 piece and 1 square - replicating this for all the possible pieces and squares would be incredibly inefficient.
  2. The piece doesn’t move on a fixed plane (the board) - this is trivial to fix though.

So, we need to be able to apply events and movement to multiple pieces/squares. By adding all squares and pawns to their own list, we can iterate over the list and apply the actions we want to each item:

Now we can iterate over first the pawns, then the squares, and apply actions:

You can see in the above code I’m using the variable created by the foreach loop in the actions - this applies the action to each item.

This code is far from efficient and would be better done in Javascript. It also has functional bugs, and doesn’t apply any particular rules around what the chess piece is allowed to do, however it should give you an idea of how to proceed.

You can see this in action and remix the code at

Let me know if you have any questions.

Geoff @ TechLeap


Wow! Thank you so much for your help!!