Chess.
Vs AI or local 2-player. Same board, no sign-up. White moves first.
ELO Rating
Casual tinyfiles ELO. Starts at 1000. K = 32. Win = + , loss = โ , draw = small move. Saved in your browser. Not official FIDE / USCF.
Formula: Expected = 1 / (1 + 10^((Opp โ You)/400)). New = Old + 32 ร (Score โ Expected). Score = 1 win, 0.5 draw, 0 loss. In Local 2-Player, White and Black both move โ White is โYouโ, Black is โOppโ.
Chess Backend โ Proposed Architecture
Right now this page is local-only โ board in JS, ELO in localStorage, AI in the browser. This is the map to make it real: same board, same ELO formula, but games live on the server, moves are validated server-side, and ratings persist across devices.
01 / Files to create
api/chess/create_game.php POST /api/chess/create_game
api/chess/get_game.php GET /api/chess/get_game?id={{game_id}}
api/chess/make_move.php POST /api/chess/make_move
api/chess/resign.php POST /api/chess/resign
api/chess/list_games.php GET /api/chess/list_games
api/chess/get_rating.php GET /api/chess/get_rating
api/chess/leaderboard.php GET /api/chess/leaderboard
sql/chess_tables.sql
Keep the tinyfiles PHP pattern: /api/bootstrap.php, json_in() / json_out(), member session, prepared PDO. Server is authoritative โ browser moves are suggestions, server validates legality.
02 / Database tables
-- sql/chess_tables.sql
chess_games
id CHAR(36) PRIMARY KEY
white_member VARCHAR(80) NOT NULL
black_member VARCHAR(80) NOT NULL -- 'AI_EASY' / 'AI_MED' / 'AI_HARD' for vs AI
mode ENUM('ai','2p') NOT NULL
difficulty ENUM('easy','med','hard') NULL
status ENUM('active','checkmate','stalemate','resigned','draw') NOT NULL DEFAULT 'active'
result VARCHAR(20) NULL -- 'white_win' / 'black_win' / 'draw'
fen TEXT NOT NULL -- current position, e.g. start FEN
turn ENUM('w','b') NOT NULL DEFAULT 'w'
created_at DATETIME
updated_at DATETIME
chess_moves
id BIGINT AUTO_INCREMENT PRIMARY KEY
game_id CHAR(36) NOT NULL
move_number INT NOT NULL
from_sq VARCHAR(2) NOT NULL -- e.g. 'e2'
to_sq VARCHAR(2) NOT NULL -- e.g. 'e4'
promotion VARCHAR(1) NULL -- 'Q' if promotion
san VARCHAR(10) NULL -- e.g. 'Nf3', 'O-O'
fen_after TEXT NOT NULL
created_at DATETIME
INDEX (game_id, move_number)
chess_ratings
member VARCHAR(80) PRIMARY KEY
rating INT NOT NULL DEFAULT 1000
wins INT NOT NULL DEFAULT 0
losses INT NOT NULL DEFAULT 0
draws INT NOT NULL DEFAULT 0
last_game_at DATETIME NULL
03 / API endpoints & request flow
POST /api/chess/create_game
{ "mode": "ai", "difficulty": "easy" }
-> { "ok": true, "game_id": "{{uuid}}", "fen": "start", "white_rating": 1000, "black_rating": 1000 }
POST /api/chess/make_move
{ "game_id": "{{uuid}}", "from": "e2", "to": "e4", "promotion": null }
- server: load FEN, generate legal moves (same rules as browser: castling, en passant, check)
- if illegal -> { "ok": false, "error": "illegal_move" }
- if legal -> apply, save chess_moves row, update FEN, check checkmate / stalemate
- if vs AI and game still active -> server makes AI move, saves it too
- if game over -> update chess_ratings for both sides (K=32, same formula as page)
GET /api/chess/get_game?id={{uuid}}
-> { "ok": true, "fen": "...", "history": [ { "from": "e2", "to": "e4" } ], "status": "active" }
POST /api/chess/resign
{ "game_id": "{{uuid}}" }
- winner = opponent, ratings updated as loss for resigner
GET /api/chess/get_rating
-> { "ok": true, "rating": 1042, "wins": 5, "losses": 3, "draws": 1 }
GET /api/chess/leaderboard
-> { "ok": true, "rows": [ { "member": "...", "rating": 1320, "wins": 12 } ] }
04 / ELO โ server-side (same as page)
// Server owns ratings. Browser ELO is a preview only. expectedWhite = 1 / (1 + 10 ** ((blackRating - whiteRating) / 400)) expectedBlack = 1 - expectedWhite whiteNew = whiteRating + 32 * (scoreWhite - expectedWhite) // score: 1 win, 0.5 draw, 0 loss blackNew = blackRating + 32 * (scoreBlack - expectedBlack) // vs AI: AI rating is fixed per difficulty, does NOT move: // Easy = 800, Medium = 1000, Hard = 1200 (template values) // Player rating moves, AI rating stays โ so you can climb past the AI. // vs 2P: both members' ratings move, same K=32. // No rating change if game is resigned before move 5 (anti-farming, template rule).
05 / Front-end integration contract
// When API exists:
// - newGame() -> POST /api/chess/create_game -> store game_id
// - doMove() -> POST /api/chess/make_move { game_id, from, to } -> use server FEN, not local board
// - renderElo() -> GET /api/chess/get_rating -> show server rating, not localStorage
// - Keep local board + localStorage ELO as offline fallback if API is down
// Recommended states: loading, live, simulation, illegal move, game over, server unavailable.
// Never show a server rating as local, and vice versa. If server and local disagree, server wins.
Build order: 1) tables + FEN, 2) create_game + get_game + make_move (server validation), 3) resign + game over, 4) ratings + leaderboard, 5) AI on server (same material + minimax, depth 2 for Hard).