php/Math   
Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 PHP/ir
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Hunch.net
 Matthew Hurst
 JMLR
 JSS
 Hal Daume III
 Math Notes >> Fun & Games

Game Artificial Intelligence [Fun & Games
Posted on May 4, 2008 @ 02:06:14 AM by Paul Meagher

The book Game Artificial Intelligence (2007), by John Ahlquist and Jeannie Novak, is an interesting read. It is not a difficult read and only includes small illustrative code snippets, nevertheless, I learned quite a bit from it. The book is very well designed and attractive with excellent illustrations and screen shots from a variety of games. It includes numerous short interviews and blurbs by senior game designers and developers from major studios that works well with the text. I'm not really a "gamer" myself and one of the reasons I found the book interesting was because it offered a high-level, knowledgeable, and insightful discussion of program-level game design using as examples some of the games my son plays plus many others.

The code snippet below is a PHP version of a thought-provoking snippet from the book showing how the "retreat" and "attack" behavior of a game AI might be controlled:

function OnSeePlayer($agent, $player) {
  if (Health($agent) < DefaultHealth($agent)*0.1) 
    Retreat($agent);
  else
    Attack($agent, $player);
}

So, if the agent sees the player, and the agent's health is less that 10% of it's normal health, the agent will retreat; otherwise the agent will attack. It is interesting to consider how one might go about building up the framework around this code snippet so that it would play a useful role in determining a Game AI's behavior. I suspect that much of the work on the Game AI component of game design could be done without any flashy graphics; mostly what you would need are simulations of your opponents tactics and strategies to see how your game AI reacts under different situations (that would eventually map onto the flashy game graphics depicting the actions and reactions).

I've not read the last two chapters on pathfinding yet so can't give a full review of this book. Nevertheless, if you are a geek looking for a good beach book this summer, I'd recommend taking this book along. If you can't play your videogames in the sand, then at least you can read an interesting discussion about the nature of the brains inside your games.

Permalink 

Counting objects within search radius 1 on a tiled map [Fun & Games
Posted on January 31, 2007 @ 10:52:38 PM by Paul Meagher

Here is some code that counts the number of trolls and bunnys directly adjacent to the x-y location on a map. The map is a simple tiled environment where each location is defined by a combination of non-negative consecutive integers.

<?php
/**
* @file count_around.php
* @function count_around
* @author Paul Meagher
* @modified Jan 31, 2007
*
* Inspired by a peek at the source code for Conway's game of life. 
*
* @see http://4umi.com/web/javascript/conway.htm
*/

define("TROLL""1");
define("BUNNY""0");

/**
* Counts number of objects directly adjacent (i.e., search radius of 1) 
* to the x-y coordinates on the supplied map.
*
* @param array  $map    a representation of the terrain
* @param int    $x      x coordinate location on the map
* @param int    $y      y coordinate location on the map
* @param string $obj    object we are looking for on the map
* @return int $n number of objects around the x-y coordinate
*/
function count_around($map$x$y$obj) {
  
$n=0;
  if(
$map[$x-1][$y-1]==$obj$n++;
  if(
$map[$x-1][$y  ]==$obj$n++;
  if(
$map[$x-1][$y+1]==$obj$n++;
  if(
$map[$x  ][$y-1]==$obj$n++;
  if(
$map[$x  ][$y+1]==$obj$n++;
  if(
$map[$x+1][$y-1]==$obj$n++;
  if(
$map[$x+1][$y  ]==$obj$n++;
  if(
$map[$x+1][$y+1]==$obj$n++;
  return 
$n;
}

$map[0] = array(BUNNY,TROLL,TROLL,TROLL);
$map[1] = array(TROLL,BUNNY,TROLL,TROLL);
$map[2] = array(TROLL,TROLL,BUNNY,TROLL);
$map[3] = array(BUNNY,TROLL,TROLL,TROLL);

$x 1;
$y 1;

$num_trolls count_around($map$x$yTROLL);
$num_bunnys count_around($map$x$yBUNNY);

echo 
"The number of trolls within search radius 1 is $num_trolls <br />";
echo 
"The number of bunnys within search radius 1 is $num_bunnys <br />";
?>

I encourage you to visit the "Game of Life" link that inspired this code.

Exercises

1. Generalize the code so that it accepts an integer $radius parameter that controls the how far from the x-y coordinates you will count objects.

2. Generalize the code so that it accepts a boolean $wrap_around parameter that controls how counting occurs when you hit a map border. If, for example, the supplied x-y coordinate was the top left-hand corner (i.e., $x=0,$y=0) and $wrap_around=1 then you would also count the number of bunnies and trolls occurring in the right top margin and bottom left margin (because you would hit these objects in a wrap around environment if you moved left or up respectively).

Permalink 

 Archive 
 


php/Math Project
© 2011. All rights reserved.