I'm asking for a general formula/algorithm that can be used in such a game.
There are $n$ piles, with varying number of pebbles. The number of pebbles in each pile is given by an array from $pile[1]$ to $pile[n]$. There is another array called the $validmove[\ ]$ array. It has any number of numbers. For example, we can define $validmove[\ ]=\{1,2,5,7\}$ This array is constant throughout the game. If $0$ is present in $validmove$, it means that an entire pile can be removed, irrespective of the number of pebbles it contains.
2 players play turnwise. On each turn, a player selects a single pile. He may remove any number of pebbles, as long as that number is present in the $validmove$ array. In the current example, suppose we have selected a pile with $13$ stones. The player can now remove $1$, $2$, $5$ or $7$ pebbles from that pile, leaving it with $12$, $11$, $8$ or $6$ pebbles respectively.
The person who removes the last stone wins (or I can specify 'loses' as well, that may require a slightly different algorithm).
If both players play perfectly, who wins?
I couldn't find an algorithm on the net, even though there is a huge Wikipedia page on it, so I thought SE could maintain one for future reference.