Category : C Source Code
Archive   : CONCEN.ZIP
Filename : CONCEN.C

 
Output of file : CONCEN.C contained in archive : CONCEN.ZIP
/*
CONCEN.C
The game Concentration for MS-Windows
*/


#include "windows.h"
#define extern
#include "concen.h"
#undef extern


/* UTIL.C */
extern FAR PASCAL MakeStr (LPSTR szStr, LPSTR szLabel, int n);

/* DRAWTILE.C */
extern FAR PASCAL DrawTile (HDC hDC, LPRECT lpClientRect, int TileType);

extern int FAR PASCAL lstrcmp (LPSTR, LPSTR);
extern LPSTR FAR PASCAL lstrcpy (LPSTR, LPSTR);
extern LPSTR FAR PASCAL lstrcat (LPSTR, LPSTR);
extern int FAR PASCAL lstrlen (LPSTR);

/* ABOUT.C */
extern BOOL FAR PASCAL AboutBoxProc (HWND hDlg, unsigned message, WORD wParam, LONG lParam);

/* Forward References */
long FAR PASCAL BoardWinProc (HWND, unsigned, WORD, LONG);
long FAR PASCAL TileWinProc (HWND, unsigned, WORD, LONG);
long FAR PASCAL MatchesWinProc (HWND, unsigned, WORD, LONG);
long FAR PASCAL AttemptsWinProc (HWND, unsigned, WORD, LONG);

int FAR PASCAL GetTileType (HWND hTileWnd);
FAR PASCAL IncrementAttempts();
FAR PASCAL IncrementMatches();
FAR PASCAL SetupBoard();
FAR PASCAL SetupGame();
FAR PASCAL CreateGameWindows();

/********************
WinMain
********************/
int PASCAL WinMain(HANDLE hInstance,
HANDLE hPrevInstance,
LPSTR lpszCmdLine,
int cmdShow)
{
MSG msg;
HMENU hSysMenu;

if (!hPrevInstance)
{
WNDCLASS rClass;

rClass.lpszClassName = "CONCENCLASS";
rClass.hInstance = hInstance;
rClass.lpfnWndProc = BoardWinProc;
rClass.hCursor = LoadCursor(NULL, IDC_ARROW);
rClass.hIcon = LoadIcon(hInstance,"concen");
rClass.lpszMenuName = "ConcenMenu";
rClass.hbrBackground = GetStockObject(GRAY_BRUSH);
rClass.style = CS_HREDRAW|CS_VREDRAW;
rClass.cbClsExtra = 0;
rClass.cbWndExtra = 0;
RegisterClass((LPWNDCLASS)&rClass);
}

hInst = hInstance;

hBoardWnd = CreateWindow("CONCENCLASS",
"Concentration",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
CW_USEDEFAULT, 0, /* x,y top, left corner */
CW_USEDEFAULT, 0, /* cx, cy width, height */
(HWND)NULL, /* parent */
(HMENU)NULL, /* menu / child id */
(HANDLE)hInstance,
(LPSTR)NULL
);


/* Add About... selection to system menu */
hSysMenu = GetSystemMenu (hBoardWnd, FALSE);
ChangeMenu (hSysMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
ChangeMenu (hSysMenu, 0, "&About...", SYSMENU_ABOUT, MF_APPEND | MF_STRING);

ShowWindow(hBoardWnd, cmdShow);
UpdateWindow(hBoardWnd);

CreateGameWindows();
SetupGame();
SetupBoard();

while (GetMessage((LPMSG)&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

exit(msg.wParam);
}


/********************
BoardWinProc
********************/
long FAR PASCAL BoardWinProc(HWND hWnd,
unsigned uMsgType,
WORD wParam,
LONG lParam)
{
FARPROC lpprocAbout;

switch (uMsgType)
{
case WM_SYSCOMMAND:
switch (wParam)
{
case SYSMENU_ABOUT:
lpprocAbout = MakeProcInstance ((FARPROC)AboutBoxProc, hInst);
DialogBox (hInst, "ABOUTBOX", hWnd, lpprocAbout);
FreeProcInstance (lpprocAbout);
break;
default:
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
}
break;

case WM_COMMAND:
if (wParam == CONMENU_NEWGAME)
{
SetupGame();
SetupBoard();
}
else
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
break;

default:
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
break;
}

return(0L);
}

/********************
TileWinProc
********************/
long FAR PASCAL TileWinProc(HWND hWnd,
unsigned uMsgType,
WORD wParam,
LONG lParam)
{
HDC hDC;
PAINTSTRUCT ps;
RECT Rect;

switch (uMsgType)
{
case WM_PAINT:
hDC = BeginPaint (hWnd, &ps);
if (hWnd==hTile1Wnd || hWnd==hTile2Wnd)
{ /* Show Tile Face */
GetClientRect (hWnd, &Rect);
FillRect (hDC, &Rect, GetStockObject(WHITE_BRUSH));
DrawTile (hDC, &Rect, GetTileType(hWnd));
}
else
{ /* Show tile back */
GetClientRect (hWnd, &Rect);
FillRect (hDC, &Rect, hTileBackBrush);
}
EndPaint (hWnd, &ps);
break;

case WM_LBUTTONUP:
if (bFirstTile)
{
bFirstTile = FALSE;
hTile1Wnd = hWnd;
hTile2Wnd = NULL;
InvalidateRect (hWnd, NULL, TRUE);
}
else /* This is the second tile chosen */
{
bFirstTile = TRUE;
hTile2Wnd = hWnd;
IncrementAttempts();
if (hTile1Wnd == hTile2Wnd)
{ /* user chose same tile - turn it back over */
hTile1Wnd = NULL;
hTile2Wnd = NULL;
InvalidateRect (hWnd, NULL, TRUE);
}
else
{
hTile2Wnd = hWnd;
InvalidateRect (hWnd, NULL, TRUE);
if (GetTileType(hTile1Wnd) == GetTileType(hTile2Wnd))
IncrementMatches();
else
/* wait for a while before turning tiles back over */
SetTimer (hWnd, 1, 1000, NULL);
}
}
break;

case WM_TIMER:
KillTimer (hWnd, 1);
InvalidateRect (hTile1Wnd, NULL, TRUE);
InvalidateRect (hTile2Wnd, NULL, TRUE);
hTile1Wnd = NULL;
hTile2Wnd = NULL;
bFirstTile = TRUE;
break;

default:
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
break;
}

return(0L);
}

/********************
AttemptsWinProc
********************/
long FAR PASCAL AttemptsWinProc(HWND hWnd,
unsigned uMsgType,
WORD wParam,
LONG lParam)
{
PAINTSTRUCT ps;
HDC hDC;
char szAttempts[20];

switch (uMsgType)
{
case WM_PAINT:
hDC = BeginPaint (hWnd, &ps);
MakeStr (szAttempts, "Attempts ", nAttempts);
TextOut (hDC, 0,0, szAttempts, lstrlen(szAttempts));
EndPaint (hWnd, &ps);
break;

default:
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
break;
}

return(0L);
}

/********************
MatchesWinProc
********************/
long FAR PASCAL MatchesWinProc(HWND hWnd,
unsigned uMsgType,
WORD wParam,
LONG lParam)
{
PAINTSTRUCT ps;
HDC hDC;
char szMatches[20];

switch (uMsgType)
{
case WM_PAINT:
hDC = BeginPaint (hWnd, &ps);
MakeStr (szMatches, "Matches ", nMatches);
TextOut (hDC, 0,0, szMatches, lstrlen(szMatches));
EndPaint (hWnd, &ps);
break;

default:
return(DefWindowProc(hWnd, uMsgType, wParam, lParam));
break;
}

return(0L);
}

/********************
GetTileType
********************/
int FAR PASCAL GetTileType (HWND hTileWnd)
{
int i, TileType;
for (i=0, TileType=TILETYPE_NULL; i if (rghTileWnd[i] == hTileWnd)
TileType = rgTileType[i];
return (TileType);
}

/********************

IncrementAttempts
********************/
FAR PASCAL IncrementAttempts()
{
nAttempts++;
InvalidateRect (hAttemptsWnd, NULL, TRUE);
}

/********************
IncrementMatches
********************/
FAR PASCAL IncrementMatches()
{
nMatches++;
InvalidateRect (hMatchesWnd, NULL, TRUE);
/* Take the tiles off the board */
MoveWindow (hTile1Wnd, 0,0, 0,0, TRUE);
MoveWindow (hTile2Wnd, 0,0, 0,0, TRUE);
}

/********************
SetupBoard
********************/
FAR PASCAL SetupBoard()
{
RECT Rect, BoardRect;
HDC hDC;
DWORD lExtent;
int x,y, row,col, i;
int nBoardWidth, nBoardHeight;
int nWidth, nHeight;


/* Place Tile, Attempts, and Matches windows on the board */

GetClientRect (hBoardWnd, &Rect);

hDC = GetDC (hAttemptsWnd);
lExtent = GetTextExtent (hDC, "Attempts 00000", 14);
ReleaseDC (hAttemptsWnd, hDC);
nWidth = LOWORD(lExtent) * 5/4;
nHeight = HIWORD(lExtent) * 3/2;
MoveWindow (hAttemptsWnd, Rect.right-nWidth, Rect.top, nWidth, nHeight, TRUE);
MoveWindow (hMatchesWnd, Rect.right-nWidth, Rect.top+nHeight, nWidth, nHeight, TRUE);
InvalidateRect (hAttemptsWnd, NULL, TRUE);
InvalidateRect (hMatchesWnd, NULL, TRUE);

SetRect (&BoardRect, Rect.left,Rect.top, Rect.right-nWidth,Rect.bottom);
nBoardWidth = BoardRect.right - BoardRect.left -10;
nBoardHeight = BoardRect.bottom - BoardRect.top -10;
nWidth = nBoardWidth / nBoardCols;
nHeight = nBoardHeight / nBoardRows;
nTileVertSpacing = nWidth/5;
nTileHorizSpacing = nHeight/6;
nTileWidth = nWidth - nTileVertSpacing;
nTileHeight = nHeight - nTileHorizSpacing;
x = BoardRect.left +5;
y = BoardRect.top +5;
row = 1;
col = 1;
for (i=0; i {
MoveWindow (rghTileWnd[i], x, y, nTileWidth, nTileHeight, TRUE);
x += nWidth;
col++;
if (col > nBoardCols)
{
col = 1;
x = BoardRect.left +5;
row++;
y += nHeight;
}
}
for (i=nTiles; i MoveWindow (rghTileWnd[i], 0,0, 0,0, TRUE);
}

/********************
SetupGame
********************/
FAR PASCAL SetupGame()
{
int i, type, j, pos;

nBoardRows = MAX_BOARDROWS;
nBoardCols = MAX_BOARDCOLS;

nTiles = nBoardRows * nBoardCols;
nTilePairs = nTiles/2;

hTile1Wnd = NULL;
hTile2Wnd = NULL;
bFirstTile = TRUE;

for (i=0; i rgTileType[i] = TILETYPE_NULL;
for (i=0; i { /* choose a tile type and put it into two random positions */
type = (i % NUM_TILETYPES) +1;
for (j=1; j<=2; j++)
{
while ( (pos=rand())>=nTiles || rgTileType[pos]!=TILETYPE_NULL)
;
rgTileType[pos] = type;
}
}

nAttempts = 0;
nMatches = 0;
}

/********************
CreateGameWindows
*********************/
FAR PASCAL CreateGameWindows()
{
WNDCLASS rClass;
int i;

hTileBackBrush = CreateHatchBrush (HS_DIAGCROSS, RGB(0,0,255));
rghBrush[0] = CreateSolidBrush (RGB(255, 0, 0));
rghBrush[1] = CreateSolidBrush (RGB(255,255, 0));
rghBrush[2] = CreateSolidBrush (RGB( 0, 0, 0));
rghBrush[3] = CreateSolidBrush (RGB(255, 0,255));
rghBrush[4] = CreateSolidBrush (RGB( 0,255, 0));
rghBrush[5] = CreateSolidBrush (RGB( 0,255,255));
rghBrush[6] = CreateSolidBrush (RGB( 0, 0,255));
rghPen[0] = CreatePen (0, 3, RGB(255, 0, 0));
rghPen[1] = CreatePen (0, 3, RGB(255,255, 0));
rghPen[2] = CreatePen (0, 3, RGB( 0, 0, 0));
rghPen[3] = CreatePen (0, 3, RGB(255, 0,255));
rghPen[4] = CreatePen (0, 3, RGB( 0,255, 0));
rghPen[5] = CreatePen (0, 3, RGB( 0,255,255));
rghPen[6] = CreatePen (0, 3, RGB( 0, 0,255));

rClass.lpszClassName = "CONCENCLASS_TILE";
rClass.hInstance = hInst;
rClass.lpfnWndProc = TileWinProc;
rClass.hCursor = LoadCursor(NULL, IDC_ARROW);
rClass.hIcon = NULL;
rClass.lpszMenuName = NULL;
rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
rClass.style = CS_HREDRAW|CS_VREDRAW;
rClass.cbClsExtra = 0;
rClass.cbWndExtra = 0;
RegisterClass((LPWNDCLASS)&rClass);

rClass.lpszClassName = "CONCENCLASS_ATTEMPTS";
rClass.hInstance = hInst;
rClass.lpfnWndProc = AttemptsWinProc;
rClass.hCursor = LoadCursor(NULL, IDC_ARROW);
rClass.hIcon = NULL;
rClass.lpszMenuName = NULL;
rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
rClass.style = CS_HREDRAW|CS_VREDRAW;
rClass.cbClsExtra = 0;
rClass.cbWndExtra = 0;
RegisterClass((LPWNDCLASS)&rClass);

rClass.lpszClassName = "CONCENCLASS_MATCHES";
rClass.hInstance = hInst;
rClass.lpfnWndProc = MatchesWinProc;
rClass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
rClass.hIcon = NULL;
rClass.lpszMenuName = NULL;
rClass.hbrBackground = GetStockObject(WHITE_BRUSH) ;
rClass.style = CS_HREDRAW|CS_VREDRAW;
rClass.cbClsExtra = 0;
rClass.cbWndExtra = 0;
RegisterClass((LPWNDCLASS)&rClass);


for (i=0; i rghTileWnd[i] = CreateWindow("CONCENCLASS_TILE",
"",
WS_CHILD | WS_BORDER | WS_VISIBLE,
0, 0, /* x,y top, left corner */
0, 0, /* cx, cy width, height */
hBoardWnd, /* parent */
i, /* menu / child id */
(HANDLE)hInst,
(LPSTR)NULL
);

hAttemptsWnd = CreateWindow("CONCENCLASS_ATTEMPTS",
"",
WS_CHILD | WS_VISIBLE,
0, 0, /* x,y top, left corner */
0, 0, /* cx, cy width, height */
hBoardWnd, /* parent */
MAX_TILES+0, /* menu / child id */
(HANDLE)hInst,
(LPSTR)NULL
);

hMatchesWnd = CreateWindow("CONCENCLASS_MATCHES",
"",
WS_CHILD | WS_VISIBLE,
0, 0, /* x,y top, left corner */
0, 0, /* cx, cy width, height */
hBoardWnd, /* parent */
MAX_TILES+1, /* menu / child id */
(HANDLE)hInst,
(LPSTR)NULL
);
}


  3 Responses to “Category : C Source Code
Archive   : CONCEN.ZIP
Filename : CONCEN.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/