Skip to content
/ NIM Public
  • Notifications You must be signed in to change notification settings
  • Fork 0
  • Star 0

C# command line version of the Nim game. Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps.

License

0 stars 0 forks Branches Tags Activity
Star
Notifications You must be signed in to change notification settings

logiclink/NIM

Branches Tags

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
Properties
Properties
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
App.config
App.config
 
 
CSNIM.csproj
CSNIM.csproj
 
 
CSNIM.sln
CSNIM.sln
 
 
ConsoleExtensions.cs
ConsoleExtensions.cs
 
 
LICENSE.md
LICENSE.md
 
 
NIM.cs
NIM.cs
 
 
Program.cs
Program.cs
 
 
README.md
README.md
 
 

Repository files navigation

Game of NIM

Nim.exe is a C# command line version of the Nim game. Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps.

Nim and its variants have been played since ancient times and its origins probably lie in China. It is known in Europe since the 16th century and got its name from Charles Leonard Bouton, who also developed and published the complete theory of the game in 1901.

Nim is one of the first computer games and played already by a machine, Nimatron, in New York in 1940 1. In 1951, the calculator Nimrod which was built in England defeated the German Economics Minister Ludwig Erhard.

Gameplay and winning strategy

In the Nim game, two players alternately take a number of elements from individual heaps. Elements can be matches in several rows 2 or liquorice sticks in a number of pots 3. The player who takes the last element wins.

The practical strategy to win at the game of Nim is according to Bouton 4 to convert the number of elements in each heap into its binary representation and then calculate the column sum for each digit. A game state in which all column sums are even is a winning position. If one or more sums are odd this is a losing position. The winning position of a player results in a losing position for the other player automatically on the next move. Therefore, the player who achieved the winning position first and retains up to the end of the game wins always. Whether a column sum is odd or even, can be determined via an XOR operation of element counts. E.g.:

Loosing position:           Move:           Winning position:
  1: 00000010 = 2                             1: 00000001 = 1
  2: 00000011 = 3  => TAKE 1 FROM HEAP 1 =>   2: 00000011 = 3
  3: 00000101 = 5                             3: 00000101 = 5
  4: 00000111 = 7                             4: 00000111 = 7
     --------                                    --------
XOR: 00000011 = 3                           XOR: 00000000 = 0

Moreover, you can take two elemets from heap 2 or 4 to get to a winning position. After this move, the following move must lead to a loosing position as you can take elements from one row only.

Nim program

In the case of the C# command line version of Nim game heaps with their element counts are passed as numbers from the command line. Thus, "nim.exe 1 3 5 7" creates a Nim game with four heaps. By default, the user is the first player and starts the game with a move. A move consists of the heap and the number of elements which should be removed, separated by a colon. The heap is identified by its index starting at one. E.g. the move "3:2" means, that the user takes two elements from the third heap. After the user, the computer executes its move and so forth. This process repeats itself until the user or the computer has taken the last elment:

C:\>Nim.exe 2 3 5 7
YOUR MOVE? ([Heap]:[Count]) 1:1
MY MOVE    ([Heap]:[Count]) 4:7
YOUR MOVE? ([Heap]:[Count]) 3:3
MY MOVE    ([Heap]:[Count]) 2:3
YOUR MOVE? ([Heap]:[Count]) 3:1
MY MOVE    ([Heap]:[Count]) 1:1
YOUR MOVE? ([Heap]:[Count]) 3:1
YOU WIN AFTER 00:00:06

Because the playground is not displayed, the user must keep track of all changes in memory. To win, the user has to determine the binary number of all elements and calculating XOR values or column totals of individual binary digits. Therefore, this command line version is an excellent memory training.

Command line switches

To help the user the verbose switch "-v" shows more and more information about the playground of the game, the binary numbers and the results of XOR operations, depending on how many "v"s are specified. With the switch "-c" the output is colored and can be distinguished more easily.

Various game strategies can be specified with the switches "-l" (use the largest number of elements), "-s" (use the smallest number of elements), "-u M" (take "M" elements at maximum) and "-m " (misery game in which the one loses, who takes the last element) With the "-f " switch is specified, that the computer plays the first move.

References

1. Nim game in Wikipedia
2. Online Nim game (german)
3. Denken wie ein Computer Das NIM-Spiel und der Trick mit den binären Zahlen, Peter Schmitz, Heise ct c’t 2017, Heft 17, Seite 132-136
4. Nim game in Wikipedia (german)
Compiled command line client
German version of this text
Home of LogicLink

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%

Footer

© 2024 GitHub, Inc.

玻璃钢生产厂家广东透光玻璃钢雕塑现货山东周年庆典商场美陈厂家直销卡通雕塑厨师玻璃钢雕塑玻璃钢名人胸像雕塑玻璃钢卡通雕塑设计需要多少钱厦门玻璃钢 雕塑峨眉山玻璃钢雕塑浙江玻璃钢雕塑报价聊城景区玻璃钢雕塑厂家夏津玻璃钢花盆花器小鱼玻璃钢雕塑苏州frp玻璃钢雕塑浙江玻璃钢人物雕塑价格金昌户外玻璃钢雕塑制作江西玻璃钢热气球雕塑鹤壁镂空玻璃钢雕塑厂家滁州玻璃钢雕塑制作厂家步行街商场美陈陶瓷玻璃钢花盆山西步行街玻璃钢雕塑生产厂家福建大型商场创意商业美陈道具上海定制玻璃钢雕塑多少钱呼和浩特校园玻璃钢雕塑生产厂家商场室外美陈四川创意成都商场美陈玻璃钢 雕塑怎么上色丽江玻璃钢卡通雕塑定制南宁玻璃钢雕塑哪家好山西玻璃钢马雕塑艺术小品玻璃钢照片雕塑2022年香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化