Skip to content

About

Play Tic Tac Toe against the computer.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Tic-Tac-Toe

Play in your browser

This game allows you to play Tic Tac Toe against the computer.

The 3D board mid-game: a 3 by 3 grid of white tiles in a dark frame, two red X pieces and two blue O rings standing on their cells

The 3D window, rendered by python3 tools/strata_snapshot.py (the board screen mid-game).

Play in your browser

The game is also built for the browser with pygbag and served at https://tic-tak-toe.play.danielstephenson.dev, alongside the other games at https://danielstephenson.dev/play. Nothing needs to be installed: the page loads Python and pygame compiled to WebAssembly, then asks for a click or tap to start. The board and buttons work with a mouse or a tap on a phone. There is no Quit button in the browser build (closing the tab is how to leave).

To build and serve it locally:

python3 -m pip install pygbag==0.9.3
python3 -m pip install --no-deps --target src "$(grep -E '^graphik @ ' requirements.txt)"
python3 -m pygbag --build --template "$(PYTHONPATH=src python3 -c 'from preponderous.graphik.grid3d.web import templatePath; print(templatePath())')" src
PYTHONPATH=src python3 -m preponderous.graphik.grid3d.relay --strata 0.9.0 --dest src/build/web
python3 -m http.server 8000 --bind 127.0.0.1 --directory src/build/web

then open http://127.0.0.1:8000 (pygbag looks for its packages on a local CDN when the page is served from localhost, so the address matters). pygbag packs only what is under src/, so the second line copies the pinned graphik release into src/preponderous/ first, as .github/workflows/browser.yml does. src/main.py is the browser entry point; src/tictactoe.py stays the desktop one. Neither the build output in src/build/ nor src/preponderous/ is committed.

The page starts in 3D, as the desktop game does (see 3D view). The 2D button in its top-right corner switches to the 2D view and 3D switches back; either way works at any moment, in the middle of a game too. The page remembers a player's choice of 2D, and ?view=2d (or ?view=3d) at the end of the address picks the view for that visit. The page is built with graphik's pygbag template, and the fourth line above copies Strata's prebuilt grid relay (from Strata's 0.9.0 release, checked against its SHA256SUMS) next to it. The 3D view is about 1.3 MB; a page that starts in 2D downloads none of it until the button is pressed. If 3D can't start (no WebGL 2, for example), the 2D view stays, the button says "3D unavailable", and the page and the game say why.

For the browser, every screen draws one frame per pass and yields to the event loop with await asyncio.sleep(0), and the one-second pauses use await asyncio.sleep(1), so the screens, the cell handlers and the win check are coroutines. A button is pressed by the left-button press event that lands on it, once per click or tap (a browser tap delivers its press and release in the same frame, so graphik's held-button check is not used for pressing). Presses made during the one-second pause before an end screen are discarded, because Play Again is drawn over the bottom-middle cell. Presses made during the computer's one-second pause are discarded too, so a tap is not played as the next move before the computer's mark has been seen. Presses made in the half second after Start is pressed are discarded as well, because Start is drawn over the centre cell and the second press of a double-click would otherwise play a move there.

Play in 3D or 2D

The 2D/3D button in the page's top-right corner switches between them at any moment, and the page remembers a choice of 2D.

A game won in 3D in the browser: X is placed by clicking a tile, which lights yellow under the pointer, the computer answers with blue O rings, and X completes the left column; the board dims under You won! and Play Again The same win on a phone held upright: You won! above the dimmed 3D board, Play Again below it and the 2D button in the top-right corner

Recorded from a local build of the browser page (Playwright, headless Chromium): a game won with clicks on a 1280x800 desktop (left, cropped to the board), and the end of a game played with taps on a 390x844 phone (right).

Getting Started

Requirements

  • Python 3
  • pygame
  • graphik, installed by pip install -r requirements.txt from the release pinned there

Installation

pip install -r requirements.txt

Running the game

python3 src/tictactoe.py

On Linux and Windows this opens the 3D window when it can (see below) and otherwise says why in plain words and opens the 2D window. --2d always opens the 2D window; --strata insists on 3D and stops with the reason instead of falling back.

3D view

The desktop game can also be played in 3D, drawn with Strata through graphik's 3D grid view (preponderous.graphik.grid3d). It is the same game: the title screen, Start, the board, the computer's move after a one-second pause, the three end screens with Play Again and Quit, hover feedback on every button and cell, and the rule that presses made during a pause are dropped all come from src/tictactoe.py unchanged. The board is a 3x3 grid of tiles with an X or an O standing on each claimed cell; a click lands on the tile under the pointer, and the other buttons are drawn as panels in the same places as in the 2D window. Closing the window quits, as in 2D. Neither window binds any keys.

It needs a graphik release that includes the 3D grid view, and Strata's Python binding:

pip install "strata-engine @ git+https://github.com/Preponderous-Software/[email protected]#subdirectory=bindings/python"

The binding downloads Strata's library (pinned by SHA-256) the first time it runs. Strata ships Linux x64 and Windows x64 libraries, so there is no 3D window on macOS, which keeps the 2D window.

The browser build has the same 3D view (src/strata_view/web.py). There the game is the 2D game, which also sends each frame the screen draws to the page's Strata grid relay, as the scene the desktop window draws, while the page shows 3D. Every screen, button, pause and rule is the 2D game's, so the 3D view in the browser has the same title screen and Start (and, as in the 2D browser build, no Quit), the same board, the computer's move after its pause, the three end screens with Play Again, and the rule that presses made during a pause are dropped. A tap or click lands on the tile or panel under it in 3D and runs the handler the 2D press would; the mouse lifts the button or lights the tile under it, as in 2D (a touch has no hover). On a phone held upright the board fills the width of the screen (graphik frames it for the window's shape), the button labels are kept at a readable size and the buttons widen to fit them, and the headlines and buttons keep out from under the page's 2D/3D button. src/strata_view/ holds the view: scene.py maps the game and each screen's text and buttons to shapes and needs no GPU, and game.py runs the game's own screens in a Strata window. python3 tools/strata_snapshot.py --out shots/ renders every screen headless to PNGs. The 3D tests under tests/view3d/ are skipped where the installed graphik has no 3D grid view.

Running the tests

The tests use pytest and run headlessly, so no display is required. They run the screens and handlers with asyncio.run and replace asyncio.sleep, so nothing actually waits.

pip install -r requirements-dev.txt
python3 -m pytest

Continuous integration

.github/workflows/ci.yml runs the tests on every push to master and every pull request, then launches the game under the dummy SDL drivers to confirm it starts and keeps running.

.github/workflows/browser.yml runs on the same events and builds the browser version with pygbag, with graphik's page template, puts Strata's grid relay next to the page and checks that the page, its 2D/3D button and the relay are there. On a manual run (workflow_dispatch), or on a push to master once the repository variable ARCADE_ENABLED is true, it deploys the build to arcade with arcade-deploy, as version <version.txt>+g<short commit>; the upload token is the ARCADE_TOKEN secret.

Support

You can find the support discord server here.

Authors and acknowledgement

Developers

Name Main Contributions
Daniel Stephenson Creator

About

Play Tic Tac Toe against the computer.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages