5 – Say Elo to Chess


🎯 Learning Objectives

After this lesson you should be able to:

  • Know how chess pieces move
  • See chess as a game made up of objects and the position of those objects as the game’s state
  • Get the first chess pieces moving
💬 Key Vocabulary

  • Chess
  • Object
  • State
  • Transition
  • Pawn
  • Valid move
  • Capture
  • Assets
  • Iterative development
  • Sprint
Last lesson, you…

  • Built your first proper game
Today, you will…

  • Learn the basics of chess
  • Build the first stages of a chess game
  • Consider the first steps of iterative development

📝 Starter Activity – Chess

https://s.yimg.com/ny/api/res/1.2/m2RcqBf7gSggb3AiEUnVmg--/YXBwaWQ9aGlnaGxhbmRlcjt3PTk2MDtoPTQ4MC42NjY2NjY2NjY2NjY3/https://s.yimg.com/uu/api/res/1.2/gnjXfQwaRGOODGUd2TVaeA--~B/aD03MjE7dz0xNDQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/hearst_mens_health_171/b4db864d88a143fae9173183bfc55b29
The Queen’s Gambit (Netflix, 2020), has made chess popular again.
  • Let’s see what you know already about chess.
  • Click the link below to go to chess.com, and play a short game.
  • If you’ve played before, you can choose a more challenging opponent on the right hand side – each has a difficulty measured using the standard Elo Rating System for chess.
  • You start with a rating of 1000 and gain or lose points as you win or lose games. A beginner might have an Elo rating of a few hundred (more losses than wins), a good amateur player between 1000 and 2000, and a chess master a score above 2000.

📝 Learn it – Chess

Answer the following questions based on what you know and what you’ve found out from playing chess.

  1. How does chess work?
  2. How many different pieces are there in chess?
  3. How are the pieces different? What does each do?
  4. How is a game of chess won?
  5. What would a state in chess be?

Think about these questions, write down your answers, then have a look at the answer below.

  1. The players each have two rows of 8 pieces, on an 8 x 8 board with squares of alternating colours. There are duplicates of some pieces, but only one of some. Players take turns to move, and pieces can only be moved following their own movement rules. A piece cannot move into a square already occupied by another piece of the same colour, and pieces other than the Knight cannot move through other pieces, they are blocked by them. Pieces either move into empty spaces, or onto spaces occupied by the other colour, in which case the piece landed on is removed from the board – a capture. Pieces that could be captured on the next turn are said to be under attack.
  2. There are 6 different pieces in chess:
    • King
    • Queen
    • Rook (or Castle)
    • Bishop
    • Knight
    • Pawn
  3. The pieces each have their own way of moving, and rarer pieces tend to have more powerful moves, with the exception of the King.
    • The King can move 1 square in any direction, so long as that square cannot be attacked (moved onto) by a piece of the other colour in the next turn. If the King is under attack at the start of the turn, it is called being in check, and the turn must end with the King out of check. This can be done by moving the King, or blocking the attack with another piece. If the King cannot be taken out of check, this is called checkmate.
    • The Queen can move any amount in any direction, so long as it’s a straight line and it is not blocked by another piece.
    • The Rook can move any amount but only vertically or horizontally.
    • The Bishop can move any amount, but only diagonally. It therefore cannot move onto squares of the other colour, and each player has one black-square and one white-square Bishop at the start.
    • The Knight moves diagonally in a 2×3 box (i.e. 2 squares in one direction, 1 square in the other), forming an L shape, and ignores any pieces in the way, which are said to be “jumped over” – the Knight is the only piece not blocked by other pieces.
    • The Pawn, despite being the least powerful piece in many ways, has the most confusing set of moves. A pawn moves forwards one square each move, but gets an optional bonus extra square on its first move. Pawns can only move forwards. Unlike every other piece, pawns cannot capture forwards, only to the forwards squares immediately diagonal to them. If a pawn reaches the back of the board, it is said to be promoted and can be exchanged for an additional piece of any other type except the King. It also has a special capture move called en passant, which most beginner players do not understand!
  4. The game is won by reaching a checkmate, a point where one player cannot stop their King being in check at the end of their turn. The player in check loses. It is possible to draw a game of chess by reaching a point where neither player can get checkmate or where the only option for one player is to repeat the same cycle of three or fewer moves. This is called stalemate.
  5. A state in chess would be the positions of all the pieces, including which player’s turn it is. Such a state is known in chess language as the configuration of the board. Every time a player moves, the board’s configuration is changed, and so the game has moved into another state. One of the reasons chess is such a complicated game is the number of possible states although there are not infinite possibilities, the number of states is known to be very large. In fact, nobody knows exactly how many possible states there are in total. After just one move from each player there are 400 possible states (each player has exactly 20 valid starting moves to pick from), and after two turns each, the number of possible states becomes almost 200,000 – too many for any human player to learn. With each turn taken, that number grows larger, although after four turns some games start reaching a checkmate and so have no next state to move into. The states of chess are often modelled as a tree, in which each move starts a new branch of possible states, but this still doesn’t make it easy to find the answer. Mathematician Claude Shannon estimated at least 10120 possibilities exist – that’s vastly more states than there are atoms in the universe.

📝 Activity 1 – Game State

The code below includes a lot of the work needed to make a chess game. Most of it is hidden away. However, the previous developer gave up before it was completely finished, and quite a lot is still missing or broken. This is real code, the basis of which you can find released online, and it really is broken! Your job over the next few lessons will be to finish this game. Today’s task, though, is just to get the pawns moving.

You’ll recognise a lot of code, like the use of a game loop. You’ll also see that the chess board, and all the pieces, are objects. You might also recognise that the sprites are indeed being blitted onto the screen.

You need to complete the following tasks today:

  • Make the game draw the white pawns
  • Make the game draw the black pawns
  • Make the pawns able to move

💬 Summary

In this lesson, you…

  • Learned the basics of chess
  • Built the first stages of a chess game
  • Considered the first steps of iterative development

In the next lesson, you will…

  • Produce an iteration of your game which adds more features and improves the game.

🏅 Badge it

🥈 Silver Badge

  • Upload your findings and knowledge about chess.
🥇 Gold Badge

  • Upload your code with some of the tasks completed.
🥉 Platinum Badge

  • Upload your code with the pawns fully implemented and working.