PrimeGame Java Sourcecode Board Game for 2 Players

• The board has n fields, initially filled with natural numbers from 1 to n.

• Each number on the board represents its score.

• The goal is to get a higher total score than the opponent.

• In turns, the players take (available) numbers off the board - and

receive the score of the number they took.

• However, when the taken number has dividing numbers left on the

board, those divisors are also taken of the board and give their score

to the opponent immediately.

Rules and Constraints• The moving player must always return a number still available on the

board.

• A player must have completed a move in less than 500ms.

• No file write-access is permitted (read-only access is allowed)

• No GUI classes may be used (e.g. Swing, AWT, SWT, etc.)

• 3rd-Party Libraries (e.g. Math libraries, etc.) may be used.Example Game with a Board Size of 20

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20• Player A starts the game and chooses... 19.

• The only number on the board dividing 19 is 1, so player B gets 1.

• Now it's player B to move...

• Player B is greedy and chooses 20...

• This, however, gives player A all remaining divisors of 20 on the

board: 2, 4, 5 and 10.

• Thus, player A actually gets more points from the divisors, than

player B got from his own move. Bad Move by Player B!

• Player A chooses 18.

• Player B gets 3, 6 and 9.

• Player B chooses 17.

• Player A gets nothing

No dividors of 17 on the board (1 is already taken).

• Player A chooses 15.

• Player B gets nothing

(3 and 5 are both already taken).

• Player B chooses 16.

• Player A gets 8.

• Player A chooses 13.

• Player B gets nothing.

• Player B chooses 12.

• Player A gets nothing.

• Player A chooses 11.

• Player B gets nothing.

• Player B chooses 14.

• Player A gets 7.

• No more numbers left on the board. Game over!Player Score Total

Player A 2 + 4 + 5 + 7 + 8 + 10 + 11 + 13 + 15 + 18 + 19 112

Player B 1 + 3 + 6 + 9 + 12 + 14 + 16 + 17 + 20 98! Player A wins the game.

The PrimeGame Framework• Set up a clean board for each match.

• Select a pair of players to compete against each other.

• “Show" the board to the player whose turn it is.

• Let the player choose a number from the board.

• Find the divisors.

• Assign the proper score to both players.

• Keep track of all player's scores and rankings.

• Enforce resource constraints (e.g. time limits).

I got stucked after implementing the following code:

package primegame . player ;

import java . util . Set ;

public interface Player {

abstract public String getPlayerName ();

abstract public Integer makeMove (Set < Integer > availableNumbers );

abstract public void setScore ( int playerScore , int opponentScore );

}

package primegame . player . samples ;

import java . util .*;

import primegame . player . Player ;

public class GreedyPlayer implements Player {

public Integer makeMove ( Set < Integer > board ) {

int highest = Integer . MIN_VALUE ;

for ( Iterator < Integer > it = board . iterator (); i. hasNext (); ) {

int n = it. next ();

if (n > highest )

highest = n;

}

return highest ;

}

public String getPlayerName () { return " Garry Greedy "; }

public void setScore ( int playerScore , int opponentScore ) {}

}

public Integer makeMove ( Set < Integer > board ) {

int highestPrime = Integer . MIN_VALUE ;

for ( Iterator < Integer > it = board . iterator (); i. hasNext (); ) {

int n = it. next ();

if ( n > highestPrime && n >= 3 ) {

boolean isPrime = true ;

for ( int j = 3; j <= Math . ceil ( Math . sqrt ( n ) ); j++ )

if ( n % j == 0 ) { isPrime = false ; break ; }

if ( isPrime ) { highestPrime = n; }

}

}

if ( highestPrime != Integer . MIN_VALUE ) { return highestPrime ; }

else {

Integer [] nrs = availableNumbers . toArray (

new Integer [ availableNumbers . size ()] );

return nrs [new Random (). nextInt ( nrs . length )]. intValue ();

}

}

I got stucked after implementing the following code:

package primegame . player ;

import java . util . Set ;

public interface Player {

abstract public String getPlayerName ();

abstract public Integer makeMove (Set < Integer > availableNumbers );

abstract public void setScore ( int playerScore , int opponentScore );

}

package primegame . player . samples ;

import java . util .*;

import primegame . player . Player ;

public class GreedyPlayer implements Player {

public Integer makeMove ( Set < Integer > board ) {

int highest = Integer . MIN_VALUE ;

for ( Iterator < Integer > it = board . iterator (); i. hasNext (); ) {

int n = it. next ();

if (n > highest )

highest = n;

}

return highest ;

}

public String getPlayerName () { return " Garry Greedy "; }

public void setScore ( int playerScore , int opponentScore ) {}

}

public Integer makeMove ( Set < Integer > board ) {

int highestPrime = Integer . MIN_VALUE ;

for ( Iterator < Integer > it = board . iterator (); i. hasNext (); ) {

int n = it. next ();

if ( n > highestPrime && n >= 3 ) {

boolean isPrime = true ;

for ( int j = 3; j <= Math . ceil ( Math . sqrt ( n ) ); j++ )

if ( n % j == 0 ) { isPrime = false ; break ; }

if ( isPrime ) { highestPrime = n; }

}

}

if ( highestPrime != Integer . MIN_VALUE ) { return highestPrime ; }

else {

Integer [] nrs = availableNumbers . toArray (

new Integer [ availableNumbers . size ()] );

return nrs [new Random (). nextInt ( nrs . length )]. intValue ();

}

}

Ask Your Question

Weekly Poll

Do you think Monopoly should update its pieces?

Discuss in The Lounge

Poll History