Add utils like juggle() to be called from GameConfig; always keep max

number of LocalPlayer instances around so they can be filled from jni
code (which is easier than making the jni construct new instances.)
This commit is contained in:
ehouse 2010-01-20 06:38:29 +00:00
parent 531ae8ea1a
commit 7eccde84e3

View file

@ -1,10 +1,14 @@
/* -*- compile-command: "cd ../../../../../../; ant reinstall"; -*- */
package org.eehouse.android.xw4.jni;
import java.util.Random;
import org.eehouse.android.xw4.Utils;
public class CurGameInfo {
private static final String BUILTIN_DICT = "OWL2_2to9.xwd";
public static final int MAX_NUM_PLAYERS = 4;
public enum XWPhoniesChoice { PHONIES_IGNORE, PHONIES_WARN, PHONIES_DISALLOW };
public enum DeviceRole { SERVER_STANDALONE, SERVER_ISSERVER, SERVER_ISCLIENT };
@ -28,27 +32,98 @@ public class CurGameInfo {
public CurGameInfo() {
nPlayers = 2;
boardSize = 15;
players = new LocalPlayer[nPlayers];
players = new LocalPlayer[MAX_NUM_PLAYERS];
serverRole = DeviceRole.SERVER_STANDALONE;
dictName = BUILTIN_DICT;
hintsNotAllowed = false;
players[0] = new LocalPlayer( "Player 1");
players[1] = new LocalPlayer( "Player 2", true );
// Always create MAX_NUM_PLAYERS so jni code doesn't ever have
// to cons up a LocalPlayer instance.
int ii;
for ( ii = 0; ii < MAX_NUM_PLAYERS; ++ii ) {
players[ii] = new LocalPlayer(ii);
}
}
public CurGameInfo( CurGameInfo src ) {
nPlayers = src.nPlayers;
boardSize = src.boardSize;
players = new LocalPlayer[nPlayers];
players = new LocalPlayer[MAX_NUM_PLAYERS];
serverRole = src.serverRole;
dictName = src.dictName;
hintsNotAllowed = src.hintsNotAllowed;
int ii;
for ( ii = 0; ii < nPlayers; ++ii ) {
for ( ii = 0; ii < MAX_NUM_PLAYERS; ++ii ) {
players[ii] = new LocalPlayer( src.players[ii] );
}
}
public boolean addPlayer()
{
boolean canAdd = nPlayers < MAX_NUM_PLAYERS;
if ( canAdd ) {
// LocalPlayer newPlayer = new LocalPlayer( nPlayers );
// players[nPlayers++] = newPlayer;
++nPlayers;
}
return canAdd;
}
public boolean moveUp( int which )
{
boolean canMove = which > 0 && which < nPlayers;
if ( canMove ) {
LocalPlayer tmp = players[which-1];
players[which-1] = players[which];
players[which] = tmp;
}
return canMove;
}
public boolean moveDown( int which )
{
return moveUp( which + 1 );
}
public boolean delete( int which )
{
boolean canDelete = nPlayers > 1;
if ( canDelete ) {
int ii;
for ( ii = which; ii < nPlayers - 1; ++ii ) {
moveDown( ii );
}
--nPlayers;
players[nPlayers] = new LocalPlayer(nPlayers);
}
return canDelete;
}
public boolean juggle()
{
boolean canJuggle = nPlayers > 1;
if ( canJuggle ) {
// for each element, exchange with randomly chocsen from
// range <= to self.
Random rgen = new Random();
Utils.logf( "nPlayers: " + nPlayers );
Utils.logf( "players.length: " + players.length );
for ( int ii = nPlayers - 1; ii > 0; --ii ) {
// Contrary to docs, nextInt() comes back negative!
int rand = Math.abs(rgen.nextInt());
int indx = rand % (ii+1);
if ( indx != ii ) {
LocalPlayer tmp = players[ii];
players[ii] = players[indx];
players[indx] = tmp;
}
}
}
return canJuggle;
}
}