Playing chess over IRC from Emacs, even against a bot

Leer en español

At work we have a sort of small chess club. We play some online games between colleagues, and afterwards we talk about the most notable moves. Most of us work remotely, so the routine is simple: we line up our calendars and, shortly after, we meet for an online game on a website like chess.com. Nothing complex, easy for everyone.

Then, since some of my colleagues use Emacs, I asked myself: why not use it? Some time ago I wrote an article about playing chess online with Emacs. Among the different options, I explained that chess lets you play against the machine (with a simple but fun AI), over a local network, against ICS servers (platforms for playing online) and over IRC. Yes, through an IRC server! It uses a private conversation where commands and moves are sent. So, if we wanted to play among ourselves, all we would need is a daemon running an IRC server.

Right, we need 2 pieces to play online:

  • An IRC server, such as ngircd.
  • The chess package installed in Emacs.

There was one problem, though: the IRC integration in chess was broken, so I fixed it. The branch of PR #42 in emacs-chess is still waiting for review, but it works perfectly. I also added the chess-irc-engage function to challenge a friend by their nick.

So the flow of a game is as follows:

sequenceDiagram
    participant A as Ana's Emacs
    participant S as ngircd
    participant P as Pepe's Emacs
    A->>S: Connects as ana-chess
    P->>S: Connects as pepe-chess
    A->>S: Challenges pepe-chess
    S->>P: "Do you wish to play a chess game against Ana?"
    P->>S: Accepts
    A->>S: e4
    S->>P: e4
    P->>S: e5
    S->>A: e5

e4 and e5 are moves in algebraic notation: the file as a letter and the rank as a number.

Let's set up the pieces on the board.

IRC server

The ngircd server is written in C and is very lightweight. Installing it on Debian/Ubuntu is easy:

sudo apt install ngircd

Replace its configuration in /etc/ngircd/ngircd.conf with:

[Global]
    Name = chess.example.org
    Info = Private IRC for chess.el
    Listen = 0.0.0.0
    Ports = 6667
    MotdPhrase = Chess only. Challenge with M-x chess-irc-engage
    PidFile = /run/ngircd/ngircd.pid
    ServerUID = irc
    ServerGID = irc

[Limits]
    MaxNickLength = 20

[Options]
    PAM = no
    DNS = no
    Ident = no

[Channel]
    Name = #chess
    Topic = Waiting room
    Modes = tn

Check the configuration:

sudo ngircd --configtest

and restart:

sudo systemctl restart ngircd

You now have an IRC server for our chess club.

There is no password: anyone who reaches port 6667 can play, or start using it for malicious purposes. My advice is to restrict network access with a firewall that only allows your friends' IPs, or to put the server inside a VPN. I leave the decision in your hands.

Now let's jump to the client.

Setting up Emacs

You need Emacs 30 or later. Add this to your init.el:

(use-package chess
  :vc (:url "https://github.com/tanrax/emacs-chess"
       :branch "fix/chess-irc"
       :rev :newest)
  :custom
  (chess-irc-server "chess.example.org")
  (chess-irc-port 6667)
  (chess-irc-nick "mynick-chess")
  (chess-full-name "My Name"))

Replace chess.example.org with the IP or domain of your IRC server, and mynick-chess with the nick you want to use.

Evaluate it and your Emacs is ready to play.

A game goes like this:

  1. You both run M-x chess-irc and press Enter at each prompt to accept the configured server, port and nick. You will see Waiting for opponent — send /msg or use M-x chess-irc-engage.
  2. The challenger runs M-x chess-irc-engage from the board and types the friend's nick, for example pepe-chess.
  3. The friend sees Do you wish to play a chess game against My Name? and answers y.
  4. Whoever wants white makes the first move. The other player will see Your opponent played the first move, you are now black.

Chess board in Emacs

During the game you have some useful commands:

Action Key Command
Offer a draw C-c C-d chess-display-draw
Resign C-c C-r chess-display-resign
Ask to take back C-c C-t chess-display-undo
Abort the game C-c C-a chess-display-abort

Want to talk to your opponent? Press " on the board: a chat buffer opens, where you type each message and send it with Enter.

And if you are brave, you can also challenge someone from any IRC client:

/msg mynick-chess chess match Pepe

Mind you, you will then have to type your moves by hand, in algebraic notation. I think it can be fun if you have a physical board in front of you.

Bot

It could be fun to have an AI (not an LLM) to play with you: a special nick that is always online and ready for a game.

For that you need a headless chess engine. It is software that lets you play chess without a graphical interface, just by sending and receiving moves. Today, the strongest free one you can find is Stockfish.

However, Stockfish can only think about one move at a time. It cannot handle several games in parallel, and the bot may well be playing with several people at once. There are many solutions, but the simplest is a queue of move requests. Every time a player moves, the bot takes the whole game and sends it to a free engine, in order of arrival. When the engine finishes thinking, it returns the move to the bot, which sends it to the opponent. Luckily, the slowness of the human brain plays in our favour. While the human plans their move, the engine can be busy with another player's game.

flowchart LR
    A[Ana's Emacs] <--> I[ngircd]
    P[Pepe's Emacs] <--> I
    I <--> B[Bot]
    B --> Q[(Engine queue)]
    Q --> S1[Stockfish 1]
    Q --> S2[Stockfish 2]

If you want a ready-to-use solution, I implemented it in the chess-irc-bot repository. A single command starts the ngircd server and the bots:

git clone https://git.andros.dev/andros/chess-irc-bot.git
cd chess-irc-bot
docker compose up -d --build

I set up 4 nicks, one per difficulty level.

Nick Difficulty
bot-easy Elo 1320
bot-normal Elo 2000
bot-hard Elo 2800
bot-perfect Stockfish with no limits

With M-x chess-irc-engage you can challenge any of them. If you want white, make the first move. If you prefer black, press the space bar to give the bot the first move.

Final notes

There are much more complex solutions with many more options, such as rankings. We are full of limitations:

  • No encryption or authentication: anyone who reaches the port can use someone else's nick.
  • No arbiter: each Emacs validates the moves and keeps its own clock.
  • One connection per game: each game opens an IRC connection with your nick, so two games at once need two nicks.
  • No history: games are not recorded, and there is no way to review them later.

Still, it is enough to play with friends or against a good bot: it is built with a simple IRC server, a free chess engine and a few lines of Python. It is even a good option for small remote chess communities or federations.

The natural next step would be a simple TUI or GUI client that speaks the same IRC protocol, so you no longer depend on Emacs. A master move. For now I am happy playing with my colleagues and challenging the bots in my spare time.

I only have one question left: white or black?

Help me keep writing

Every coffee gives me a push toward the next article.

Comments

There are no comments yet.

1 mention from another site

You may also like