JavaRush/Courses/Claude Code SELF/Three Attempts to Build Chess

Three Attempts to Build Chess

Open

1. First attempt: spell out all the rules in detail

Since keeping it short did not work, Tommy decided to go to extremes:

“Well, fine. If Sam’s short spec of ‘make chess’ is not enough — let’s spell it out for him down to the last detail. Alex, give me a detailed spec: how each piece moves, what happens on check, how castling works — everything, so Sam can’t make a mistake anywhere.”

Alex starts writing it all out. Knight like this, bishop like that, rook, queen, king, pawn. Rule for check. Rule for castling. En passant capture. Tommy peeks into the chat as the text grows — and still he wants more:

“Not enough. Make it even more detailed. And if we’re writing it, put in everything: board size, square labels, what to show on checkmate, whether we need a clock for the game, whether to keep score or not, what color to highlight the piece that moved…”

Alex writes. And writes. The document swells — no longer three lines, but a whole sheet: five pages, two hundred lines. Move rules, end rules, layout, clocks, highlights.

Chess spec (excerpt):
...
4.3. The knight moves in an L: two squares straight + one sideways,
     jumping over pieces. Up to 8 possibilities total; any that go off the board — discard.
4.4. On check, ONLY moves that get the king out of attack are allowed:
     move the king / block with a piece / capture the attacker.
4.5. Castling: the king and rook move at the same time if neither has moved,
     there is nothing between them, the king is not in check and does not pass through an attacked square...
...
7.2. Game clock: 10 minutes per player; on zero — loss.
7.3. Highlight the last move in yellow, highlight check in red.
...

Tommy looks at the expanded document — and instead of getting scared, rubs his hands in satisfaction:

“Nice, now that’s different! Look at how powerful the documentation is. Everything is specified down to the last square. Now Sam can’t possibly do anything wrong, no matter how hard he tries — just read it and execute it.”

Tommy leans back in the chair at his desk, rubbing his hands in satisfaction, while boxes of documents and piles of paper tower to the ceiling around him — that’s how much the spec has grown.

“Read it, you say.” Alex snorts. “Are you going to proofread this yourself?”

“What do you mean, me? That’s Sam’s job.”

“Nope. It’s yours.” Alex turns serious. “Look, here’s the important part. The code the agent writes, you may not need to proofread — see, for a month you haven’t checked a single line yourself, and nothing. But the spec — you have to. Because the spec is your decision about what to do at all. If you make a mistake in it, add something stupid, or forget something important — the agent will honestly do exactly what the dumb spec says, and you, not it, will be to blame. A human sets the task, and a human approves it too.”

“Oh, come on.” Tommy waves a hand. “Two hundred lines to proofread? I trust you. Let Sam figure it out, that’s what he’s for.”

Alex only sighs — too early, he thinks, but let him bump his own head. They hand the sheet to Sam. And he… pushes back:

“Listen, this is two hundred lines, I can’t handle that in one go. Break it into parts — into fifteen separate chunks or so, and I’ll do them one by one. But the whole mountain at once? No, I’ll definitely lose the plot somewhere.”

Breaking it into fifteen chunks is already too much work for Tommy. They grit their teeth and hand it over as is — maybe it will work. Sam honestly tries, but in the tricky spots, just as he warned, he gets lost: he made castling sloppy, catches check only half the time, and completely missed en passant. They run it through the seven-point checklist — it got better, now it passes five, but castling and the small details are still shaky.

“It’s not a quarter anymore, but it’s still not chess.” Tommy sighs. “And writing the spec for this is torture. More detail is not the answer. There are so many rules that they don’t fit into a human-readable spec, and they don’t fit into one agent run either.”

2. Second attempt: cut the rules down

“Fine.” Tommy comes at it from the other end. “If all the rules are too much, then let’s cut them down. Let the game keep track of the simplest things: board, pieces, turns — white, black, white. And whether the pieces move correctly or not — let the players keep track themselves; they’re human, they know chess. Move whatever you want, as long as the turn order is respected.”

It sounds simple, and Tommy is already rubbing his hands. But Alex, as always, looks one step ahead:

“Suppose that. And how will you know the game is over? The game doesn’t know the rules. What is checkmate, it has no idea — the king is under attack, nowhere to go, and the board has no clue that this is the end.”

Tommy freezes. And really, he hadn’t thought about that.

“Well…” he drawls. “Then let’s say the game ends when one of the kings is simply eaten — removed from the board. Rough, of course…”

“Very rough.” Alex nods. “In real chess the king is not captured; it doesn’t get that far, checkmate is declared. But without rules, that’s the only way to catch the end. A hack.”

“And then castling won’t work either,” Tommy adds, tasting the bitterness. “It’s two moves at once, king and rook. Without rules, the game will decide that it’s cheating — moving two pieces in one turn.”

He rubs the bridge of his nose. The picture is sour: legality on the players, the end on the “captured king” hack, no castling. And then Alex brings up something Tommy hasn’t thought of yet:

“And now remember who is going to sit on the other side. The agent. And it knows chess for real — by all the rules.”

“So what?” Tommy doesn’t catch it right away.

“Here’s what: on your crooked board, it’ll get confused. It won’t capture the king — because it understands that you don’t win chess that way; it’ll wait for checkmate. But your board can’t do checkmate. It’ll castle properly — move the king two squares — and the board doesn’t know that move, so it’ll bounce it as cheating.”

Tommy darkens, finishing the picture himself:

“So I’ll have to write a separate instruction for the agent too. How to play my broken version of chess — not by the normal rules, but by its hacks. What a joy.”

“Exactly.” Alex nods. “We cut the board down, and now we have to fit the agent to it too. We removed one kind of complexity and created another. It isn’t chess anymore, it’s a ‘piece mover,’ and it takes no less effort than before.”

“No.” Tommy waves it off. “Writing instructions for the agent on how to play broken chess is already too much. That’s not going to work.”

3. Third attempt: take something ready-made

They both fall silent, stuck at a dead end. And then it hits Alex:

“Stop. Why are we even straining ourselves? Chess is an ancient game, people have written it in browsers a thousand times. There has to be someone who already packed all these rules into a ready-made piece of code. Let’s not reinvent the wheel — let’s look for something ready-made.”

“What do you mean — it’s already written somewhere?” Tommy doesn’t get it right away.

“Exactly. That’s called a library — a ready-made chunk of code for a task: some smart person wrote it, polished it, published it, so others wouldn’t have to suffer again. You’ve already seen this principle, remember? When the agent lacked a skill, we didn’t teach it from scratch — we took a ready-made skill from the catalog. Code works the same way: first ask whether there is a ready-made solution, and only if there isn’t, write it yourself.”

“And how do you even look for it? Google ‘download chess code’?”

“You can Google it, but it’s easier to ask AI.” Alex types straight into the chat: “Are there ready-made JavaScript libraries for chess in the browser, compare the popular ones.” “It’ll go through the internet and immediately tell us what’s alive and what’s abandoned,” he adds. “Look what it returned.”

In response — not one thing, but a whole pile:

For chess in the browser there are several libraries:
- chess.js      — rules and logic (moves, check, checkmate). Very popular, alive.
- chessboard.js — visual board, piece dragging.
- chessground   — board from lichess, beautiful, but more complex.
- ...and a couple more smaller ones.

“Now you choose.” Alex scrolls through the results. “It’s important to look not just at the name: is it popular (if thousands use it, it means it works and won’t be abandoned), is it fresh, is it suitable for our task. chessground is powerful, but it’s overkill for us. And chess.js is exactly for the rules: inside it are all the chess laws at once — moves, check, checkmate, castling, en passant. Popular, alive. We’ll take it.”

He leans back and drops what Tommy should burn into his memory:

“And remember this for the future, in general. Almost everything you’ll set out to do in your first few years has already been written by someone else before you. These days almost nothing is written from scratch — modern development is more about ‘finding ready-made libraries and gluing them together with your own code’ than about inventing everything yourself. So the first question is always: is there a ready-made solution for this?”

“So all seven points from my list are already inside chess.js?” Tommy finally gets it.

“All seven. People wrote them before us. Our job is to hook up the board and decide who plays for whom.”

4. How to connect something ready-made: CDN

Tommy is already imagining the hassle — and from experience with past services, he immediately gets wary:

“So, where do I download it from? Do I need to register, get some key, like with that site parser? Or pay?”

“No, no, none of that is needed,” Alex reassures him. “Libraries like this are free, and you don’t have to download them to your machine. There is a thing called CDN. Here’s the trick: popular libraries are already sitting ready on special servers on the internet — distributed worldwide, with no keys or registration. You don’t need to drag the file into your project. You just insert a link to the library directly into your page — remember when we were breaking down <script> in HTML? — and when the browser opens the page, it pulls the library in from that link automatically.”

He shows the line:

<script src="https://cdn.jsdelivr.net/npm/chess.js"></script>

“This means, ‘browser, go to this server and load chess.js from there.’ One line instead of copying the library into your project. And it works fast: CDN servers are spread all over the world, they serve the file from the closest place, and they cache it too — once it’s downloaded, it flies from then on.”

Tommy is beginning to get it:

“Wait. So the internet has a ton of ready-made pieces of code, free, and anyone can connect them to their project with one link? You don’t even have to download them?”

“Exactly.” Alex nods. “Not a ton — millions. Think of it this way: for almost any common task, someone has already written a library, polished it, and put it on a CDN. All you have to do is paste the link.”

“And how do I know which link to paste and where to put it?”

“Just ask AI.” Alex grins. “‘Connect chess.js through a CDN for me’ — it’ll find the current link and put it where it belongs. Your job is to know that the ready-made thing exists, and not rush to write your own.”

How a CDN works. Your game.html page has one line with a link to chess.js. Step 1: when the browser opens the page, it requests chess.js from that link. Step 2: CDN servers are spread all over the world, and the closest one serves the file. Step 3: the browser puts the file in its cache — next time it’s instant. No download into the project, no keys or sign-up, free. Which link to use — ask AI: “connect chess.js through a CDN” How a CDN works: a library from one link game.html your page — one line: <script src="https://cdn.jsdelivr.net/ npm/chess.js"></script> the board came alive: the rules came from chess.js BROWSER CACHE chess.js CDN servers all over the world closest 1 2 browser requests chess.js the closest one serves it 3 the file goes into the cache — next time it’s instant no download into the project no keys or sign-up free Which link to use — ask AI: “connect chess.js through a CDN”

5. The spec for the working version and the check

Now the spec is short — not because Tommy got lazy again, but because the rules don’t need to be written; they’re already in the library:

Build game.html — chess for two on one screen.
Take all move rules, check, checkmate, castling — from the chess.js library
(connect it through a CDN); do not write the rules yourself.
Board 8×8, pieces, turns in order. A “New Game” button.
What to write in the spec yourself and what to take ready-made. We sort the lines of the spec sheet with one question: is this the same for everyone who makes chess? Knight in an L, only escapes on check, castling, en passant — the same for everyone: these are rules, already written and battle-tested in the chess.js library; in the spec they shrink to one line, “take the rules from chess.js.” The 8×8 board and square labels are the agent’s job: draw the board and wire it to the library. Two players at one screen, a “New Game” button — only you decide, and that is your spec; clock and highlighting come later. And you are the one who checks by the ruler. The spec got short not out of laziness: all that’s left in it is what you decide What to write in the spec yourself, what to take ready-made Is this the same for everyone who makes chess? THE SPEC SHEET 4.3 knight — in an L 4.4 on check — escapes only 4.5 castling: king + rook en passant 8×8 board square labels two players at one screen “New Game” button 7.2 game clock 7.3 move and check highlighting yes — rules draw and wire up no — a choice LIBRARY chess.js rules already written and battle-tested in the spec they take one line: “take the rules from chess.js” AGENT draws the board and wires it to the library — the glue YOU — YOUR SPEC you decide the game: two players, a button clock, highlighting — later and you check by the ruler The spec got short not out of laziness: all that’s left in it is what you decide.

Sam takes it on — and this time assembles it without drifting: all the heavy lifting is already in chess.js, and all he has to do is the board and the wiring. Soon he reports: done.

“‘Done.’” Tommy snorts. “We know your ‘done.’ We don’t take your word for it anymore.”

And he runs through the whole list, point by point, sits down himself and checks:

  • board 8×8, coloring — ✓
  • pieces are placed — ✓
  • turns alternate, white goes first — ✓
  • each piece moves by its own rule — ✓ (knight in an L, bishop diagonally — just right)
  • castling — ✓
  • check and checkmate — ✓ (cornered the king — “checkmate” popped up)
  • en passant, pawn two-square move — ✓

Seven out of seven. For the first time, “done” really means done — not on Sam’s word, but by the ruler they themselves set up and ran through.

“Now that’s a real one.” Tommy admires it. “And note: we didn’t write a single rule. Everything is someone else’s, ready-made, and the game looks like it came straight from a tournament.”

One ruler — every attempt. The same seven-point ruler; “not always” doesn’t count. Start, short spec: the board and pieces are there, turn order not always, the rest no; nobody wrote the rules — “it’ll figure it out.” Attempt 1, the spec sheet: passes five points, castling and the details are shaky; you write the rules — and the agent gets lost in the tricky spots. Attempt 2, cut the rules: board, pieces and turn order are there, move rules and details are on the players, no castling, the end of the game is the “captured king” hack; the ruler got shorter, the rules were thrown out. Attempt 3, chess.js: all seven points; the rules were written by the library’s authors. Rules are written by those who have already written and battle-tested them. What’s left for you is what only you decide — and checking by the ruler One ruler — every attempt the same seven-point ruler · “not always” doesn’t count Start short spec Attempt 1 the spec sheet Attempt 2 cut the rules Attempt 3 chess.js 1234567 8×8 board with correct coloring pieces set up correctly alternate turns, White goes first each piece has its own movement rule castling check and checkmate details: en passant, pawn two squares many conditions on the players not allowed hack: “captured king” on the players the ruler got shorter Who writes the rules: nobody:“it’ll figure it out” you —and the agent gets lost nobody:rules thrown out the library’sauthors Rules are written by those who have already written and battle-tested them. What’s left for you is what only you decide — and checking by the ruler.

6. Markers for the agent: a screenshot for me, records for the machine

The game is ready — but Alex holds Tommy back before he can run off:

“One more thing while we’re building. You’ll have the agent on the second side. And it’s blind — it has no eyes like you do, and it can’t look at a pretty board. It needs to understand what is happening from the page itself.”

“Then let it read the page, it has everything there — where each piece is standing.” Tommy shrugs.

“It’ll read some of it, sure,” Alex agrees. “It can pull the piece layout from the HTML page. But not everything important is visible on the page. Whose turn is it now? Is the game over? Who won? All of that lives inside the game’s head, and may never make it out onto the page — the agent can only guess. So we prepare the game specifically for it: we ask to put clear markers right on the page that are easy for it to latch onto.”

He adds to the spec:

Attach data attributes to the board element for the agent:
- data-turn="w"/"b"       — whose turn it is now;
- data-game-over="true"   — when the game is over (checkmate / stalemate / draw);
- data-winner="w"/"b"/"draw" — who won.

“Look why this matters,” Alex says, pointing with his finger. “There are two different viewers here, and each needs something different. You need a screenshot at the end — you look at the board, there’s checkmate, there’s the winner, you can see it with your eyes. But for the agent the picture is useless; it needs a record right in the code: data-game-over="true" — the game is over, no ‘it seems like.’ Humans trust their eyes, machines trust markers. So in a game built for an agent, we keep both.”

“Got it.” Tommy nods. “I get the picture for me, the marker in code for it. We’ll build both in.”

On the left, a chessboard after Fool’s Mate with the caption “Checkmate. Black wins”; on the right, the browser’s developer tools: the board element shows the markers data-turn="w", data-game-over="true" and data-winner="b".

This is how it looks in the page code: on the left, the board after checkmate — that’s for your eyes; on the right, the browser’s developer tools (the F12 key) — the board element carries the markers data-turn, data-game-over, data-winner. That’s for the agent. You don’t need to dig in there yourself — the agent will read them on its own.

The game is assembled, checked against the list, markers are in place. All that’s left is the juiciest part: how the agent reaches the board with its hands to move the pieces.

1
Task
Claude Code SELF,  9 level,  2 lecture
Locked
Short spec using a ready-made library ♟️
Short spec using a ready-made library ♟️
1
Task
Claude Code SELF,  9 level,  2 lecture
Locked
Markers for a blind opponent 🏷️
Markers for a blind opponent 🏷️
1
Task
Claude Code SELF,  9 level,  2 lecture
Locked
Report: the library has been chosen 🔍
Report: the library has been chosen 🔍
1
Task
Claude Code SELF,  9 level,  2 lecture
Locked
Build chess from the spec 🎯
Build chess from the spec 🎯
1
Task
Claude Code SELF,  9 level,  2 lecture
Locked
Connect the library through CDN 🔌
Connect the library through CDN 🔌
Комментарии
  • популярные
  • новые
  • старые
Для того, чтобы оставить комментарий Вы должны sign in
This page has no comments yet