DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #147 - NIM

Setup

Nim is a mathematical strategy game in which two players take turns removing objects from piles of straw. On each turn, a player must remove at least one straw, and may remove any number straw provided they all come from the same heap/pile. The goal of the game is to take the last piece of straw.

Pile 0: ||||

Pile 1: ||

Pile 2: |||||

Pile 3: |

Pile 4: ||||||

...or more concisely: [4,2,5,1,6]

Rules

  • Each player takes turns picking a pile and removing as much straw as they want from the pile they pick.
  • Players must take at least one straw.
  • Whoever picks the last straw wins.

Task

Write a function to create an AI that plays NIM.

You have to encode an AI in a function that takes a board represented as a list of positive integers and returns: [pile_index, number_of_straws], which refers to an index of a pile on the board and some none-zero number amount of straw to draw from that pile.

Examples

(chooseMove([0,5,0,0]),[1,5]);

(chooseMove([0,3,5]),[2,2]);

(chooseMove([3,3,6]),[2,6]);

Good luck!~


This challenge comes from xcthulhu on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (0)