{"id":22370115,"url":"https://github.com/dasld/py2048base","last_synced_at":"2025-06-21T21:03:04.892Z","repository":{"id":62578416,"uuid":"340512529","full_name":"dasld/py2048base","owner":"dasld","description":"Python clone of the famous 2048 game.","archived":false,"fork":false,"pushed_at":"2021-08-06T17:49:35.000Z","size":315,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-27T06:07:08.259Z","etag":null,"topics":["2048","beginner-friendly","game","puzzle"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/py2048base","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dasld.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-19T23:17:08.000Z","updated_at":"2021-10-20T22:27:17.000Z","dependencies_parsed_at":"2022-11-03T21:01:27.985Z","dependency_job_id":null,"html_url":"https://github.com/dasld/py2048base","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dasld/py2048base","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasld%2Fpy2048base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasld%2Fpy2048base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasld%2Fpy2048base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasld%2Fpy2048base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dasld","download_url":"https://codeload.github.com/dasld/py2048base/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasld%2Fpy2048base/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261193089,"owners_count":23122904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["2048","beginner-friendly","game","puzzle"],"created_at":"2024-12-04T19:33:57.015Z","updated_at":"2025-06-21T21:02:59.881Z","avatar_url":"https://github.com/dasld.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"######\npy2048\n######\n\n.. image:: https://img.shields.io/github/license/dasld/py2048base?color=blue\u0026style=flat-square   :alt: GitHub\n.. image:: https://img.shields.io/pypi/v/py2048base?color=green\u0026style=flat-square   :alt: PyPI\n\n**py2048** is a clone of the famous game_ by `Gabriele Cirulli`__.\nIt's probably not very interesting to people looking for playing 2048, but may\nbe useful for people looking for learning Python.\nIt's written in pure Python 3.8 and makes an effort not to use third-party libraries.\nFor example, the game grid could be a numpy matrix, but I'd rather not break a butterfly\nupon a wheel.\nThe game's been divided in two parts:\nthe backend, and the frontends (or interfaces).\n\nThe package in this repository provides no concrete user interface for the game,\nbut contains a class that must be inherited by every actual frontend.\n\n\n********\nFeatures\n********\n\n* Customizable rules\n      You can configure the size of the game grid and the winning condition\n      (wanna go beyond 2048?);\n* Customizable looks\n      By subclassing the basic frontend and overriding its\n      methods, you can make the game look any way you want, without touching the\n      basic game logic;\n* Automatic mode\n      You can replace human input with a function that picks a\n      random direction! More useful for testing than actual gameplay, though.\n\n\n**********\nHow to use\n**********\n\nTo implement a frontend\n=======================\n\nThe backend takes input (in the form of a `py2048.Directions` object),\nprocesses it, and waits for the next input.\nThis loops until\nthe player exits, or\nthe player wins for the first time in this session, or\nthe player runs out of valid movements.\nIt's a pure-Python implementation of the original game logic:\nit makes equal numbers merge into their sum, \"moving\" from one tile into\nanother, and so on.\nThe backend deals only with plain Python data, such as `int` and `dict`;\nit is up to each frontend to actually collect the input and display the\nupdated game state as a (hopefully) pretty interface.\n\nTo write such frontend, subclass `py2048.basefrontend.Base2048Frontend`\nand override the following methods:\n\n* `choose_direction`\n* `on_player_quit`\n* `on_player_victory`\n* `on_player_overvictory`\n* `on_player_loss`\n\n`choose_direction` must return exactly one `py2048.Directions` object.\nIf writing a graphical frontend (a GUI), you'll probably want to create a \"worker\"\nthread that runs the backend's loop and waits for a condition when it reaches\nthe `choose_direction` call.\nThe main thread will then be responsible for storing user input somewhere accessible\nby the worker thread and \"waking it up\".\nThe other overriden methods shouldn't return anything.\n`player_quit` is called when the player exits the game before winning or\nlosing.\nAn \"overvictory\" is what happens when the grid \"jams\" (runs out of valid movements)\nbut the player had already won (had already reached the target goal number).\n\nTo run tests\n============\n\nWe're using the `pytest` library to check the correctness of the basic\ndata structures.\nTo run the tests, open a terminal, move into the root folder of the project\n(the one which contains `setup.py`), and type the following:\n\n    pytest-3 -v py2048/test.py\n\nMake sure you're in the right folder,\nthat there are no slashes in the command, and\nthat it doesn't end with *.py*.\nAlternatively, use\n\n    make test\n\n\n********\nWishlist\n********\n\nIn the future, I'd like to create an AI to play the game!\n\n\n------------\n\n************\nHow it works\n************\n\nBackend\n=======\n\nIts main components are the `Cell` and the `Grid`.\nThe `Cell` class is a wrapper over an `int` that represents a tile in the game\ngrid.\nIt has a `locked` property, which is simply a `bool` that determines whether it\ncan change its stored `int` in this cycle.\nA cycle is what happens between the arrival of a valid user input and the moment\nthe game pauses to get the next input.\nThis `locked` property prevents a 2 merging into a 2 and the resulting 4 merging\ninto another 4 all in a single cycle, for example.\nIf you play the original game, you'll notice that is not allowed.\n\nThe `Grid` is a wrapper over a `dict` that maps points into `Cells`\n(and points are named tuples that store x and y coordinates).\nThis class (along with `py2048.basefrontend.Base2048Frontend`), implements most\nof the game logic.\nIt's responsible for determining how and when one `Cell` can merge into another,\nupdating the score, and so on.\n\n\nFrontend\n========\n\n\n\n\n.. _game: https://play2048.co/\n.. _cirulli: http://gabrielecirulli.com\n__ cirulli_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasld%2Fpy2048base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdasld%2Fpy2048base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasld%2Fpy2048base/lists"}