/* C source for Winsock Chess Revision 1994-03-15 Modified by Donald Munro for use as a 2 player chess game over a WINSOCK layer on a TCP (or other WinSock supporting) network. Source code and make files for MS Visual C/C++ V1.00/1.50. February/March 1994 All GNU copyright and distribution conditions as described below and in the file COPYING also apply to WinSock Chess. This module is adapted from GNU Chess. C source for GNU CHESS Revision: 1990-09-30 Modified by Daryl Baker for use in MS WINDOWS environment Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc. Copyright (c) 1988, 1989, 1990 John Stanback This file is part of CHESS. CHESS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the CHESS General Public License for full details. Everyone is granted permission to copy, modify and redistribute CHESS, but only under the conditions described in the CHESS General Public License. A copy of this license is supposed to have been given to you along with CHESS so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ #define NOATOM /*Minimize windows.h processing*/ #define NOCLIPBOARD #define NOCREATESTRUCT #define NOFONT #define NOREGION #define NOSOUND #define NOWH #define NOKANJI #define STRICT #include #include #include #include #include #include #include "winsock.h" #include "gnuchess.h" #include "defs.h" #include "chess.h" #include "saveopen.h" #include "color.h" #include "resource.h" // Message Cracker for socket message // void OnWsConnect(HWND hwnd, SOCKET socket, WORD wEvent, WORD wError) #define HANDLE_WM_SOCKET(hwnd, wParam, lParam, fn) \ (fn)((hwnd), (SOCKET)(wParam), (WORD)(WSAGETSELECTEVENT(lParam)), \ (WORD)(WSAGETSELECTERROR(lParam))) DWORD clrBackGround; /* rgb structures for various colors */ DWORD clrBlackSquare; DWORD clrWhiteSquare; DWORD clrBlackPiece; DWORD clrWhitePiece; DWORD clrText; static HBRUSH hBrushBackGround; short boarddraw[64]; /* Display copies of the board */ short colordraw[64]; /* Needed because while computer is calculating*/ /* moves it updates board and color thus you can*/ /* not repaint the screen accuratly */ struct PIECEBITMAP pieces[7]; HINSTANCE hInst; /* current instance */ char szAppName[] = "WSChess"; extern char mvstr[4][6]; int coords = 1; static int FirstSq = -1; /* Flag is a square is selected */ static int GotFirst = FALSE; static int EditActive = FALSE; int User_Move = TRUE; int xchar, ychar; static HMENU hMainMenu; HWND hwndHostDlg=NULL; DWORD idDdeServInst,idDdeClntInst; WORD wComType=IDC_SOCKETS,wPacketSize; BOOL bConnected=FALSE,bHost=FALSE,bMoveReady=FALSE,bWaiting=FALSE; BOOL (*Initialise)(void); BOOL (*ConnectHost)(void); BOOL (*ConnectClient)(void); void (*GetOpponentsMove)(WORD); void (*SendMove)(WORD); BOOL (*IsCommand)(LPSTR); void (*LoadGame)(void); void (*StartGame)(void); void (*SetTime)(int); HSZ hszServName,hszTopic,hszServNameCl,hszTopicCl,hszItem,hszGet; HCONV hconvHost,hconvClient; HWND hwndMain,hwndStatus; MoveInfo moveinfo; char szSockDesc[45]; void OnSocketMessage(HWND , SOCKET , WORD , WORD ); void EnableMenuItems(void) //------------------------ { HMENU hmenuMain; hmenuMain = GetMenu(hwndMain); if ( (bConnected) && (User_Move) ) { EnableMenuItem(hmenuMain,MSG_CHESS_NEW,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,MSG_CHESS_EDIT,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,MSG_CHESS_GET,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_TIMECONTROL,MF_ENABLED | MF_BYCOMMAND); } else { EnableMenuItem(hmenuMain,MSG_CHESS_NEW,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,MSG_CHESS_EDIT,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,MSG_CHESS_GET,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_TIMECONTROL,MF_GRAYED | MF_BYCOMMAND); } if (bConnected) { EnableMenuItem(hmenuMain,IDM_DISCONNECT,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_PROTOCOL,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_HOST,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_CLIENT,MF_GRAYED | MF_BYCOMMAND); } else { EnableMenuItem(hmenuMain,IDM_DISCONNECT,MF_GRAYED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_PROTOCOL,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_HOST,MF_ENABLED | MF_BYCOMMAND); EnableMenuItem(hmenuMain,IDM_CLIENT,MF_ENABLED | MF_BYCOMMAND); } } int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) //-------------------------------------------------------------- { HWND hWnd; MSG msg; POINT pt; lpCmdLine ++; if (!hPrevInstance) if (!ChessInit(hInstance)) return (NULL); hInst = hInstance; /* Saves the current instance */ QueryBoardSize (&pt); /* Create the main window. It will be autosized in WM_CREATE message */ hWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (!hWnd) return (NULL); hwndMain = hWnd; ShowWindow(hWnd, nCmdShow); /* Initialize chess */ if (init_main ( hWnd ) ) { SMessageBox (hWnd, IDS_INITERROR,IDS_CHESS); FreeGlobals (); return (NULL); } UpdateWindow(hWnd); EnableMenuItems(); player = opponent; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } void CreateStatusBar(HWND hWnd,HINSTANCE hInst,short ychar) //--------------------------------------------------------- { RECT rectWinRect; int ny; GetClientRect(hWnd, &rectWinRect); ny = rectWinRect.bottom - (15 + ychar); hwndStatus = CreateWindow("STATBR",NULL,WS_CHILD | WS_VISIBLE, 0,ny,rectWinRect.right,rectWinRect.bottom - ny, hWnd,(HMENU)5000,hInst,NULL); ShowWindow(hwndStatus,SW_SHOW); } // Handle WM_CREATE message BOOL OnCreate(HWND hWnd,CREATESTRUCT FAR *lpCreateStruct) //------------------------------------------------------- { int i; HDC hDC; TEXTMETRIC tm; POINT point; hwndMain = hWnd; GetStartupColors(); hBrushBackGround = CreateSolidBrush ( clrBackGround ); for ( i=pawn; i55)) ) { algbr_flag = promote + PromoteDialog (hWnd, hInst); } else algbr_flag = 0; algbr ( First, Square, algbr_flag); lstrcpy(str,mvstr[0]); temp = VerifyMove ( hWnd, str, 0, &mv); if ( temp == TRUE) { ElapsedTime (1); strcpy(moveinfo.szMove,str); strcat(moveinfo.szMove,"\r\n"); (*SendMove)(7); PostMessage ( hWnd, MSG_COMPUTER_MOVE, NULL, (LPARAM)NULL); } else { User_Move = TRUE; PostMessage ( hWnd, MSG_USER_MOVE, NULL, (LPARAM)NULL); } } break; case MSG_COMPUTER_MOVE: EnableMenuItems(); RedrawStatusBar(); if ( !(flag.quit || flag.mate || flag.force) ) { User_Move = FALSE; if (! bWaiting) (*GetOpponentsMove)(7); else { bWaiting = FALSE; swap(&opponent,&computer); player = opponent; strncpy(str,moveinfo.szMove,5); str[5] = 0; if (str[4] == '\r') str[4] = 0; if (! ((*IsCommand)(str))) { temp = VerifyMove ( hWnd, str, 0, &mv); if ( flag.beep ) MessageBeep (0); swap(&opponent,&computer); User_Move = TRUE; OutputMove(hWnd); PostMessage (hWnd, MSG_USER_MOVE, NULL,NULL); } else { swap(&opponent,&computer); if (!User_Move) PostMessage ( hWnd, MSG_COMPUTER_MOVE, NULL, (LPARAM)NULL); else PostMessage ( hWnd, MSG_USER_MOVE, NULL, (LPARAM)NULL); } } } break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } return (NULL); }