Memory Drive

바둑 +1
반응형
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