DEV Community

dev.to staff
dev.to staff

Posted on

5 1

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!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay