MATChess: One-Click Chess Against MATLAB

In MATChess, you play chess against MATLAB with only your mouse click. MATLAB plays white and moves first. You play the dark pieces. Click a piece, then click a green square. That is all you need.
The goal is not just to move pieces. It is to think one step ahead of MATLAB, and to choose the level that matches your skill.
Play the game
Entry page: MATChess
Open the entry, click Play Game, then click Run this app in MATLAB Drive.
The start of a game. MATLAB (white) has made its first move. Now it is your turn.
How to play
  1. MATLAB moves first. Wait until the top line says Your move.
  2. Click one of your dark pieces.
  3. Green squares show every legal move for that piece.
  4. Click a green square to make your move.
  5. When the game ends, click anywhere to play again.
The game only lets you make legal moves. You cannot make a move that leaves your own king in check.
A dark knight is selected. Green squares show where it can go. Yellow squares show MATLAB's last move.
What the colours mean
The line at the top tells you what is happening: Your move., CHECK! Your move., MATLAB is thinking..., or the result when the game ends.
MATLAB is thinking. After your move, it waits half a second and then replies.
Choose your level
There are three buttons on the left side of the board: EASY, NORMAL and TOUGH. Click a button to start a new game at that level. The orange button shows the level you are playing. The game starts on NORMAL.
Try this: play the same first moves on EASY and on TOUGH. Where does MATLAB start to play differently?
How MATLAB chooses a move
MATLAB gives every position a score. A higher score is better for MATLAB. The score has three parts:
  • Piece value: pawn 100, knight 320, bishop 330, rook 500, queen 900, king 20,000.
  • Centre bonus: knights and bishops get extra points when they stand near the centre of the board.
  • Pawn push: pawns get extra points as they move forward.
Then MATLAB tries its moves and your replies, and keeps the move with the best score. It looks at captures first, so it can skip weak moves faster. This idea is called alpha-beta pruning. The king is worth so much that losing it ends the search, and a quicker win scores a little higher.
The rules in the game
Included:
  • All normal moves for every piece.
  • Pawns can move two squares from their first row.
  • Check, checkmate and stalemate.
  • A pawn that reaches the last row becomes a queen.
Not included:
  • Castling and en passant.
  • Draw by repetition and the 50-move rule.
  • Choosing a piece other than a queen when a pawn is promoted.
One click does every job
The game uses just one input: a click. Each click does one job, depending on where you click and what is happening:
  • Click a level button: start a new game at that level.
  • Click one of your pieces: select it.
  • Click a green square: move the selected piece.
  • Click after the game ends: play again.
Each square is 66 pixels wide. The game turns the click position into a column and a row with ceil((x-136)/66) and ceil((y-50)/66). After the game ends, it waits half a second before it accepts a click. This stops a quick click from hiding the result.
How the board is drawn
The board is stored as an 8-by-8 matrix. Positive numbers are MATLAB's white pieces and negative numbers are your dark pieces. The numbers are 1 pawn, 2 knight, 3 bishop, 4 rook, 5 queen, 6 king, and 0 for an empty square. A move is four numbers: the start row and column, and the end row and column.
The code is split into three parts: initialize, update and render. The graphics are created once and then reused. The board is one patch object with 64 squares, and only the colours change from frame to frame. The pieces are Unicode chess symbols in text objects. Each piece is drawn twice: a bigger black symbol behind and a smaller coloured symbol on top. This gives every piece a dark outline.
Try this
  • Can you beat TOUGH? How many moves does it take?
  • Play on EASY and try to catch MATLAB's random mistakes.
  • Can you trap a piece with a knight fork?
Which level can you beat? Tell me in the comments. Thank you for playing!
Umar
Umar 2026-11-22,5:41
I gave you vote on this one @KARUPPASAMYPANDIYAN M because after glancing through your code, it caught my attention regarding the recursive minmax search with alpha-beta pruning in nm(), the position evaluation in ev(), and legal move filtering in legal(). The compact board representation and modular move generation in gen() show strong algorithmic thinking. Pawn promotion and checkmate/stalemate detection are also nice touches. Good job! 👍

标签