Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming langauge is JAVA: The game of primeNim works as follows: n > 1 stones are placed on a table between two players.. For each

Programming langauge is JAVA:

The game of primeNim works as follows: n > 1 stones are placed on a table between two players.. For each person's turn there will be n stones left on the table. If n is a prime number then the current player loses and the other player wins. Otherwise the player removes 1, 2 or 3 stones. (Except if n is 4 the player may remove at most two stones.)

Play continues until one of the players gets stuck with a prime number. Then that player loses and the other player wins.

Implement a method

static int primeNim(int n) {} 

that returns a winning play (1, 2 or 3) or -1 if there is no winning play.

For example with n = 6 (six stones left) the current player should remove 1 stone in order to leave 5 (a prime number) for her opponent. Removing 3 stones would also work but we'll assume the method always returns the smallest number that ensures a win.

There is a boolean isPrime(int n) method available for you to call.

You will need one recursive call for each possible number of stones to remove (1, 2 or 3).

When a recursive call returns -1, that means we've found a winning move. Put the appropriate return statement right after each recursive call to be more efficient.

Here is a table of correct return values up to n = 54.

2 -1 3 -1 4 1 5 -1 6 1 7 -1 8 1 9 2 10 3 11 -1 12 1 13 -1 14 1 15 2 16 3 17 -1 18 1 19 -1 20 1 21 2 22 3 23 -1 24 1 25 2 26 3 27 -1 28 1 29 -1 30 1 31 -1 32 1 33 2 34 3 35 -1 36 1 37 -1 38 1 39 2 40 3 41 -1 42 1 43 -1 44 1 45 2 46 3 47 -1 48 1 49 2 50 3 51 -1 52 1 53 -1 54 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

Students also viewed these Databases questions