Question
In Java: Remember the Nim game: two players take turn to take any number of stones from any one pile among several piles. The winner
In Java:
Remember the Nim game: two players take turn to take any number of stones from any one pile among several piles. The winner is the one who takes the last piece. For example: if the game starts with 3 pile:
original: 7, 8, 9 after player 1: 0, 8, 9 after player 2: 0, 8, 8 after player 1: 0, 0, 8 after player 2: 0, 0, 0 player 2 won
The key to the winning strategy: treat each number of a pile as a binary, pay attention to the sum of one's in each digit, move so that all the sums are even number. Another way to look at the example above:
original: 7=0111 8=1000 9=1001 after pl: 0=0000 8=1000 9=1001 (a random move) after p2: 0=0000 8=1000 9=1000 (a winning move: for the sums are 2, 0, 0, 0)
(BTW: a winning at the beginning should produce: 1=0001 8=1000 9=1001 (sums: 2,0,0,2)
Assignment: (class Nim): assume each pile has up to 15 stones (up to 4 digits binary) and the piles are stored in an array, such as {7, 8, 9}. The code should produce an array of 4 int to keep the sums of digits, which in the beginning of the example should be {2, 1, 1, 2}.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started