Hisworld.tistory.com
The hisWorld
(140)
hisworld_new
(0)
hisOld
(138)
Android
(7)
Computer Vision
(18)
JAVA
(4)
C/C++
(26)
열혈강의 C
(32)
TCP/IP socket
(12)
WinAPI
(16)
System Programming
(0)
etc
(23)
Plan
(0)
hisWorld
(0)
hiStory
(0)
hiStudy
(0)
hiSnap
(0)
홈
태그
미디어로그
위치로그
방명록
Windows Live Messenger
winapi
Join C
대학생 공모전
The Turing test page
훈스닷넷
C/C++ Reference
WIKI
/
/
블로그 내 검색
Sokoban
URUZ-7
2009. 1. 20. 03:03
2009. 1. 20. 03:03
#include "Turboc.h" #define LEFT 75 //키보드 키 값 정의 #define RIGHT 77 #define UP 72 #define DOWN 80 #define MAXSTAGE 3 //최대 스테이지 정의 #define putchxy(x,y,c) {gotoxy(x,y);putch(c);} //x,y좌표에 c를 출력하는 매크로 void DrawScreen(); //화면 그리는 함수 BOOL TestEnd(); //성공/실패 Bool 변수 void Move(int dir); //이동처리 함수 char ns[18][21]; //스테이지 배열 int nStage; //스테이지 int nx,ny; //유저 시작위치 int nMove; //이동한 수 //스테이지 맵 : 가로 18 / 세로 21 char arStage[MAXSTAGE][18][21]={ { "####################", "####################", "####################", "##### ############", "#####O ############", "##### O############", "### O O ###########", "### # ## ###########", "# # ## ##### ..##", "# O O @ ..##", "##### ### # ## ..##", "##### ##########", "####################", "####################", "####################", "####################", "####################", "####################" }, { "####################", "####################", "####################", "####################", "####.. # ######", "####.. # O O ####", "####.. #O#### ####", "####.. @ ## ####", "####.. # # O #####", "######### ##O O ####", "###### O O O O ####", "###### # ####", "####################", "####################", "####################", "####################", "####################", "####################" }, { "####################", "####################", "####################", "####################", "########## @####", "########## O#O #####", "########## O O#####", "###########O O #####", "########## O # #####", "##.... ## O O ###", "###... O O ###", "##.... ############", "####################", "####################", "####################", "####################", "####################", "####################" }, }; //main함수 void main() { int ch; int x,y; setcursortype(NOCURSOR); //WinCon.h 커서 설정 nStage=0; //스테이지 초기화 for(;1;) { memcpy(ns,arStage[nStage],sizeof(ns)); //스테이지 복사 for (y=0;y<18;y++) { for (x=0;x<20;x++) { if (ns[y][x]=='@') { nx=x; //시작위치 설정 ny=y; ns[y][x]=' '; } } } clrscr(); //화면 클리어 nMove=0; //이동횟수 초기화 for (;2;) { DrawScreen(); //화면 그림 ch=getch(); //키 입력 if (ch==0xE0 || ch==0) { ch=getch(); switch (ch) { case LEFT: case RIGHT: case UP: case DOWN: Move(ch); //ch를 인자로 Move함수 호출 break; } } else { ch=tolower(ch); if (ch=='r') { //다시 도전 break; } if (ch=='n') { //다음 스테이지 if (nStage < MAXSTAGE-1) { nStage++; } break; } if (ch=='p') { //이전 스테이지 if (nStage > 0) { nStage--; } break; } if (ch=='q') { //종료 setcursortype(NORMALCURSOR); //커서 복귀 exit(0); //강제 종료 } } if (TestEnd()) { //스테이지 클리어시 TRUE clrscr(); gotoxy(10,10); printf("%d 스테이지를 풀었습니다. 다음 스테이지로 이동합니다", nStage+1); delay(2000); if (nStage < MAXSTAGE-1) { nStage++; } break; } } } } //화면 그리기 함수 void DrawScreen() { int x,y; for (y=0;y<18;y++) { for (x=0;x<20;x++) { putchxy(x,y,ns[y][x]); //x,y좌표(배열)에 ns[y][x]의 요소를 출력 } } putchxy(nx,ny,'@'); //유저 위치에 @로 유저 표시 gotoxy(40,2);puts("SOKOBAN"); gotoxy(40,4);puts("Q:종료, R:다시 시작"); gotoxy(40,6);puts("N:다음, P:이전"); gotoxy(40,8);printf("스테이지 : %d",nStage+1); gotoxy(40,10);printf("이동 회수 : %d",nMove); } //클리어 확인 함수 BOOL TestEnd() { int x,y; for (y=0;y<18;y++) { for (x=0;x<20;x++) {//스테이지 맵에 (.)이 없어야 되고, 그 자리가 모두 (0)이어야 클리어 if (arStage[nStage][y][x]=='.' && ns[y][x]!='O') { return FALSE; //아니면 실패 } } } return TRUE; //성공 } //이동 처리 함수 void Move(int dir) { int dx=0,dy=0; //키 입력 좌표 switch (dir) { case LEFT: dx=-1; break; case RIGHT: dx=1; break; case UP: dy=-1; break; case DOWN: dy=1; break; } if(ns[ny+dy][nx+dx]!='#') //이동 계산 좌표가 벽(#)이 아닌경우 움직일 수 있다. { if(ns[ny+dy][nx+dx]=='O') //짐을 미는 경우 { //짐 뒤에 짐, 짐 뒤에 벽이 있는지 확인 if(ns[ny+dy*2][nx+dx*2] == ' ' || ns[ny+dy*2][nx+dx*2] == '.') { if(arStage[nStage][ny+dy][nx+dx]=='.') //본맵의 목적지라면 { ns[ny+dy][nx+dx]='.'; //스테이지 목적지로 그림 } else { ns[ny+dy][nx+dx]=' '; //아니면 빈공간으로 그림 } ns[ny+dy*2][nx+dx*2]='O'; //이동된 곳에 짐 다시 그림 //유저는 DrawMap에서 그려줌 } else { return; } } nx+=dx; //이동 좌표 누적 시킴 ny+=dy; nMove++; } }
api실습 소코반 이해를 돕기 위해
콘솔 기반의 WinApi.co.kr의 소코반 코드에 주석을 달면서 분석해 보았다.
공유하기
게시글 관리
Hisworld.tistory.com
+ Recent posts
Powered by
Tistory
, Designed by
wallel
Rss Feed
and
Twitter
,
Facebook
,
Youtube
,
Google+
티스토리툴바