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)

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.