Memory Drive

반응형
Console로 프로그래밍된 바둑 소스이다.

출처는 불분명... 어디더라??
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #define LEFT '4'
  6. #define RIGHT '6'
  7. #define UP  '8'
  8. #define DOWN '2'
  9. #define ENTER '5'
  10. #define CLEAR 0
  11. #define NONE 0
  12. #define MAPSIZE 19
  13. #define PLAYER1 1
  14. #define PLAYER2 2
  15. #define PLAY 1
  16. #define START 1
  17. #define END 0
  18. #define NO 0
  19. #define OK 1
  20. #define YES 1
  21. #define OKTOTALCOUNT 4
  22. #define FLAGTRUE 1
  23. #define FLAGFALSE 0
  24. void gotoxy(int x, int y);
  25. int xy[MAPSIZE][MAPSIZE];
  26. int map[MAPSIZE][MAPSIZE];
  27. int x, y, x2 = 2;
  28. int centerX=MAPSIZE/2, centerY=MAPSIZE/2;
  29. int displayX=0, displayLineY=21, displayY=22;
  30. char mapChar[]="┌┬┐├┼┤└┴┘";
  31. void clearValue() {
  32. int x,y;
  33. for(y=0;y<MAPSIZE;y++) {
  34. for(x=0;x<MAPSIZE;x++) {
  35.   map[y][x]=CLEAR;
  36. }
  37. }
  38. }
  39. int IsKill(int cnt) {
  40. return cnt==OKTOTALCOUNT;
  41. }
  42. int TestFlag(int x,int y) {
  43. if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return FLAGTRUE;
  44. if(map[y][x]==OK) return FLAGTRUE;
  45. return FLAGFALSE;
  46. }
  47. int SefeDeleteTest(int x,int y,int OtherPlayer) {
  48. int result;
  49. int cnt;
  50. if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return OK;
  51. if(xy[y][x]==OtherPlayer) return OK;
  52. if(xy[y][x]==NONE) return NO;
  53. cnt=0;
  54. result=NO;
  55. map[y][x]=OK;
  56. if(TestFlag(x,y-1)==FLAGFALSE) {
  57. if(SefeDeleteTest(x,y-1,OtherPlayer)) {
  58.   cnt=cnt + 1;
  59. }
  60. } else {
  61. cnt=cnt+1;
  62. }
  63. if(TestFlag(x-1,y)==FLAGFALSE) {
  64. if(SefeDeleteTest(x-1,y,OtherPlayer)) {
  65.   cnt=cnt + 1;
  66. }
  67. } else {
  68. cnt=cnt+1;
  69. }
  70. if(TestFlag(x+1,y)==FLAGFALSE) {
  71. if(SefeDeleteTest(x+1,y,OtherPlayer)) {
  72.   cnt=cnt + 1;
  73. }
  74. } else {
  75. cnt=cnt+1;
  76. }
  77. if(TestFlag(x,y+1)==FLAGFALSE) {
  78. if(SefeDeleteTest(x,y+1,OtherPlayer)) {
  79.   cnt=cnt + 1;
  80. }
  81. } else {
  82. cnt=cnt+1;
  83. }
  84. if(IsKill(cnt)){
  85. result=OK;
  86. }
  87. return result;
  88. }
  89. void DolDelete(int x,int y,int OtherPlayer) {
  90. if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return;
  91. if(xy[y][x]==OtherPlayer) return;
  92. if(xy[y][x]==NONE) return;
  93. xy[y][x]=CLEAR;
  94. if(x+1<MAPSIZE) DolDelete(x+1,y,OtherPlayer);
  95. if(x-1>=0)      DolDelete(x-1,y,OtherPlayer);
  96. if(y+1<MAPSIZE) DolDelete(x,y+1,OtherPlayer);
  97. if(y-1>=0)      DolDelete(x,y-1,OtherPlayer);
  98. }
  99. void RalDeleteTest(int x,int y,int OtherPlayer) {
  100. clearValue();
  101. if(SefeDeleteTest(x+1,y,OtherPlayer)==YES)
  102. DolDelete(x+1,y,OtherPlayer);
  103. clearValue();
  104. if(SefeDeleteTest(x-1,y,OtherPlayer)==YES)
  105. DolDelete(x-1,y,OtherPlayer);
  106. clearValue();
  107. if(SefeDeleteTest(x,y+1,OtherPlayer)==YES)
  108. DolDelete(x,y+1,OtherPlayer);
  109. clearValue();
  110. if(SefeDeleteTest(x,y-1,OtherPlayer)==YES)
  111. DolDelete(x,y-1,OtherPlayer);
  112. }
  113. void input(int Player) {
  114. char c;
  115.   int LOOP=START;
  116. int OtherPlayer;
  117. if(Player == PLAYER1) {
  118. OtherPlayer = PLAYER2;
  119. } else {
  120. OtherPlayer = PLAYER1;
  121. }
  122.   while(LOOP!=END){
  123. c = getch();
  124. switch(c)
  125. {
  126. case RIGHT:
  127.   x=x+1;
  128.   if(x >= MAPSIZE) x=x-1;
  129.   gotoxy(x,y);
  130.   break;
  131. case LEFT:
  132.   x=x-1;
  133.   if(x < 0 ) x=x+1;
  134.   gotoxy(x,y);
  135.   break;
  136. case UP:
  137.   y=y-1;
  138.   if(y < 0 ) y=y+1;
  139.   gotoxy(x,y);
  140.   break;
  141. case DOWN: 
  142.   y=y+1;
  143.   if(y >= MAPSIZE ) y=y-1;
  144.   gotoxy(x,y);
  145.   break;
  146. case ENTER:
  147.   if(xy[y][x] != NONE ) break;
  148.   xy[y][x]=Player;
  149.   RalDeleteTest(x,y,Player);
  150.   clearValue();
  151.   if(SefeDeleteTest(x,y,OtherPlayer)==YES){
  152.    xy[y][x]=CLEAR;
  153.   } else {
  154.    LOOP = END;
  155.   }
  156. }
  157. }
  158. }
  159. void printScreen(){
  160. system("cls");
  161. int x,y,n;
  162. char c[3];
  163. for(y=0;y<MAPSIZE;y++) {
  164. for(x=0;x<MAPSIZE;x++) {
  165.            if(xy[y][x] == PLAYER1) {
  166.    printf("○");
  167.   } else if(xy[y][x] == PLAYER2 ) {
  168.    printf("●");
  169.   } else if(xy[y][x] == NONE ) {
  170.    n=(x+16)/17+(y+16)/17*3;
  171.    c[0]=mapChar[n*2];
  172.    c[1]=mapChar[n*2+1];
  173.    c[2]=0;
  174.    if((x-3)%6==0 && (y-3)%6==0) {
  175.     strcpy(c,"×");
  176.    }       
  177.    printf("%s",c);
  178.   }
  179. }
  180. printf("\n");
  181. }
  182. gotoxy(displayX,displayLineY);
  183. printf("UP:8  DOWN:2  LEFT:4  RIGHT:6  ENTER:5")
  184. }
  185. int gamePlayCheck(int Player)
  186. {
  187. //승패를 가른다.
  188. return 1;
  189. }
  190. void InitMapData() {
  191. int x,y;
  192. for(y=0;y<MAPSIZE;y++) {
  193. for(x=0;x<MAPSIZE;x++) {
  194.   xy[y][x]=CLEAR;
  195. }
  196. }
  197. }
  198. // 커서를 나타냅니다.
  199. void SetCursorShow(void)
  200. {
  201. HANDLE hConsole;
  202. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  203. CONSOLE_CURSOR_INFO ci;
  204. GetConsoleCursorInfo( hConsole, &ci );
  205. ci.dwSize=100;
  206. ci.bVisible = TRUE;
  207. SetConsoleCursorInfo( hConsole , &ci );
  208. }
  209. void main() {
  210. int Player=PLAYER1,GamePlayFlag=PLAY;
  211. InitMapData();
  212. SetCursorShow();
  213. clearValue();
  214. printScreen();
  215. x=centerX;
  216. y=centerY;
  217. while(GamePlayFlag!=END){
  218. gotoxy(displayX,displayY);
  219. printf("%d 님이 두실 차례입니다.",Player);
  220. gotoxy(x,y);
  221. input(Player);
  222. printScreen();
  223. if(Player==PLAYER1) {
  224.   Player=PLAYER2;
  225. } else {
  226.   Player=PLAYER1;
  227. }
  228. GamePlayFlag = gamePlayCheck(Player);
  229. }
  230. gotoxy(displayX,displayY);
  231.   printf("%d님의 승리",Player);
  232. }
  233. void gotoxy(int x , int y)
  234. {
  235. COORD Pos = { x*2 , y};
  236. SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE), Pos);
  237. }
 
반응형

'Computer_IT > C++' 카테고리의 다른 글

LinkedList 자료  (0) 2006.04.06
VC++ 단축키 모음  (2) 2006.03.19
Console Codepage 설정.  (0) 2006.02.15
[펌] Visual C++ Console - 사격게임  (0) 2006.02.15
Visual C++ Console 에서 TurboC clrscr 구현  (0) 2006.02.15

반응형
출처... 잊어먹었음.
  1. #include < stdio.h >
  2. #include < time.h >
  3. #include < conio.h >
  4. #include < stdlib.h >
  5. #include < windows.h >
  6. /*
  7. * < 사격게임 >
  8. *
  9. * 호환성
  10. * 이 프로그램은 Borland C++ 5.5 에서 컴파일하고 시험되었습니다.
  11. *
  12. * 게임설명
  13. * 사격장은 각각 하나씩의 화살을 포함하고 있는 아홉개의 레인으로 만들어져 있습니다.
  14. * 프로그램이 시작되면 레인의 오른쪽에 한개의 타겟(X)이 나타납니다.
  15. * 당신의 과제는 화살들 중의 하나로 타겟을 맞추는 것 입니다.
  16. * 화살을 쏘려면 레인 번호를 누르면 됩니다.
  17. * 25개의 타겟이 나타난 후에, 게임은 끝나고, 최종점수가 출력됩니다.
  18. *
  19. * 난이도 변경하기
  20. * 난이도를 변경하려면 ShootArrow(int)함수의 두번째 for루프를 변경해서 화살의 속도를 변경하면 됩니다.
  21. * 화살이 천천히 나갈수록 맞추기까지의 시간을 소비하므로 난이도는 높아집니다.
  22. * PlayGame(void)함수의 TIME_LIMIT값을 2초로 맞추어 놓았기 때문에
  23. * 화살이 발사되는데 2초가 걸린다면 게임이 안되죠. 역시 제한시간도 마음대로 수정하세요.
  24. */
  25. #define true 1
  26. #define false 0
  27. typedef int bool;
  28. void ShowScore(int);
  29. int PlayGame(void);
  30. void EraseTarget(int);
  31. void ShootArrow(int);
  32. int ShowTarget(void);
  33. void DrawScreen(void);
  34. void gotoxy(int x, int y);
  35. void clrscr(void);
  36. int main(void) {
  37.   int score;
  38.   srand((unsigned) time(NULL));
  39.   DrawScreen();
  40.   score = PlayGame();
  41.   ShowScore(score);
  42.   return 0;
  43. }
  44. void DrawScreen(void) {
  45.   int x;
  46.   clrscr();
  47.   gotoxy(20, 11);
  48.   printf("게임을 시작하려면 아무키나 누르세요.");
  49.   getch();
  50.   clrscr();
  51.   for (x = 1; x <= 10; x++) {    // 루프는 한번에 한 개의 헤인벽과 한개의 화살을 그린다.
  52.        gotoxy(1, x * 2 + 2);
  53.        printf("-----------------------------------");
  54.        if (x < 10) {
  55.            gotoxy(1, x * 2 + 3);
  56.            printf("%d%s", x, " >>-->");
  57.        }
  58.   }
  59. }
  60. void EraseTarget(int target_position) {
  61.   gotoxy(60, target_position * 2 + 3);
  62.   printf(" ");
  63. }
  64. int PlayGame(void) {
  65.   int score = 0;
  66.   int target_position;
  67.   long start_time;
  68.   bool shot_fired;
  69.   int num;       // 값으로 저장하는 숫자키
  70.   const int TIME_LIMIT = 2// 한 타겟당 제한시간 2초
  71.   int x;
  72.   for (x = 1; x <= 20; x++) {      // 이 루프는 25개의 타겟을 부여한다.
  73.        target_position = ShowTarget();
  74.        start_time = time(NULL);    // 여기에서 시작시간이 저장된다.
  75.        shot_fired = false;
  76.        
  77.        // 제한시간과 남은 화살개수를 알려줌
  78.        gotoxy(44, 2);
  79.        printf("%s%d%s", "남은 화살: ", 21 - x, " ");
  80.        gotoxy(10, 2);
  81.        printf("%s%d%s", "한 타겟 당 시간제한 ", TIME_LIMIT, "초");
  82.        
  83.        do {        // 선수가 사격을 할 때까지 키 입력을 기다린다.
  84.            num = getch() - '0';
  85.            if (num >= 1 && num <= 9) {
  86.                ShootArrow(num);
  87.                shot_fired = true;
  88.            }
  89.        } while (!shot_fired)
  90.        
  91.        // 시간 안에(2초) 타겟을 맞추었을 때 실행된다.
  92.        if ((time(NULL) < start_time + TIME_LIMIT) && num == target_position) {
  93.            putchar('\a');
  94.            ++score;
  95.        }
  96.        EraseTarget(target_position);
  97.   }
  98.   return score;
  99. }
  100. void ShootArrow(int a) {  // 파라미터 a는 발사한 화살의 번호
  101.   int x;
  102.   long delay;
  103.   for (x = 4; x <= 60; x++) {
  104.        gotoxy(x, a * 2 + 3);   // 루프의 매 회마다 화살을 1문자씩 오른쪽으로 움직인다.
  105.        printf(">>-->");
  106.        for (delay = 0; delay < 3999999; delay++) // 이 코드로 화살의 속도조절을 한다. 시스템의 성능에 따라 다르다.
  107.            continue;
  108.        gotoxy(x, a * 2 + 3);
  109.        printf("   ");
  110.   }
  111.   gotoxy(4, a * 2 + 3);
  112.   printf(">>-->");
  113. }
  114. void ShowScore(int score) {
  115.   gotoxy(60, 20);
  116.   printf("-----------------");
  117.   gotoxy(60, 21);
  118.   printf("%s%d", " Your score is ", score);
  119.   gotoxy(60, 22);
  120.   printf("-----------------");
  121. }
  122. int ShowTarget(void) {
  123.   int p = rand() % 9 + 1;      // 이 난수는 타겟이 나타날 레인번호이다. 1 ~ 9
  124.   gotoxy(60, p * 2 + 3);
  125.   printf("X");
  126.   return p;
  127. }
  128. void gotoxy(int x, int y)
  129. {
  130. COORD pos={x,y};
  131. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  132. }
  133. void clrscr(void)
  134. {
  135. COORD Cur= {0,0 };
  136. unsigned long dwLen;
  137. FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE) , ' ', 80*25, Cur, &dwLen);
  138. }
 
반응형

'Computer_IT > C++' 카테고리의 다른 글

Console - 바둑소스  (0) 2006.02.22
Console Codepage 설정.  (0) 2006.02.15
Visual C++ Console 에서 TurboC clrscr 구현  (0) 2006.02.15
Visual C++ Console에서 TurboC gotoxy구현  (0) 2006.02.15
Compiler ...  (0) 2006.02.14