Metadata-Version: 2.5
Name: magmacrunch-solitaire
Version: 0.1.1
Summary: Klondike solitaire, draw three, as the magmacrunch arcade plays it. Terminal version.
Project-URL: Homepage, https://magmacrunch.com/arcade/solitaire/
Author: magmacrunch media
License-Expression: PolyForm-Noncommercial-1.0.0
License-File: LICENSE
License-File: NOTICE
Keywords: arcade,cards,games,klondike,solitaire,terminal,tui
Requires-Python: >=3.10
Requires-Dist: magmacrunch>=0.17.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# Solitaire (terminal)

The magmacrunch arcade's Solitaire, Klondike draw three, in a terminal. It is
the second version of the game, beside `web/`. It runs on the
[magmacrunch](https://github.com/magmacrunch-media/magmacrunch) engine's
terminal backend, either as a cabinet in the magmacrunch arcade or as its own
command, and plays the same either way.

```
pipx install magmacrunch-solitaire
solitaire                    # the title
solitaire --play --seed 7    # straight to a deal, the same deals every time
```

For working on it, from this folder, once the website's `shared/` is linked
in beside `web/` (`node tools/link-shared.mjs` from the repository's root),
which the oracle test needs:

```
pip install -e ".[dev]"
python -m klondike
```

## Keys

A cursor stands for the mouse, and ENTER is a click wherever it stands.

| key | |
|---|---|
| `←` `→` | along the top row (stock, waste, foundations) or across the columns |
| `↑` `↓` | up and down a column's face-up cards, and between the top row and the columns |
| `1` to `7` | a column |
| `ENTER` `SPACE` | click: draw from the stock, pick a card up, or put the held card there |
| `F` | double-click: a card to its foundation, if it can go |
| `D` | draw, wherever the cursor is |
| `X` | click the felt: put the held card down |
| `N` | deal again |
| `H` `S` | help, the scores; the clock stops while they are open |
| `Esc` `Esc` | leave the deal, for the title (and from there, the arcade) |
| `Q` | quit |

The stock shows its back and how many cards are left. The waste shows its
top three, fanned. A column shows its face-down cards as one back and a
count, then its face-up cards one to a row. The held card is lit cyan and
the cursor pink. A win shows the page's win screen: time, moves, score, and
the time bonus, with a NEW HIGH SCORE badge when the score makes the board.
It goes on the board with its time beside it, as the page saves it.

Needs a 41x24 terminal.

## It is the browser's game, on the browser's clock

`klondike/rules.py` is `web/js/game.js`, method for method, with the deck and
its shuffle from the website's `shared/adenosine-cards.js`. The page keeps
its time with a one-second `setInterval`, and that is part of the rules: the
time on the table, and the bonus a win is paid, are whatever that timer last
set. So the port keeps the page's clock too. Its time only moves when the
screen moves it, by the real time gone, and the interval fires at each of
its seconds on the way. A double click is two clicks on one card inside 300
ms of that time. Two tests hold the port to the page:

- **`tests/test_oracle.py`.** `tools/js_oracle.mjs` loads those files
  unmodified on a page that does nothing, with a seeded `Math.random` and a
  clock of its own. It plays 40 games through the `Solitaire` class the way
  the page's clicks reach it: the stock, a card, an empty foundation or
  column, the felt, the menu's pauses and New. Half are a random clicker
  that now and then clicks the same card again quickly. Half are a greedy
  player, which won 16 of its 285 deals. That is 134,958 clicks and clock
  steps. The test makes the same ones through the port and requires the same
  state after every one: every pile, face up or down, the card held and
  where from, moves, score, the seconds on the clock, whether it runs, and
  the win screens set going. Widening the double click to 310 ms fails it at
  seed 1, event 266; a bonus of x36 fails it at seed 2, event 3,194.
- **`tests/test_rules.py`.** It sets up by hand the cases worth pinning,
  including the fixes below, so that undoing one shows as a change.

## Fixed in both

- **Only a column's top card goes to a foundation.** The page checked the card
  picked and moved the column's top card: with the 5♥ and 4♠ face up and the
  hearts at 4, picking the 5♥ put the 4♠ on the hearts. Now nothing moves.
- **A card on a foundation stays there.** Moving one to another foundation
  counted a move and moved nothing; now it counts nothing.
- **A won table is finished.** It still played: a king taken off its
  foundation and put back won again, with a second bonus and a second score.
  A win now stops the clock for good, and Help and the scores no longer start
  it again.
- **The bonus is paid on the time to the win,** read at the winning move, not
  on the timer's last tick up to a second before it. A win under 30 seconds is
  paid as 30, so the bonus never falls as a win gets faster: it was 24,500,000
  at one second and nothing under one.
- **The clock holds for every reason at once.** A modal and the page being
  hidden each hold it, and it runs again only once neither does. A new deal
  starts it clean, and drops a win screen still waiting from the last deal.
- **A draw puts down a waste card held.** The page kept it held by its old
  place on the waste, so the next move took whatever card the draw had put
  there. Worse, holding the waste's king, turning the waste back into the
  stock and clicking an empty column pushed `undefined` into the column, and
  every render after threw: in Chromium, "Cannot read properties of
  undefined (reading 'getHTML')", and the table stopped answering clicks.
  `drawFromStock` and `draw_from_stock` now put a held waste card down first;
  a tableau card held stays held. In Chromium the same clicks on the fixed
  page leave the column empty, and the king, drawn again, goes there. Taking
  the fix out of the port alone fails the oracle at seed 1, event 252; the
  trace has 57 draws with a waste card held, three of them turn-overs.

## Licence

PolyForm Noncommercial 1.0.0, as for the rest of this repository. See
`LICENSE` and `NOTICE`.
