home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD Direkt: Spezial 1
/
CDD_SPIELE_.ISO
/
wingames
/
life
/
life.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-15
|
35KB
|
963 lines
/****************************************************************************
*
* FILE: LIFE.C
*
* DESCRIPTION: Windows 3.0 Version of the classic simulation of "LIFE".
* This program incorporates a minor twist in the basic
* algorithm by utilizing color to represent various
* life "phases". The final phase of any life cycle is
* always death!
*
* This software is hereby placed in the public domain.
* Use and abuse it at your own risk!
*
*
* AUTHOR: Tom Wheeler
* 31294 Morlock
* Livonia, Mich. 48152
* [72037,1742]
*
*****************************************************************************
*
* DATE VER DESCRIPTION
* ---------- --- ---------------------------------------------------
* 05/20/91 1.0 Initial Development 1.0
* 05/22/91 1.1 Added Color Support
*
****************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "life.h"
#define CLASSNAME "LifeClass"
#define APPNAME "Life"
#define DEAD 0 /* state: cell is dead */
#define ALIVE 0x1 /* state: cell is alive */
#define CHILD 0x2 /* state: cell is a child */
#define ADOLESCENT 0x4 /* state: cell is an adolescent */
#define ADULT 0x8 /* state: cell is an adult */
#define MIDAGED 0x10 /* state: cell is middle aged */
#define RETIRED 0x20 /* state: cell is retirement age */
#define SENIOR 0x40 /* state: cell is a senior citizen */
#define METHUSULA 0x80 /* state: cell is to be terminated */
#define AGECYCLE 10 /* # of cycles till next age */
#define MAX_XCELL 100 /* max number of horizontal cells */
#define MAX_YCELL 100 /* max number of vertical cells */
#define DEFAULT_WIDTH 15 /* default number of cells to display */
#define DEFAULT_HEIGHT 15 /* default number of cells to display */
#define CYCLE_TIMER 1
/* Macro to get a random integer within a specified range */
#define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
typedef unsigned char UCHAR;
/* MATRIX defines the cell matrix used to represent the grid of entities.
* Life states are represented as the low byte of the matrix entry, the high
* byte is used to is used to keep a count representing the age of the
* individual entities.
*/
typedef int MATRIX[MAX_XCELL+2][MAX_YCELL+2];
typedef struct _Life {
int CellX; /* individual cell width */
int CellY; /* individual cell height */
int TopCellX; /* horiz. id of first cell displayed */
int TopCellY; /* vert. id of frist cell displayed */
int CellsPerPageX; /* num of visible X cells on screen */
int CellsPerPageY; /* num of visible Y cells on screen */
BOOL Grid; /* grid on/off state */
BOOL Run; /* run state of matrix */
MATRIX Matrix; /* state matrix */
}LIFE;
typedef LIFE *PLIFE;
MATRIX Scratch; /* temporary cell matrix */
HWND hInst = NULL; /* instance variable */
HMENU hMenuFrame = NULL; /* instance menu handle */
HWND hWndFrame = NULL; /* instance window handle */
HANDLE hAccelFrame = NULL; /* instance keyboard accelerator */
HANDLE hMatrix = NULL; /* handle to cell matrix structure */
HPEN hBluePen = NULL; /* Blue drawing pen */
HPEN hGreenPen = NULL; /* Green drawing pen */
HPEN hCyanPen = NULL; /* Cyan drawing pen */
HPEN hRedPen = NULL; /* Red drawing pen */
HPEN hMagentaPen = NULL; /* Magenta drawing pen */
HPEN hBrownPen = NULL; /* Brown drawing pen */
HPEN hGrayPen = NULL; /* Gray drawing pen */
HPEN hLightRedPen = NULL; /* Light Red drawing pen */
HPEN hWhitePen = NULL; /* White drawing pen */
HBRUSH hBlueBrush = NULL; /* Solid Blue brush */
HBRUSH hGreenBrush = NULL; /* Solid Green brush */
HBRUSH hCyanBrush = NULL; /* Solid Cyan brush */
HBRUSH hRedBrush = NULL; /* Solid Red brush */
HBRUSH hMagentaBrush = NULL; /* Solid Magenta brush */
HBRUSH hBrownBrush = NULL; /* Solid Brown brush */
HBRUSH hGrayBrush = NULL; /* Solid Gray brush */
HBRUSH hLightRedBrush = NULL; /* Solid Light Red brush */
HBRUSH hWhiteBrush = NULL; /* Solid White brush */
PLIFE pLife = NULL; /* pointer to cell life structure */
int iSmall; /* size in pixels of a small cell */
int iNorm; /* size in pixels of a normal cell */
int iLarge; /* size in pixels of a large cell */
int iCycleTime = 250; /* default timer (milliseconds) */
WORD wCycleTimer = 0; /* timer used for life cycling */
/****************************************************************************
* void DrawGridLines(HDC hDC,RECT *rect)
*
* Description: DrawsGridLines on the Life Display Window
*
* Input: hDC - Device Context to draw on
* rect - pointer to client rectangle
*
* Output: N/A
****************************************************************************/
void DrawGridLines(HDC hDC,RECT *rect)
{
int iX,iY;
SelectObject(hDC,hGrayPen);
for(iX = pLife->CellX; iX < rect->right; iX += pLife->CellX) {
MoveTo(hDC,iX,0);
LineTo(hDC,iX,rect->bottom);
}
for(iY = pLife->CellY; iY < rect->bottom; iY += pLife->CellY) {
MoveTo(hDC,0,iY);
LineTo(hDC,rect->right,iY);
}
}
/****************************************************************************
* void DrawCell(HDC hMouseMoveDC,int iXPos,int iYPos,UCHAR ucState)
*
* Description: Draws the cell at the indicated grid position in the color
* appropriate to its current state.
*
* Input: hDC - Device Context to draw on
* iXPos
* iYPos - Screen Matrix position to draw at
* ucState - State to draw
*
* Output: N/A
****************************************************************************/
void DrawCell(HDC hDC,int iXPos,int iYPos,UCHAR ucState)
{
int iX,iY;
HPEN hPen;
HBRUSH hBrush;
iX = iXPos * pLife->CellX;
iY = iYPos * pLife->CellY;
if(ucState & ALIVE) {
if(ucState & SENIOR) {
hPen = hBluePen;
hBrush = hBlueBrush;
}
else if(ucState & RETIRED) {
hPen = hBrownPen;
hBrush = hBrownBrush;
}
else if(ucState & MIDAGED) {
hPen = hMagentaPen;
hBrush = hMagentaBrush;
}
else if(ucState & ADULT) {
hPen = hRedPen;
hBrush = hRedBrush;
}
else if(ucState & ADOLESCENT) {
hPen = hCyanPen;
hBrush = hCyanBrush;
}
else if(ucState & CHILD) {
hPen = hGreenPen;
hBr