Daily Challenge (217 Part Series)
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]
- 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.
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.
(chooseMove([0,5,0,0]),[1,5]); (chooseMove([0,3,5]),[2,2]); (chooseMove([3,3,6]),[2,6]);
Want to propose a challenge idea for a future post? Email email@example.com with your suggestions!