4방 체스의 AI에 대해, 4방 체스 마스터가 입력합니다.
#include
HINSTANCE hInst;
HWND hWnd
ATOM MyRegisterClass(HINSTANCE hInstance); p>
p>
BOOL InitInstance(HINSTANCE, int);
LRESULT 콜백 WndProc(HWND, UINT, WPARAM, LPARAM)
void MyPaint();
BOOL XiaQi(int whichPlay, LPARAM lParam)
BOOL IsWin(int whichPlay)
void SetMap(); HBITMAP bmp[3] ,bg,bufBmp;
HDC mdc,hdc,bufDC
int who=1
const int h=11,w =12;
p>
const int picH=40,picW=40;
int map[w][h]
이제 통과;
void SetMap()
{
for(int i=0; i { for( int j=0; j { map[i][j]=0; } } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg MyRegisterClass(hInstance); >if (!InitInstance (hInstance, nCmdShow) ) { return FALSE } while( msg.message! =WM_QUIT ) { if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { now=GetTickCount( ); if (pass-now>=40) { MyPaint(); } } } return msg.wParam; p> } p> ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; .cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; hCursor = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW) wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); .lpszMenuName = NULL; wcex .lpszClassName = "캔버스" wcex.hIconSm = NULL; } BOOL InitInstance (HINSTANCE hInstance, int nCmdShow) { hInst = hInstance hWnd = CreateWindow( "캔버스", "고뱅" , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,0, 0, NULL, NULL, hInstance, NULL) if (!hWnd) { 반환 FALSE } SetMap() bmp[0]=(HBITMAP)LoadImage( NULL,"white.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE) bmp[1]=(HBITMAP)LoadImage(NULL,"red.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE) ; bmp[2]= (HBITMAP)LoadImage(NULL,"black.bmp",IMAGE_BITMAP,picW,picH,LR_LOADFROMFILE) if(!bmp[0] | | !bmp[1] || !bmp[2] ) { MessageBox(hWnd, "불필요한 리소스를 찾으면 프로그램이 종료됩니다.", "오류 메시지", MB_ICONERROR); 종료프로세스( 0) } MoveWindow(hWnd,10,10,640,48 0,true); ShowWindow(hWnd, nCmdShow) UpdateWindow(hWnd) hdc=GetDC(hWnd); p>SetTextColor(hdc,RGB(255,0,0)); TextOut(hdc,500,100,"빨간색 면이 먼저 재생됩니다.",strlen("빨간색 면이 먼저 재생됩니다.")) mdc=CreateCompatibleDC(hdc); bufDC=CreateCompatibleDC(hdc); bufBmp=CreateCompatibleBitmap(hdc,w*picW,h*picW); /p> SelectObject(mdc,bufBmp); MyPaint(); TRUE를 반환합니다. } LRESULT CALLBACK WndProc(HWND hWnd, UINT 메시지, WPARAM wParam, LPARAM lParam) { int i=0 스위치(메시지) { case WM_LBUTTONDOWN: if(XiaQi(who,lParam)) { MyPaint() if(who==1) { if(IsWin(who)) { MessageBox(hWnd,"레드 팀이 승리했습니다.","축하합니다",MB_OK) SetMap() } who=2; /p> p> SetTextColor(hdc,RGB(0,0,0)) TextOut(hdc,500,100,"Black이 체스를 둘 차례입니다",strlen(" 이제 블랙이 체스를 둘 차례입니다." ")) } else { if(IsWin(who)) { MessageBox(hWnd,"흑인이 승리했습니다.","축하합니다",MB_OK) SetMap() } who=1; SetTextColor(hdc,RGB(255,0,0)) TextOut(hdc,500,100,"빨간색 면입니다. Turn to play",strlen( "Red가 플레이할 차례입니다.")); } } break; case WM_DESTROY: for(i=0; i<3; i++) { DeleteObject(bmp[i]) } ReleaseDC(hWnd,bufDC) DeleteObject(bufBmp) ReleaseDC(hWnd,mdc); p> ReleaseDC(hWnd,hdc); PostQuitMessage(0); break; return DefWindowProc(hWnd) , 메시지, wParam, lParam) } return 0 } void MyPaint() { SelectObject(mdc,bg) for(int i=0; i { for(int j=0; j { SelectObject(bufDC,bmp[map[i][j]]) BitBlt(mdc,picW*i,picW*j,picW*w,picH*h,bufDC,0,0,SRCCOPY) } } p> p> BitBlt(hdc,0,0,picW*w,picH*h,mdc,0,0,SRCCOPY) pass=GetTickCount() } BOOL XiaQi(int whichPlay, LPARAM lParam) { if( LOWORD(lParam) <= picW*w && LOWORD(lParam) >= 0 && HIWORD(lParam) <= picH*h && HIWORD(lParam)>=0) { int x=0,y =0 ; for(int i=1; i<=w; i++) { if( i*picW >= LOWORD(lParam) ) { x=i; x-- 중단; p> } for(int j=1; j<=h; j++) { if( j*picH >= HIWORD (lParam ) ) { y=j; y--; } } if(whichPlay==1) { if(map[x][y]==0 ) { 지도[x][y]=1 } else { p> 거짓을 반환 } } else { if(map [x][y]==0) { map[x][y]=2 } < /p> else { FALSE 반환 } } TRUE 반환; ; } else { FALSE 반환 } } BOOL IsWin(int whichPlay) { int num=0 for(int i=0; i< w; i++) { for(int j=0; j { if(whichPlay ==1) { if(map[i][j]==2 || map[i][j]==0 ) { 숫자=0; } if(map[i][j]==1 ) { 숫자++; if(num==5) { 반환 TRUE } p> p> } } else { if(map[i][j ]==1 | 지도[i][j]==0) { 숫자=0 } > if(map[ i][j]==2) { 숫자++ if(num==5) { p> TRUE를 반환합니다. } } } } } p> num=0; for( i=0; i { for( int j=0; j { if(whichPlay==1) { if( 지도[j][ i]==2 || 지도[j][i]==0) { num=0; if(map[j][i]==1) { 숫자++ if(num==5 ) { TRUE를 반환합니다. } } } else { if(map[j][i]==1||map[j][i]==0) { 숫자=0; } if( map[j][i]==2) { 숫자++ if(num==5) { TRUE를 반환합니다. } } } } } 숫자=0; int k=0; for(i=w-1; i>=0; i--) { for(int j=0; j<=i; j++) { if(whichPlay==1) p> p> { if(map[j+k][j]==0 || 지도[j+k][j]==2) { 숫자=0; } if(map[j+k][j]==1) { 숫자++; } if(num==5) { 반환 TRUE; p> } } else { if(map[j+ k][j]== 0 || 맵[j+k][j]==1) { num=0; if(map[j+k][j]==2) { 숫자++ } if(num ==5) { 반환 TRUE } } } 숫자=0; k++ } 숫자=0; =0; for(i=w-1; i>=0; i--) { for(int j=0; j <=i; j++) { if(whichPlay==1) { if(map[j][ j+k]==0 || 지도[j][j+k]==2) { 숫자=0 } if (map[j][j+k]==1) { 숫자++ } if(num= =5) { 반환 TRUE } } else { if(map[j][j+k]==0 || map[j][j+k]==1) { 번호=0; } if(map[j][j+k]==2) { 숫자++; > } if(num==5) { TRUE 반환 } } } 숫자=0; k++ } 숫자=0; for(i=0; i { k=i for(int j= w-1; j>=i; j--,k++) { if(whichPlay==1) { if(map[j][k]==0 || 지도[j][k]==2) { num=0; } if(map[j][k]==1) { 숫자++; /p> if(num==5) { 반환 TRUE } } else { if(map[j][k]==0 || map[j][k]==1) { 숫자=0; } if(map[j][k]==2) { 숫자++; } if(num==5) { 반환 } } } 숫자=0 } 숫자 =0; for( i=w-1; i>=0; i--) { for(int j=i; j>=0; j--,k++) { if(whichPlay==1) { if(map[j][k]==0 || 지도[j][k]==2) { num= 0; } if(map[j][k]==1) { 숫자++; > } if(num==5) { TRUE 반환 } } else { if(map[j][k]==0 || map[j][k]==1 ) p> { 숫자=0; } if(map[j][k]==2) { 숫자++; } if(num==5) { 반환 } } } 숫자=0 } return 거짓; }