{"id":24313627,"url":"https://github.com/stuarthayhurst/battleships","last_synced_at":"2026-04-21T19:33:44.342Z","repository":{"id":188923636,"uuid":"469825640","full_name":"stuarthayhurst/battleships","owner":"stuarthayhurst","description":"Battleships opponent and compute experiments, with AVX2 / AVX-512","archived":false,"fork":false,"pushed_at":"2026-02-25T17:22:09.000Z","size":113,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-02-25T20:39:30.082Z","etag":null,"topics":["avx2","avx512","battleships","compute","opponent"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stuarthayhurst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"stuarthayhurst","ko_fi":"stuarthayhurst","custom":"https://paypal.me/stuartahayhurst"}},"created_at":"2022-03-14T16:54:57.000Z","updated_at":"2026-02-25T17:22:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"85426786-7590-4974-bbb1-3487f85ccce6","html_url":"https://github.com/stuarthayhurst/battleships","commit_stats":null,"previous_names":["stuarthayhurst/battleships"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/stuarthayhurst/battleships","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuarthayhurst%2Fbattleships","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuarthayhurst%2Fbattleships/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuarthayhurst%2Fbattleships/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuarthayhurst%2Fbattleships/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stuarthayhurst","download_url":"https://codeload.github.com/stuarthayhurst/battleships/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stuarthayhurst%2Fbattleships/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32106682,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-21T11:25:29.218Z","status":"ssl_error","status_checked_at":"2026-04-21T11:25:28.499Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avx2","avx512","battleships","compute","opponent"],"created_at":"2025-01-17T09:12:43.148Z","updated_at":"2026-04-21T19:33:44.334Z","avatar_url":"https://github.com/stuarthayhurst.png","language":"Python","funding_links":["https://github.com/sponsors/stuarthayhurst","https://ko-fi.com/stuarthayhurst","https://paypal.me/stuartahayhurst"],"categories":[],"sub_categories":[],"readme":"## battleships\n  - A repository to experiment with the battleships board game\n  - I don't particularly like battleships, but it makes for interesting problem solving\n\n## Sub-projects:\n  - Battleships computer opponent: A computer opponent to play battleships against\n  - Battleships board compute: Find every valid battleships layout, according to grid size and a set of ship lengths\n    - Includes AVX2 and AVX-512 accelerated C implementations\n\n## Battleships computer opponent:\n  - `battleships.py` runs a game of battleships, with options to play with 2 players, against the computer, or watch the computer play against a random number generator\n  - The computer opponent has been designed to be as hard as is possible\n  - The opponent plays completely fairly, and can't see the locations of your ships (read the source code if you don't believe me)\n  - The opponents are stored in `opponents/`, and can be benchmarked with `./benchmark.py`\n\n## Battleships board compute:\n  - `compute/countBoards.py` will calculate the number of valid battleships layouts from a grid size and list of ship lengths\n    - This code has been written with `pypy3` in mind, and is strongly suggested to be used (~5x performance improvement)\n    - This script takes around 13.5 seconds to run using a Ryzen 7 7700X and `pypy3`\n      - With `n` being the number of ships and `w` being with width of the board, the time complexity scales with `O((w^2 * 2)^n * n!)`\n        - This is the unoptimised time complexity, if every board was checked\n        - In reality, it likely won't scale this way, as most boards are discarded early\n      - Using 5 ships and a width of 7, this gives ~1.1 trillion combinations to try\n  - **Alternatively**, `compute/countBoards.c` is a C implementation of the same algorithm\n    - This runs in about 0.5 seconds, using a Ryzen 7 7700X\n      - However, optimisation work has only been done on Zen 3, Zen 3+ and Zen 4 systems\n    - Compile: `make -C compute`\n      - Supports `DEBUG=[true/false]` to enable debug support and verbose build output\n      - Supports `VERBOSE=[true/false]` to enable verbose build output\n      - Supports `ARCH=[microarchitecture]` to target a specific microarchitecture\n        - Defaults to using `-march=native`\n        - `ARCH=x86-64` could be helpful to run on any x86-64 CPU, if being used to benchmark\n      - Supports `AVX2=[true/false]` to enable AVX2 optimisations\n      - Supports `AVX512=[true/false]` to enable AVX-512 optimisations\n      - Supports `AVX512_SHORT=[true/false]` to enable AVX-512 optimisations, with 8-bit elements\n        - AVX2, AVX-512 and AVX-512 8-bit optimisations are enabled by default\n        - They'll be used in order of AVX-512 8-bit -\u003e AVX-512 -\u003e AVX2 -\u003e scalar\n          - The Makefile selects which options the code is allowed to use\n          - The code tests for support at compile time and makes a decision\n      - Supports `BOARD_TYPE_SIZE=[integer]` to force a specific board element size\n        - Defaults to `32`\n        - If `AVX512_SHORT` is enabled and supported, this will be overridden\n      - Supports `BOARD_WIDTH=[integer]` to force a select board width\n        - Defaults to `7`\n    - Run: `./compute/countBoards`\n  - These programs don't save the boards, but could easily be modified to save or print them\n  - Comparison of implementation performance (Ryzen 7 7700X):\n    - Scalar code is free to be auto-vectorised by the compiler\n\n    | Runner + version      | Runtime | Valid boards / s |\n    |:----------------------|:--------|:-----------------|\n    | Python (3.12)         | 71.56s  | 1,743,000        |\n    | Pypy3 (3.10 / 7.3.16) | 13.28s  | 9,394,000        |\n    | C (Scalar) (GCC-15)   | 0.50s   | 250,900,000      |\n    | C (AVX2) (GCC-15)     | 0.50s   | 251,700,000      |\n    | C (AVX-512) (GCC-15)  | 0.53s   | 236,500,000      |\n    | C (AVX-512S) (GCC-15) | 0.42s   | 299,500,000      |\n\n    - Runtime is rounded to 2 decimal places\n    - Number of valid boards per second is rounded to 4 significant figures\n  - Comparison of SIMD selection performance by runtime (Ryzen 7 9700X, GCC-14):\n\n    | SIMD    | 9x9    | 10x10   | 11x11   | 12x12    | 13x13    |\n    |:--------|:-------|:--------|:--------|:---------|:---------|\n    | Scalar  | 18.74s | 67.56s  | 379.26s | 1396.50s |          |\n    | AVX2    | 26.94s | 89.10s  | 531.56s | 1633.51s |          |\n    | AVX512  | 19.71s | 121.06s | 363.69s | 1330.86s |          |\n    | AVX512S | 16.02s | 83.26s  | 234.88s | 786.12s  | 2087.83s |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuarthayhurst%2Fbattleships","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstuarthayhurst%2Fbattleships","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuarthayhurst%2Fbattleships/lists"}