watzee.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 662 行 · 第 1/2 页
C
662 行
#include <windows.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "watzee.h"
static char *AppName = "Watzee";
static char ClassName[32]="Watzee";
static void DoScore( HDC, WORD );
static void DoScoreTotals( HDC );
static WORD GetDieCheck( HWND, POINT );
static WORD GetScoreCheck( POINT );
static void InitializeGameData( void );
static void NextPlayer( HWND, HDC );
long _EXPORT FAR PASCAL WndProc( HWND, UINT, WPARAM, LPARAM );
/*
* WinMain -- register the window class and call some initialization
* routines; then send a message to WndProc to start a new
* game
*/
int PASCAL WinMain( HINSTANCE instance, HINSTANCE previnstance,
LPSTR cmdline, int cmdshow )
/*******************************************************/
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
cmdline = cmdline;
previnstance = previnstance;
#ifdef __WINDOWS_386__
sprintf( ClassName,"Watzee%d",instance );
#else
if( !previnstance ) {
#endif
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (LPVOID) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 6 * sizeof( DWORD );
wndclass.hInstance = instance;
wndclass.hIcon = LoadIcon( instance, AppName );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = AppName;
wndclass.lpszClassName = ClassName;
RegisterClass( &wndclass );
#ifndef __WINDOWS_386__
}
#endif
InitializeGameData();
GetFontInfo();
CreateFonts();
hwnd = CreateWindow( ClassName, "WATZEE",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CharWidth * 28 + CharWidth / 2, CharHeight * 28,
NULL, NULL, instance, NULL );
GetDiceBitmaps( hwnd );
ShowWindow( hwnd, cmdshow );
UpdateWindow( hwnd );
SendMessage( hwnd, WMW_START_NEW_GAME, 0, 0 );
while( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
/*
* WndProc -- process messages from Windows
*/
long _EXPORT FAR PASCAL WndProc( HWND hwnd, UINT message, WPARAM wparam,
LPARAM lparam )
/*********************************************************************/
{
static HANDLE hdlginstance;
static BOOL got_watzee_bonus;
PAINTSTRUCT ps;
FARPROC dlg_proc;
HDC hdc;
POINT point;
short x;
short y;
short dy;
short i;
switch( message ) {
case WM_CREATE :
hdlginstance = ((CREATESTRUCT far *) MK_FP32((void*)lparam))->hInstance;
x = CharWidth * 22 + CharWidth / 2;
y = CharHeight * 3;
dy = CharHeight * 3;
dy += dy / 7;
/* create the check marks for the dice bitmaps, and the ROLL and
OK buttons */
for( i = 0; i < 5; i++, y += dy ) {
CreateWindow( "BUTTON", "", WS_CHILD|WS_VISIBLE|BS_CHECKBOX,
x, y, CharWidth, CharHeight, hwnd,
(HMENU)(IDW_DICE1+i),
hdlginstance,
NULL );
}
CreateWindow( "BUTTON", "ROLL", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x, CharHeight * 19, 9 * CharWidth / 2 ,
CharHeight * 2, hwnd, (HMENU)IDW_ROLL,
hdlginstance, NULL );
CreateWindow( "BUTTON", "OK", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x, 43 * CharHeight / 2, 9 * CharWidth / 2,
CharHeight * 2, hwnd, (HMENU)IDW_OK,
hdlginstance, NULL );
return( 0 );
case WMW_START_NEW_GAME :
dlg_proc = MakeProcInstance( (FARPROC)GetNumPlayersDialogProc, hdlginstance );
DialogBox( hdlginstance, "GetNumPlayers", hwnd, (DLGPROC)dlg_proc );
FreeProcInstance( dlg_proc );
dlg_proc = MakeProcInstance( (FARPROC)GetInitialsDialogProc, hdlginstance );
DialogBox( hdlginstance, "GetPlayersInitials", hwnd, (DLGPROC)dlg_proc );
FreeProcInstance( dlg_proc );
EnableWindow( GetDlgItem( hwnd, IDW_OK ), FALSE );
EnableWindow( GetDlgItem( hwnd, IDW_ROLL ), TRUE );
PlayingGameYet = TRUE;
InvalidateRect( hwnd, NULL, FALSE );
for( i = 0; i < NumberOfPlayers; i++ ) {
if( Player[i][IS_COMPUTER] ) break;
}
if( i < NumberOfPlayers && !GotTimer ) {
while( !SetTimer( hwnd, ID_TIMER, TIMER_INTERVAL, NULL ) ) {
if( MessageBox( hwnd, "Too many clocks or timers are active",
AppName, MB_ICONEXCLAMATION|MB_RETRYCANCEL ) == IDCANCEL ) {
DestroyWindow( hwnd );
return( 0 );
}
}
GotTimer = TRUE;
}
if( i == 0 ) {
PCTurn = TRUE;
EnableWindow( GetDlgItem( hwnd, IDW_ROLL ), FALSE );
}
hdc = GetDC( hwnd );
RollDice( hwnd, hdc );
GetDiceInfo();
ReleaseDC( hwnd, hdc );
// SetFocus( hwnd );
return( 0 );
case WM_COMMAND :
switch( LOWORD( wparam ) ) {
case IDM_NEWGAME :
if( GotTimer ) {
KillTimer( hwnd, ID_TIMER );
}
if( MessageBox( hwnd, "Start New Game: Are you sure?", "WATZEE",
MB_YESNO | MB_ICONEXCLAMATION ) == IDYES ) {
InitializeGameData();
InvalidateRect( hwnd, NULL, TRUE );
SendMessage( hwnd, WMW_START_NEW_GAME, 0, 0 );
} else if( GotTimer ) {
SetTimer( hwnd, ID_TIMER, TIMER_INTERVAL, NULL );
}
break;
case IDM_OPTIONS :
if( GotTimer ) {
KillTimer( hwnd, ID_TIMER );
}
dlg_proc = MakeProcInstance( (FARPROC)OptionsDialogProc, hdlginstance );
DialogBox( hdlginstance, "Options", hwnd, (DLGPROC)dlg_proc );
FreeProcInstance( dlg_proc );
if( GotTimer ) {
SetTimer( hwnd, ID_TIMER, TIMER_INTERVAL, NULL );
}
break;
case IDM_EXIT :
DestroyWindow( hwnd );
break;
case IDM_SCORING :
if( GotTimer ) {
KillTimer( hwnd, ID_TIMER );
}
dlg_proc = MakeProcInstance( (FARPROC)HelpDialogProc, hdlginstance );
DialogBox( hdlginstance, "WatzeeHelp", hwnd, (DLGPROC)dlg_proc );
FreeProcInstance( dlg_proc );
if( GotTimer ) {
SetTimer( hwnd, ID_TIMER, TIMER_INTERVAL, NULL );
}
break;
case IDM_ABOUT :
if( GotTimer ) {
KillTimer( hwnd, ID_TIMER );
}
dlg_proc = MakeProcInstance( (FARPROC)AboutDialogProc, hdlginstance );
DialogBox( hdlginstance, "AboutWatzee", hwnd, (DLGPROC)dlg_proc );
FreeProcInstance( dlg_proc );
if( GotTimer ) {
SetTimer( hwnd, ID_TIMER, TIMER_INTERVAL, NULL );
}
break;
case IDW_DICE1 :
case IDW_DICE2 :
case IDW_DICE3 :
case IDW_DICE4 :
case IDW_DICE5 :
if( !PCTurn && CurrentRoll < 2 ) {
SendMessage( hwnd, WMW_DIE_CHECK, wparam, 0 );
}
break;
case IDW_ROLL :
SendMessage( hwnd, WMW_ROLL, 0, 0 );
break;
case IDW_OK :
SendMessage( hwnd, WMW_OK, 0, 0 );
break;
}
return( 0 );
case WM_LBUTTONUP :
if( !PCTurn ) {
MAKE_POINT( point, lparam );
if( point.x > CharWidth * 24
&& point.y < CharHeight * 20 ) {
wparam = GetDieCheck( hwnd, point );
if( wparam && CurrentRoll < 2 ) {
SendMessage( hwnd, WMW_DIE_CHECK, wparam, 0 );
}
} else {
wparam = GetScoreCheck( point );
if( wparam && Player[CurrentPlayer][wparam] == UNDEFINED ) {
SendMessage( hwnd, WMW_SCORE_CHECK, wparam, 0 );
}
}
}
return( 0 );
case WMW_DIE_CHECK :
i = LOWORD( wparam ) - IDW_DICE1;
Dice[i].is_checked = !Dice[i].is_checked;
CheckDlgButton( hwnd, wparam, (BOOL)Dice[i].is_checked );
// SetFocus( hwnd );
return( 0 );
case WMW_SCORE_CHECK :
hdc = GetDC( hwnd );
if( wparam != LastScoreSelection ) {
DoScore( hdc, wparam );
EnableWindow( GetDlgItem( hwnd, IDW_OK ), TRUE );
}
ReleaseDC( hwnd, hdc );
// SetFocus( hwnd );
return( 0 );
case WMW_ROLL :
hdc = GetDC( hwnd );
if( DieCheckMeansRoll ) {
for( i = 0; i < 5; i++ ) {
if( Dice[i].is_checked ) break;
}
} else {
for( i = 0; i < 5; i++ ) {
if( !Dice[i].is_checked ) break;
}
}
if( i < 5 ) {
if( LastScoreSelection ) {
Player[CurrentPlayer][LastScoreSelection] = UNDEFINED;
WriteScore( hdc, CurrentPlayer, LastScoreSelection );
DoScoreTotals( hdc );
LastScoreSelection = 0;
}
got_watzee_bonus = FALSE;
CurrentRoll++;
RollDice( hwnd, hdc );
GetDiceInfo();
if( DiceInfo.got_watzee && Player[CurrentPlayer][WATZEE] == 50 ) {
got_watzee_bonus = TRUE;
}
if( CurrentRoll == 2 ) {
EnableWindow( GetDlgItem( hwnd, IDW_ROLL ), FALSE );
}
}
ReleaseDC( hwnd, hdc );
// SetFocus( hwnd );
return( 0 );
case WMW_OK :
hdc = GetDC( hwnd );
LastScoreSelection = 0;
if( got_watzee_bonus ) {
DoScore( hdc, WATZEE_BONUS );
LastScoreSelection = 0;
got_watzee_bonus = FALSE;
}
NextPlayer( hwnd, hdc );
ReleaseDC( hwnd, hdc );
// SetFocus( hwnd );
return( 0 );
case WMW_GAME_OVER :
hdc = GetDC( hwnd );
WriteScoreOptions( hdc );
ReleaseDC( hwnd, hdc );
if( GotTimer ) {
KillTimer( hwnd, ID_TIMER );
GotTimer = FALSE;
}
if( MessageBox( hwnd, "Another Game ?", "WATZEE", MB_YESNO ) == IDYES ) {
InitializeGameData();
InvalidateRect( hwnd, NULL, TRUE );
SendMessage( hwnd, WMW_START_NEW_GAME, 0, 0 );
} else {
DestroyWindow( hwnd );
}
return( 0 );
case WM_TIMER :
if( PCTurn ) {
PCPlay( hwnd );
}
return( 0 );
case WM_PAINT :
hdc = BeginPaint( hwnd, &ps );
DrawDice( hwnd, hdc );
DrawScoreCard( hdc );
WriteScoreOptions( hdc );
if( PlayingGameYet ) {
WriteInitials( hdc );
HighliteName( hdc, CurrentPlayer );
WriteScores( hdc );
}
EndPaint( hwnd, &ps );
return( 0 );
case WM_DESTROY :
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?