# I've put a Nim player on Swish

**URL:** <https://swi-prolog.discourse.group/t/ive-put-a-nim-player-on-swish/8853>\
**Category:** General\
**Tags:** swish\
**Created:** [March 5, 2025, 8:21am UTC](https://swi-prolog.discourse.group/t/ive-put-a-nim-player-on-swish/8853 "2025-03-05T08:21:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeblog](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joeblog/32/122_2.png) [@joeblog](https://swi-prolog.discourse.group/u/joeblog)\
**Post date:** [March 5, 2025, 8:21am UTC](https://swi-prolog.discourse.group/t/ive-put-a-nim-player-on-swish/8853/1 "2025-03-05T08:21:37Z")

</div>

I’ve put some notes and code I’m writing while trying to figure out John Conway etc’s _Winning Ways for your Mathematical Plays_ (I’m not finding it an easy read).  
Work so far is at [SWISH -- SWI-Prolog for SHaring](https://swish.swi-prolog.org/p/Nim.swinb)  
A snag I’ve hit is that according to a theory attributed to Charles Bouton dating back to 1902, the example Nim game I’ve used should always be won by the starting player. My code seems to have some bug because player2 often wins.  
Anyways, I thought I’d share my progress so far for anyone interested.

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [March 5, 2025, 9:13pm UTC](https://swi-prolog.discourse.group/t/ive-put-a-nim-player-on-swish/8853/2 "2025-03-05T21:13:13Z")

</div>

The rules of the game seem similar to [prolog - The game of 23 matches in SWIPROLOG - Stack Overflow](https://stackoverflow.com/questions/75950249/the-game-of-23-matches-in-swiprolog) which hopefully provides inspiration.

---

<div class="post-metadata">

**Author:** ![joeblog](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joeblog/32/122_2.png) [@joeblog](https://swi-prolog.discourse.group/u/joeblog)\
**Post date:** [March 6, 2025, 6:52am UTC](https://swi-prolog.discourse.group/t/ive-put-a-nim-player-on-swish/8853/3 "2025-03-06T06:52:17Z")

</div>

Glad to say both my code and Bouton’s theorem are working fine now.  
I misunderstood that winning moves could have _nimbers_ of either 0 or 2. It turns out they can only be 0. (I think the 2 came up in his paper as an example of an even number of ones that become 0 in his aggregation method… I shouldn’t skim through stuff so quickly).  
Anyways, I’ve put all four example Nim games provided by Stanford University’s General Game Playing course.  
All examples have four heaps labeled a, b, c and d. In the [first example](https://games.ggp.org/base/games/nim1/nim1.kif), my predicate

```prolog
playgame([control(player1),heap(a,1),heap(b,5),heap(c,4),heap(d,2)], Moves).

```

player1 has a move with a _nimber_ of 0, so always wins.

In [Example 2](https://games.ggp.org/base/games/nim2/nim2.kif) player1 has no 0 _nimber_ move, so player2 does and always wins. (I have no idea why that his true, but a long line of famous mathematicians agree, and my code gives that result).

```prolog
playgame([control(player1),heap(a,2),heap(b,2),heap(c,10),heap(d,10)], Moves).

```

I’ve also included Stanford’s other two examples at [SWISH -- SWI-Prolog for SHaring](https://swish.swi-prolog.org/p/Nim.swinb)
