Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Secrets of the Wireless Elite: Mobile Applications Need Scripting Too!

By John Papageorge
2005-04-13


Theory in Practice: Park's Example Code

To avoid computation, Park advises that you preprocess data and use it later. "It's nice when you can preprocess data at load time or during run time," he says.

He continues, "I wrote an American football game in which the AI could use hundreds of random numbers per second. In this case I could avoid a fair amount of computation by pulling from a pool of random numbers during each football play. Resource pools represent a simple and common concept employable in many other contexts."

Here's the code:

Listing 1. Class RandomPool

import java.util.Random;


public final class RandomPool
{
private static final boolean DEBUG = false;

private static java.util.Random _randomGenerator = new java.util.Random();

private static final int POOLSIZE = 30;
private static int _poolsize = POOLSIZE;
private static int[] _randomInts = new int[POOLSIZE];

private static int _idxCurrent = 0;
private static int _wrapCount = 1; //init=1 to generate 1st entire pool

//------------------------------------------------------------------------
public static final boolean resize(int poolsize)
{
int[] pool = new int[poolsize];
if ((pool != null))
{
_randomInts = pool;
_poolsize = poolsize;
return true;
}
return false;
}
}
"Between plays, the pool refreshes with new numbers by calling RandomPool.generate()":

Listing 2. Method RandomPool.generate()

public static final void generate()

{
if (RandomPool.DEBUG)
{
int count = _idxCurrent + (_wrapCount*_poolsize);
System.out.print( "RandomPool: used "+ count +" numbers.\n");
}

int idx = _idxCurrent;
_idxCurrent = 0; //init number ptr

// Replace whole pool if all numbers were used
if (_wrapCount > 0)
{
_wrapCount = 0;
idx = _poolsize-1;
}

// Replace just the used numbers
while (idx-- > 0)
{
_randomInts[idx] = Math.abs(_randomGenerator.nextInt());
}
}
"During plays, the game grabs numbers from the random number pool," he says. "Instead of refilling the pool when it runs out, the game reuses the numbers in the pool until the play completes, then refreshes the pool before the next play":

Listing 3. Refreshing the pool

 public static final int nextInt(int limit)

{
// If we ran out of numbers, start reusing them from the beginning
if (_idxCurrent >= _poolsize)
{
_idxCurrent = 0;
_wrapCount++;
}

// Return number from 0 to limit
if (limit > 0)
return (_randomInts[_idxCurrent++] % (limit+1));
else
return _randomInts[_idxCurrent++];
}
}


Tutorial Pages:
» Mobile Apps Developer Tom Park Imparts His Wisdom
» The Mobile Gamer's Toolkit
» Park's Secret Weapon: Scripting
» What Every Wireless Developer Should Know
» What Every Wireless Developer Should Avoid
» The Challenges Every Wireless Developer Faces
» Time to Market Equals Success
» Brand Games Extended to Mobile Devices
» BREW vs. Java Technology
» Theory in Practice: Park's Example Code
» Mobile Gaming: the Future
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Secrets of the Wireless Elite: Alexei Polyakov
» Linux Wireless Networking
» A New Strategy of Language Pack Management for Wireless Apps
» Open Source Wireless Tools Emerge
» Getting Practical About Wireless Security, Part 1: Building a Wireless Sniffer with Perl
» Challenges and Opportunities in Mobile Games

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources