{"id":26046617,"url":"https://github.com/ratwolfzero/nim","last_synced_at":"2025-08-23T01:04:26.737Z","repository":{"id":280745076,"uuid":"943034742","full_name":"ratwolfzero/NIM","owner":"ratwolfzero","description":"The Secret of NIM","archived":false,"fork":false,"pushed_at":"2025-05-26T22:25:37.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-26T23:32:18.975Z","etag":null,"topics":["game-of-nim","nim","nim-sum","nim-value","nimrod","xor"],"latest_commit_sha":null,"homepage":"https://github.com/ratwolfzero/NIM","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/ratwolfzero.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,"zenodo":null}},"created_at":"2025-03-05T04:21:21.000Z","updated_at":"2025-05-26T22:25:40.000Z","dependencies_parsed_at":"2025-05-26T23:33:09.765Z","dependency_job_id":null,"html_url":"https://github.com/ratwolfzero/NIM","commit_stats":null,"previous_names":["ratwolfzero/nim"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ratwolfzero/NIM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratwolfzero%2FNIM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratwolfzero%2FNIM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratwolfzero%2FNIM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratwolfzero%2FNIM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ratwolfzero","download_url":"https://codeload.github.com/ratwolfzero/NIM/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ratwolfzero%2FNIM/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271727437,"owners_count":24810561,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"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":["game-of-nim","nim","nim-sum","nim-value","nimrod","xor"],"created_at":"2025-03-07T21:11:47.587Z","updated_at":"2025-08-23T01:04:26.717Z","avatar_url":"https://github.com/ratwolfzero.png","language":"Python","readme":"# The Secret of NIM\n\nThe Game of Nim is a mathematical strategy game where two players take turns removing objects from heaps or piles. The goal is to be the player who takes the last object. Although it may appear complex at first, Nim has a simple yet beautiful winning strategy based on binary arithmetic and the concept of the \"Nim-sum.\"\n\n## Historical Context\n\nThe game of Nim has ancient roots, but its mathematical foundations were laid in 1901 by Charles L. Bouton, who developed a complete mathematical theory of Nim based on binary addition modulo 2 — an operation equivalent to the bitwise XOR of heap sizes, now commonly called the Nim-sum. Nim became a cornerstone of combinatorial game theory and even inspired early computer implementations, such as the 1951 Nimrod computer. Today, it remains a classic example of strategic thinking and binary arithmetic.\n\n![Nim](Nim_1.png)\n![Nim](Nim_2.png)\n\n## How to Play\n\n1. **Setup:** The game starts with several heaps, each containing a specific number of objects (e.g., sticks, stones, coins). You can adjust the number of objects in each heap using the sliders in our interactive visualization. The default setup includes four heaps.\n\n2. **Turns:** Players alternate turns. On each turn, a player **must** select a heap and remove at least one object from it. A player can remove any number of objects from the chosen heap, up to the entire heap.\n\n3. **Winning:** The player who takes the last object wins the game.\n\n---\n\n## Understanding the Nim-Sum: Binary XOR (⊕ - Circle Plus Symbol)\n\nThe key to winning Nim lies in understanding the **Nim-sum**, calculated using the **bitwise XOR operation** (`^` or \"XOR\").\n\n1. **Binary Representation:** Convert the number of objects in each heap to binary. For example, 5 in decimal is `101` in binary. The code uses a 4-bit binary representation, so 5 becomes `0101`.\n\n2. **Bitwise XOR:** The XOR operation compares the bits in the binary representations of heap sizes. If the bits are the same (0 and 0 or 1 and 1), the result is 0. If the bits are different (0 and 1 or 1 and 0), the result is 1. We perform this operation on the binary representations of *all* heap sizes.\n\n---\n\n## Calculating the Nim-sum: An Example\n\nLet’s consider heaps with sizes 1, 3, 5, and 7:\n\n    1 (decimal)  = 0001 (binary)\n    3 (decimal)  = 0011 (binary)\n    5 (decimal)  = 0101 (binary)\n    7 (decimal)  = 0111 (binary)\n\nThe Nim-sum is the result of XORing all heap sizes:\n\n    Nim-sum = 0001 XOR 0011 XOR 0101 XOR 0111 = 0000 (binary) = 0 (decimal)\n\nBy comparing each bit, we get:\n\n- **Rightmost bit:** 1 XOR 1 XOR 1 XOR 1 = 0\n- **Second bit from right:** 0 XOR 1 XOR 0 XOR 1 = 0\n- **Third bit from right:** 0 XOR 0 XOR 1 XOR 1 = 0\n- **Leftmost bit:** 0 XOR 0 XOR 0 XOR 0 = 0\n\nThus, the Nim-sum is `0000` (0 in decimal).\n\n---\n\n## Winning Strategy for Nim\n\n## Key Concepts\n\n### **Unsafe Game States (Nim-sum ≠ 0)**  \n\nA position where the **Nim-sum is not zero** is called an **unsafe game state**.  \nIf it is your turn and you **play optimally**, you can always make a move that forces your opponent into a losing position.  \n\n### **Safe Game States (Nim-sum = 0)**  \n\nA position where the **Nim-sum is zero** is called a **safe game state**.  \nIf you are in a safe game state, **any move you make will leave the Nim-sum nonzero**, giving your opponent a chance to win if they play optimally.  \n\n---\n\n### **How to Win**\n\n#### **Step 1: Calculate the Nim-sum**\n\nBefore making your move, compute the **Nim-sum** of all heap sizes.  \nThe **Nim-sum** is the **bitwise XOR** of all heap values:\n\n$$\n\\large\n\\text{Nim-sum} = h_1 \\oplus h_2 \\oplus \\dots \\oplus h_n\n\\large\n$$\n\n#### **Step 2: Determine Your Position**\n\n- **If the Nim-sum is 0** → You are in a **safe game state** (which means a losing position if your opponent plays optimally).  \n  Any move you make will give your opponent a winning strategy.  \n\n- **If the Nim-sum is not 0** → You are in an **unsafe game state** (which means a winning position if you play optimally).  \n  You should move to a **safe game state** by following Step 3.\n\n#### **Step 3: Move to a Safe Game State**\n\nFind a heap $$\\large h_i \\large$$ where reducing it to:\n\n$$\n\\large\nh_i' = h_i \\oplus \\text{Nim-sum} \\text{ with }  h_i' \u003c h_i\n\\large\n$$\n\nguarantees that the **new Nim-sum becomes 0**, putting your opponent in a losing position.\n\n---\n\n### **Why This Works**\n\nBy ensuring the new Nim-sum is zero, you force your opponent into a position where they **cannot** make a move that results in a Nim-sum of zero again.  \n\nIn mathematical terms, each heap in Nim has a Grundy number equal to its size — a value that allows us to generalize Nim’s logic to many other games.\n\n## Beyond Nim: How Grundy Numbers (Nimbers) Solve Impartial Games\n\nWhile Nim itself is a classic and elegant game, the concept of Grundy numbers (also known as nimbers) extends far beyond it. These numbers provide a universal way to analyze and solve a wide range of impartial games—games where the available moves depend only on the state of the game, not on whose turn it is. By translating complex game positions into equivalent Nim heaps using Grundy numbers, we unlock a powerful and general winning strategy.\n\nThe winning strategy in Nim is based on these Grundy numbers. In Nim, the Grundy number for a heap is simply its size. In other impartial games, a position's Grundy number is found by taking the Mex (Minimum Excluded value) of the set of Grundy numbers of all possible next moves. This means it's the smallest non-negative integer not present in that set. This method allows us to reduce complex impartial games to Nim and apply the same winning strategy.\nSee Sprague–Grundy theorem \u003chttps://en.wikipedia.org/wiki/Sprague–Grundy_theorem\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fratwolfzero%2Fnim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fratwolfzero%2Fnim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fratwolfzero%2Fnim/lists"}