Games


Hello Readers,

If you read the past series on Hangman you will pleased to know I’m releasing a version of the code that includes a word list that is based off of GRE/SAT vocabulary list. It also implements AJAX to allow for real-time drawing of PNGs with GD.

This is really a easy to install and use program. Just drag and drop

Download Now

Our Sponsors

Football has always been an American pastime and also a programming one as well. Ever year EA Games as well as many others are busy building the next years hottest simulator for our entertainment sports.

When I played pro football, I never set out to hurt anyone deliberately - unless it was, you know, important, like a league game or something.
-Dick Butkus

Today I’m going to teach you how to build something similar to NES Tecmo Super Bowl, without the play sequence. The play sequence is too fast to simulate on web and will be left as an exercise with flash or a related software.

The steps I’ve chosen to do to accomplish this are as follows
Determine a basic win/loss formula for play selection
Develop In Depth Data for more realistic feel
Bring it all together

So for the first step it’s quite easy. I want to think of something that after the play is chosen in football, how to determine the outcome. A simple formula might involve the following variables

Offensive play choice
Defensive play choice
Offensive team power
Defensive team power
Random Variation

Based around these simple variables I should be able to determine what is the outcome of a given play. Here is an example in php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
$off_play_choice = $_GET['oselect'];
$def_play_choice = $_GET['oselect_def'];
 
$off_power = determine_off_strength(); // a summation of individual's strength
$def_power = determine_def_strength();
 
if($playtype = 'run'){
$min_yards_run = .3;
$max_yards_run = 3.6;
$avg_run = 2;
 
$minstd = calc_std($min_yards_run,$max_yards_run,$avg_run);
$maxstd =  calc_std($min_yards_run,$max_yards_run,$avg_run);
}else{
$min_yards_pass = .3;
$max_yards_pass = 3.6;
$avg_pass = 2;
 
$minstd = calc_std($min_yards_run,$max_yards_run,$avg_run);
$maxstd =  calc_std($min_yards_run,$max_yards_run,$avg_run);
}
 
// this forumla takes all variables and returns a yardage which would be reported to the user(s)
 
$yards_balance = $off_playchoice - $def_playchoice * ($off_power - $def_power) * rand($minstd,$maxstd);
?>

$yards_balance in this example above is the goal, once we have this we can then build placement of the marker and basically have a game. In the next post we will will also build the user interface and show what this thing really can do.

In the previous Hangman article we wrote with PHP and GD a graphics port that with this code will complete the hangman game. This bit of code handles the user interaction as well as initialization of the game.

This code can be added on to the other code to create a complete game.

Without further ado here is the entire hangman game user interface written in php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$misses=0;
if($_REQUEST['addmiss'] == 1){
	$misses = 1;	
}
if($_REQUEST['draw']==1){
	session_start();
	$misses = $_SESSION['misses'] + $misses;
	$_SESSION['misses'] = $misses;
	$im = drawTxt($im,$word,$_SESSION['choices']);
	$im = drawHangMan($im,$misses);
	header ("Content-type: image/png");
	ImagePng ($im);
}
if($_REQUEST['new_word']){
	require_once('/home/rionmass/hawkenterprises-wordlist-config.php');
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if (!$link) {
	    die('Not connected : ' . mysql_error());
	}
	$db_selected = mysql_select_db(DB_NAME, $link);
	if (!$db_selected) {
	    die ('Can\'t use foo : ' . mysql_error());
	}
	$sql = 'SELECT word from TABLENAME LIMIT 1';
	$rs = mysql_query($sql);
	$word = mysql_result($rs,0,0);
	session_start();
	$_SESSION['word'] = $word;
}
session_start();
if(!isset($_SESSION['word'])){
	header('location:?new_word=1');	
}else{
	$_SESSION['choices'][] = $_POST['choice'];
	if(isset($_POST['choice'])){
		$_SESSION['choices'][] = $_POST['choice'];
		if(false !== strpos($_POST['choice'],$_SESSION['word'])){
			header('location:?draw=1');
 
		}else{
			header('location:?draw=1&addmiss=1');
		}
 
	}
}
?><form action="" method="post"><input type="text" name="choice"><input type="submit" name="submit" value="go"></form>

I hope you enjoyed this little series I know we did.

His achievements read like the graffiti on the walls of a hangman’s changing room.
Johnathan Larsen

Next Page »