{"id":20761885,"url":"https://github.com/james-p-d/pegsolitaire","last_synced_at":"2026-05-28T11:31:07.848Z","repository":{"id":170649453,"uuid":"275200763","full_name":"James-P-D/PegSolitaire","owner":"James-P-D","description":"Peg Solitaire solver in OCaml","archived":false,"fork":false,"pushed_at":"2021-07-04T20:44:29.000Z","size":14266,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-25T23:37:06.956Z","etag":null,"topics":["ocaml","peg-solitaire","solitaire"],"latest_commit_sha":null,"homepage":"","language":"OCaml","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/James-P-D.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2020-06-26T16:30:21.000Z","updated_at":"2021-07-04T20:44:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"60567453-fe6e-4742-b84c-e16a862f94ad","html_url":"https://github.com/James-P-D/PegSolitaire","commit_stats":null,"previous_names":["james-p-d/pegsolitaire"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/James-P-D/PegSolitaire","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/James-P-D%2FPegSolitaire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/James-P-D%2FPegSolitaire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/James-P-D%2FPegSolitaire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/James-P-D%2FPegSolitaire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/James-P-D","download_url":"https://codeload.github.com/James-P-D/PegSolitaire/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/James-P-D%2FPegSolitaire/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33607334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-28T02:00:06.440Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["ocaml","peg-solitaire","solitaire"],"created_at":"2024-11-17T10:27:13.050Z","updated_at":"2026-05-28T11:31:07.833Z","avatar_url":"https://github.com/James-P-D.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PegSolitaire\n\n[Peg Solitaire](https://en.wikipedia.org/wiki/Peg_solitaire) solver in OCaml\n\n![Screenshot](https://github.com/James-P-D/PegSolitaire/blob/master/screenshot.gif)\n\n## Details\n\nPeg-Solitaire is a one-player game in which a number of marbles are placed on a grid with one empty location in the centre. The objective of the game is to remove marbles from the grid by jumping over them into empty locations, eventually resulting in a board which contains only one marble in the centre cell. Note that marbles can only jump horizontally and vertically and not diagonally, and that they can only jump over pieces that are immediate neighbours.\n\nFor example, given the initial [English Peg Solitaire](https://en.wikipedia.org/wiki/Peg_solitaire#Board) board:\n\n```\n   0  1  2  3  4  5  6 \n0        O  O  O       \n1        O  O  O       \n2  O  O  O  O  O  O  O \n3  O  O  O  -  O  O  O \n4  O  O  O  O  O  O  O \n5        O  O  O       \n6        O  O  O       \n```\n\nThe player could choose to move the marble at `(3, 1)` to `(3, 3)`, which would cause the removal of the marble at `(3, 2)`:\n\n```\n   0  1  2  3  4  5  6 \n0        O  O  O       \n1        O  -  O       \n2  O  O  O  -  O  O  O \n3  O  O  O  O  O  O  O \n4  O  O  O  O  O  O  O \n5        O  O  O       \n6        O  O  O       \n```\n\nThen the player could move `(5, 2)` to `(3, 2)` which would remove `(4, 2)`:\n\n```\n   0  1  2  3  4  5  6 \n0        O  O  O       \n1        O  -  O       \n2  O  O  O  O  -  -  O \n3  O  O  O  O  O  O  O \n4  O  O  O  O  O  O  O \n5        O  O  O       \n6        O  O  O       \n```\n\nIf we make a series of bad decisions, we might find ourselves with a board like this:\n\n```\n    0  1  2  3  4  5  6 \n0         O  O  O\n1         O  -  O\n2   O  O  O  O  O  O  O\n3   O  -  O  -  O  -  -\n4   O  O  O  O  O  O  O\n5         O  -  O\n6         O  -  O\n```\n\nAt this point we are stuck because there are no more valid moves we can make. This is a terminal state.\n\nHowever, if we choose wisely, we'll end up with a board with a single marble at `(3, 3)`, which is the completed state:\n\n```\n    0  1  2  3  4  5  6 \n0         -  -  -\n1         -  -  -\n2   -  -  -  -  -  -  -\n3   -  -  -  O  -  -  -\n4   -  -  -  -  -  -  -\n5         -  -  -\n6         -  -  -\n```\n\nFor much more information on the puzzle see [Durango Bill's website](http://www.durangobill.com/Peg33.html#:~:text=The%204%20possible%20legal%20moves,center%20hole%2C%20the%20player%20wins.)\n\n## Algorithm\n\nThe OCaml program performs a [depth-first search](https://en.wikipedia.org/wiki/Depth-first_search) recursively on the board. If it encounters a terminal state (i.e. a board where there are no more possible moves) it will backtrack through previous states until it encounters a move it hasn't yet made. This process is repeated until we reach the complete state (i.e. a board with only one marble left at cell `(3, 3)`), at which point it will unwind and produce a list of moves which will take us from the start to the completed board. If the program is unable to reach the complete state it will return an empty list.\n\nThis list returned will be comprised of tuples in the form `(x1, y1, x2, y2)` where `(x1, y1)` is the position of a marble and `(x2, y2)` is the position of an empty cell. We can then run this list through the `apply_move_list` function to step-through each state from beginning to end.\n\n## Running\n\nThe application has been tested with [OCaml v4.10.0](https://ocaml.org/docs/install.html). Simply run the `OCaml64` shortcut, change to the folder containing [solitaire.ml](https://github.com/James-P-D/PegSolitaire/blob/master/src/solitaire.ml) and then use `ocamlopt` to compile to native code:\n\n```\njdorr@DESKTOP-MF9T345 /cygdrive/c/Users/jdorr/Desktop/Dev/PegSolitaire/src\n$ ocamlopt -o solitaire solitaire.ml\n```\n\nYou can then run the `./solitaire.exe` executable:\n\n```\njdorr@DESKTOP-MF9T345 /cygdrive/c/Users/jdorr/Desktop/Dev/PegSolitaire/src\n$ ./solitaire.exe\n   0  1  2  3  4  5  6\n0        O  O  O\n1        O  O  O\n2  O  O  O  O  O  O  O\n3  O  O  O  -  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n```\n\nThere will be a long pause whilst our program searches for a solution to the puzzle. After ~20 seconds you should see the following:\n\n```\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  O  -  -  -\n4  -  -  -  -  -  -  -\n5        -  -  -\n6        -  -  -\n\nSuccess!\nPress ENTER to step through moves to complete puzzle\n```\n\nOur program has found a solution which can now be stepped-through by pressing \u003ckbd\u003eenter\u003c/kbd\u003e:\n\n```\n   0  1  2  3  4  5  6\n0        O  O  O\n1        O  O  O\n2  O  O  O  O  O  O  O\n3  O  O  O  -  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(3, 1) -\u003e (3, 3)\n   0  1  2  3  4  5  6\n0        O  O  O\n1        O  -  O\n2  O  O  O  -  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(1, 2) -\u003e (3, 2)\n   0  1  2  3  4  5  6\n0        O  O  O\n1        O  -  O\n2  O  -  -  O  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(2, 0) -\u003e (2, 2)\n   0  1  2  3  4  5  6\n0        -  O  O\n1        -  -  O\n2  O  -  O  O  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(4, 0) -\u003e (2, 0)\n   0  1  2  3  4  5  6\n0        O  -  -\n1        -  -  O\n2  O  -  O  O  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(3, 2) -\u003e (1, 2)\n   0  1  2  3  4  5  6\n0        O  -  -\n1        -  -  O\n2  O  O  -  -  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(0, 2) -\u003e (2, 2)\n   0  1  2  3  4  5  6\n0        O  -  -\n1        -  -  O\n2  -  -  O  -  O  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(4, 2) -\u003e (4, 0)\n   0  1  2  3  4  5  6\n0        O  -  O\n1        -  -  -\n2  -  -  O  -  -  O  O\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(6, 2) -\u003e (4, 2)\n   0  1  2  3  4  5  6\n0        O  -  O\n1        -  -  -\n2  -  -  O  -  O  -  -\n3  O  O  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(2, 3) -\u003e (2, 1)\n   0  1  2  3  4  5  6\n0        O  -  O\n1        O  -  -\n2  -  -  -  -  O  -  -\n3  O  O  -  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(2, 0) -\u003e (2, 2)\n   0  1  2  3  4  5  6\n0        -  -  O\n1        -  -  -\n2  -  -  O  -  O  -  -\n3  O  O  -  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(0, 3) -\u003e (2, 3)\n   0  1  2  3  4  5  6\n0        -  -  O\n1        -  -  -\n2  -  -  O  -  O  -  -\n3  -  -  O  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(2, 3) -\u003e (2, 1)\n   0  1  2  3  4  5  6\n0        -  -  O\n1        O  -  -\n2  -  -  -  -  O  -  -\n3  -  -  -  O  O  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(4, 3) -\u003e (4, 1)\n   0  1  2  3  4  5  6\n0        -  -  O\n1        O  -  O\n2  -  -  -  -  -  -  -\n3  -  -  -  O  -  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(4, 0) -\u003e (4, 2)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  -\n2  -  -  -  -  O  -  -\n3  -  -  -  O  -  O  O\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(6, 3) -\u003e (4, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  -\n2  -  -  -  -  O  -  -\n3  -  -  -  O  O  -  -\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(4, 3) -\u003e (4, 1)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  O\n2  -  -  -  -  -  -  -\n3  -  -  -  O  -  -  -\n4  O  O  O  O  O  O  O\n5        O  O  O\n6        O  O  O\n\n(2, 5) -\u003e (2, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  O\n2  -  -  -  -  -  -  -\n3  -  -  O  O  -  -  -\n4  O  O  -  O  O  O  O\n5        -  O  O\n6        O  O  O\n\n(0, 4) -\u003e (2, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  O\n2  -  -  -  -  -  -  -\n3  -  -  O  O  -  -  -\n4  -  -  O  O  O  O  O\n5        -  O  O\n6        O  O  O\n\n(2, 4) -\u003e (2, 2)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        O  -  O\n2  -  -  O  -  -  -  -\n3  -  -  -  O  -  -  -\n4  -  -  -  O  O  O  O\n5        -  O  O\n6        O  O  O\n\n(2, 1) -\u003e (2, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  O\n2  -  -  -  -  -  -  -\n3  -  -  O  O  -  -  -\n4  -  -  -  O  O  O  O\n5        -  O  O\n6        O  O  O\n\n(2, 3) -\u003e (4, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  O\n2  -  -  -  -  -  -  -\n3  -  -  -  -  O  -  -\n4  -  -  -  O  O  O  O\n5        -  O  O\n6        O  O  O\n\n(4, 4) -\u003e (4, 2)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  O\n2  -  -  -  -  O  -  -\n3  -  -  -  -  -  -  -\n4  -  -  -  O  -  O  O\n5        -  O  O\n6        O  O  O\n\n(4, 1) -\u003e (4, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  O  -  -\n4  -  -  -  O  -  O  O\n5        -  O  O\n6        O  O  O\n\n(6, 4) -\u003e (4, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  O  -  -\n4  -  -  -  O  O  -  -\n5        -  O  O\n6        O  O  O\n\n(3, 4) -\u003e (5, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  O  -  -\n4  -  -  -  -  -  O  -\n5        -  O  O\n6        O  O  O\n\n(4, 6) -\u003e (4, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  O  -  -\n4  -  -  -  -  O  O  -\n5        -  O  -\n6        O  O  -\n\n(4, 3) -\u003e (4, 5)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  -  -  -\n4  -  -  -  -  -  O  -\n5        -  O  O\n6        O  O  -\n\n(2, 6) -\u003e (4, 6)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  -  -  -\n4  -  -  -  -  -  O  -\n5        -  O  O\n6        -  -  O\n\n(4, 6) -\u003e (4, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  -  -  -\n4  -  -  -  -  O  O  -\n5        -  O  -\n6        -  -  -\n\n(5, 4) -\u003e (3, 4)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  -  -  -  -\n4  -  -  -  O  -  -  -\n5        -  O  -\n6        -  -  -\n\n(3, 5) -\u003e (3, 3)\n   0  1  2  3  4  5  6\n0        -  -  -\n1        -  -  -\n2  -  -  -  -  -  -  -\n3  -  -  -  O  -  -  -\n4  -  -  -  -  -  -  -\n5        -  -  -\n6        -  -  -\n\njdorr@DESKTOP-MF9T345 /cygdrive/c/Users/jdorr/Desktop/Dev/PegSolitaire/src\n$\n```\n\nFinally, you can also try running the program on the [Try OCAML](https://try.ocamlpro.com/) website. It will be *very* slow (2m40s on my machine), and you'll need to comment-out any `input_line stdin` statements as the site does not appear to support user input. The site will also think that the application is stuck in an infinite loop and attempt to timeout. Simply hit the \u003ckbd\u003e10 seconds!\u003c/kbd\u003e button to bump-up the timeout:\n\n![Screenshot](https://github.com/James-P-D/PegSolitaire/blob/master/tryocamlwebsite.gif)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjames-p-d%2Fpegsolitaire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjames-p-d%2Fpegsolitaire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjames-p-d%2Fpegsolitaire/lists"}