It calculates the Nim sum of a nim game.

Overview

nim-sum-calculator

It calculates the Nim sum of a nim game.

The rules of Nim

The traditional game of Nim is played with a number of coins arranged in heaps: the number of coins and heaps is up to you. There are two players. When it's a player's move he or she can take any number of coins from a single heap. They have to take at least one coin, though, and they can't take coins from more than one heap. The winner is the player who makes the last move, so there are no coins left after that move. (Some people play the game the other way around, with the last person to make a move losing the game)

Winning Strategy

Adding the Nim way (Nim Sum)

The secret to finding the winning strategy hinges on writing the sizes of the heaps (the number of coins in each heap) in binary, and then adding those numbers up — but not using the ordinary way of adding numbers, but something appropriately called Nim addition.

To add some given binary numbers using Nim addition, you first write them underneath each other, as you might for ordinary addition. Then you look at each of the columns in turn. If the number of 1s in a column is odd, you write a 1 underneath it; if it's even, you write a 0 underneath it. Doing this for each column gives a new binary number, and that's the result of the Nim addition.

As an example, let's Nim-add the binary numbers 10, 11, and 100 (which stand for the decimal numbers 2, 3 and 4):

0 1 0
0 1 1
1 0 0

Nim Sum:
1 0 1

So the result, which is called the Nim sum, is the binary number 101. Nim addition is not the same as ordinary addition: the binary number 101 is 5 in decimal, which is not equal to the ordinary sum 2+3+4 = 9.

The Fact of Nim Sum

  • Fact 1: Suppose it's your turn and the Nim sum of the number of coins in the heaps is equal to 0. Then whatever you do, the Nim sum of the number of coins after your move will not be equal to 0.

  • Fact 2: Suppose it's your turn and the Nim sum of the number of coins in the heap is not equal to 0. Then there is a move which ensures that the Nim sum of the number of coins in the heaps after your move is equal to 0.

Now suppose you are player A, so you go first. Also suppose that the Nim sum of the number of coins in the heaps is not equal to 0. Your strategy will be this: if possible always make a move that reduces the next Nim sum, the Nim sum after your move, to 0. This would then mean that whatever player B does next, by fact 1 the move would turn the next Nim sum into a number that's not 0.

References

Plus Magazine: Play to win with Nim

You might also like...
Ice-Walker-Game - This repository is about the Ice Walker game made in Python.

Ice-Walker-Game Ce dépot contient le jeu Ice Walker programmé en Python. Les différentes grilles du jeu sont contenues dans le sous-dossier datas. Vou

Deal Or No Deal was a very popular game show. Even now, for a family party, it's a fun game to pass time

Deal Or No Deal was a very popular game show. Even now, for a family party, it's a fun game to pass time. I made a code to play the game right in your terminal/console. This isn't made to be a game which can be installed by everyone and played, I just made it as a fun project as I just started out with python. So if you have python installed and wanna have some fun, or just curious to see how I did this, feel free to check the code out!

Mastermind-Game - A game to test programming and logical skills

Bem vindo ao jogo Mastermind! O jogo consiste em adivinhar uma senha que será ge

Quiz Game: answering questions naturally with a friendly UI to enjoy the game
Quiz Game: answering questions naturally with a friendly UI to enjoy the game

About Quiz Game : The Game is about answering questions naturally with a friendl

This is a simple telegram bot for the game Pyal, a word guessing game inspired by Wordle

Pyal Telegram Bot This is a simple telegram bot for the game Pyal, a word guessing game inspired by Wordle. How does it work? Differently from the ori

Adventure-Game - Adventure Game which is created using Python

Adventure Game 🌇 This is a Adventure Game which is created using Python. Featur

Game-of-life - A simple python program to simulate and visualise the Conway's Game of life
Game-of-life - A simple python program to simulate and visualise the Conway's Game of life

Conway's game of life A simple python program to simulate and visualise the Conw

Minecraft clone using Python Ursina game engine!
Minecraft clone using Python Ursina game engine!

Minecraft clone using Python Ursina game engine!

A Pygame game made in 48 hours
A Pygame game made in 48 hours

Flappuccino Flappuccino is a game created in 48 hours for the PyGame Community New Years Jam using Python with Pygame. Screenshots Background Informat

Releases(v0.1.1)
Owner
Working as a software engineer intern at a startup based in Hong Kong.
Simple python 3D vector3 math library wrapping some types from GLM library using pybind11.

vmath Simple python 3D vector3 math library wrapping some types from GLM library using pybind11. Description Both pure python version and C++ version

6 Aug 02, 2022
Stock game is a python program that simulates real-life stock marketing, saving, and investments

Stock game is a python program that simulates real-life stock marketing, saving, and investments. Users get to trade and manage their portfolio and manage their 100,000 dollar portfolio.

Sai Praneth Raju K. 1 Jul 14, 2022
Synthesizer based on Conway's Game of Life

Conway Synth Synthesizer based on Conway's Game of Life Trying to avoid step sequencer fashions that have been done before and basing it on actual cel

Giacomo Loparco 4 Mar 15, 2022
Easily manage wine prefixes in a new way. Run Windows software and games on Linux

Bottles Easily manage wineprefix using environments Documentation · Forums · Telegram group · Funding 📚 Documentation Before opening a new issue, che

Bottles 4.1k Jan 09, 2023
Dueling Platform for Competitive Programming. Learn through Games.

CP-Dueling Dueling Platform for Competitive Programming. Learn through Games. Setting Up Minimum Python version needed = 3.9.9 Install Virtualenv and

Bhavesh 3 Feb 07, 2022
Simple darts game using Tkinter and sqlite3. Also associated with Python.

Ever wanted to play a simple and fun game before, and it even keeps a database of your score? Well here it is!! Introducing; Darts! A simple and fun g

an aspirin 2 Dec 19, 2021
Wordle Tas Tool is a terminal application for solving Wordle puzzles

Wordle Tas Tool Terminal application for solving Wordle puzzles Wordle Tas Tool (WTT) is a Python script that iterates over SCOWL95 to solve Wordle pu

1 Feb 08, 2022
It calculates the Nim sum of a nim game.

nim-sum-calculator It calculates the Nim sum of a nim game. The rules of Nim The traditional game of Nim is played with a number of coins arranged in

2 Jan 02, 2022
Hexagon game. Two players: AI and User. Implemented using Alpha-Beta pruning to find optimal solution for agent.

Hexagon game. Two players: AI and User. Implemented using Alpha-Beta pruning to find optimal solution for agent.

Anton 1 Oct 18, 2021
A shooter game.

Screenshots Installation GNU/Linux Go to the Releases tab. Download InfiniteShooter.flatpak from the latest version of InfiniteShooter Open the termin

PastThePixels 13 Dec 01, 2022
Snake Game in Python

Snake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself.

Pavan Ananth Sharma 4 Jul 05, 2022
Jiminy, fast and portable Python/C++ simulator of poly-articulated systems with OpenAI Gym interface for reinforcement learning.

Jiminy is a fast and portable cross-platform open-source simulator for poly-articulated systems. It was built with two ideas in mind: provide a fast y

Alexis DUBURCQ 122 Dec 29, 2022
This is text based adventure game

CHOOSE-YOUR-OWN-ADVENTURE This is text based adventure game CONTRIBUTORS Aditya binukumar Srishti Sharma Shiva Tripathi Tanishq Tanwar ABOUT Theme: Ho

Tanishq Tanwar 3 May 31, 2022
A base chess engine that makes moves on an instance of board.

A base chess engine that makes moves on an instance of board.

0 Feb 11, 2022
Python Knots and Crosses game, with customizable markers and more!

Knot-and-Crosses Python Knots and Crosses game, with customizable markers and more! Features: Ability to change your marker Ability to change how many

4 Nov 07, 2021
This repository contains the Unix Game challenges and metadata

This repository contains the Unix Game challenges and metadata

Nokia 7 Apr 06, 2022
Email guesser - Guessing BF email based on emailGuesser by WhiteHatInspector

email_guesser Guessing BF email based on emailGuesser by WhiteHatInspector (http

4 Dec 25, 2022
Chess turnament organizer (short construct concept)

Turnament Organizer Chess turnament organizer (short construct concept). It is my hobby app I want to write to support lightweight tool for smart roun

kkuba91 3 Dec 16, 2022
KBYD - Simple Bulls and Cows Game

KBYD KBYD - Simple Bulls and Cows Game How to Play KBYD is a simple Bulls and Cows Game. When the game starts, the computer randomly generates 3 to 5

1 Dec 04, 2021
A first-person shooting game developed by using OpenGL

OpenGL-MazeSurvivor-FirstPerson Shooting Game This application named ‘MAZE SURVIVOR’ is a first-person shooting game that finished within a month. It

JunHong 2 Jan 04, 2023