홈
태그
미디어로그
위치로그
방명록
분류 전체보기
(112)
0x01 Notice
(11)
diary
(8)
0x02 Tips
(7)
iOS
(5)
Facebook
(2)
Twitter
(0)
0x03 Thinks
(4)
0x04 Security
(14)
news
(10)
web
(0)
mobile
(0)
system
(1)
forensic
(0)
vulnerabilities
(3)
0x05 Programming
(76)
C / C++
(12)
Java
(5)
Object -C
(3)
API / MFC
(11)
Unix/Linux
(6)
Socket Programing
(6)
Kelnel Programing
(6)
iPhone Programing
(19)
Android Programing
(5)
Web Programming
(2)
ABOUT ME
-
트위터
인스타그램
Today
-
Yesterday
-
Total
-
::j0ker's Lab::
::j0ker's Lab::
메뉴
검색
컨텐츠 검색
블로그 내 검색
[API] 숫자퍼즐 게임체크 & 시간 & 스텝 구현
0x05 Programming/API / MFC
2009. 9. 12. 14:15
#include "stdafx.h" #include <time.h> #define OK 0 #define NO -1 #define LEVEL 10 int g_data[3][3]={ {1,2,3}, {4,5,6}, {7,8,9} }; int g_step = 0; int g_time = 0; int CanMove(int sx, int sy, int dx, int dy) { if(sx < 0 || sy < 0 || dx < 0 || dy < 0) { return NO; } if(2 < sx || 2 < sy || 2 < dx || 2 < dy) { return NO; } if(9 == g_data[dy][dx]) { g_data[dy][dx] = g_data[sy][sx]; g_data[sy][sx] = 9; return OK; } return NO; } int GameOver() { int x,y; int value=1; for(y=0;y<3;y++) { for(x=0;x<3;x++) { if(value != g_data[y][x]) { return NO; } value++; } } return OK; } void MyIni(HWND hWnd, WPARAM wParam, LPARAM lParam) { int dx,dy; int dir; int i; dx = 2; dy = 2; for(i=0;i<LEVEL;i++) { dir = rand()%4; switch(dir) { case 0: if(OK == CanMove(dx-1,dy,dx,dy)) { dx = dx-1; } break; case 1: if(OK == CanMove(dx+1,dy,dx,dy)) { dx = dx+1; } break; case 2: if(OK == CanMove(dx,dy+1,dx,dy)) { dy = dy+1; } break; case 3: if(OK == CanMove(dx,dy-1,dx,dy)) { dy = dy-1; } break; } } SetTimer(hWnd, 1, 1000,NULL); } void MyTimer(HWND hWnd, WPARAM wParam, LPARAM lParam) { char temp[80]; if(1 == wParam) { g_time ++; if(g_time == 60) { KillTimer(hWnd, 1); wsprintf(temp,"Your Steps : %d\n",g_step); MessageBox(hWnd,temp,"Game Over",MB_OK); } } InvalidateRect(hWnd, NULL, TRUE); } void MyPaint(HWND hWnd, WPARAM wParam, LPARAM lParam, HDC hdc) { int x,y ; int xpos,ypos; char temp[60]; for(y=0;y<3;y++) { for(x=0;x<3;x++) { xpos = x*100; ypos = y*100; Rectangle(hdc,xpos,ypos,xpos+100,ypos+100); if(9 != g_data[y][x]) { wsprintf(temp,"%d",g_data[y][x]); TextOut(hdc,xpos+40,ypos+40,temp,strlen(temp)); } } } wsprintf(temp,"Step : %d",g_step); TextOut(hdc,350,30,temp,strlen(temp)); wsprintf(temp,"Time : %d",g_time); TextOut(hdc,350,50,temp,strlen(temp)); if(0 == GameOver()) { KillTimer(hWnd,1); wsprintf(temp,"Your Steps : %d Steps\n During Time : %d Seconds",g_step,g_time); MessageBox(hWnd,"Congratz !!","made by j0ker",MB_OK); MessageBox(hWnd,temp,"made by j0ker",MB_OK); } } void MyLbutton(HWND hWnd, WPARAM wParam, LPARAM lParam) { int x,y; int xpos,ypos; char temp[20]; xpos = LOWORD(lParam); ypos = HIWORD(lParam); x = xpos/100; y = ypos/100; if(0 == GameOver()) { return ; } if(OK == CanMove(x,y,x+1,y)) { g_step++; InvalidateRect(hWnd, NULL, TRUE); } if(OK == CanMove(x,y,x-1,y)) { g_step++; InvalidateRect(hWnd,NULL,TRUE); } if(OK == CanMove(x,y,x,y+1)) { g_step++; InvalidateRect(hWnd,NULL,TRUE); } if(OK == CanMove(x,y,x,y-1)) { g_step++; InvalidateRect(hWnd, NULL, TRUE); } }
1. WM_PAINT 연결
2. WM_CREATE 연결
3. WM_TIMER 연결
4. WM_LBUTTONDOWN 연결
모두 즐밍하세요 :D
공유하기
게시글 관리
::j0ker's Lab::
관련글
관련글 더보기
[API]산성비 응용
[API] 09월 25일 수업내용
[API]09월11일 수업내용정리
[API] 09월 04일 수업내용 정리
티스토리툴바