C
오목 게임 프로그램
switch_user
2020. 9. 14. 23:31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <stdio.h> #define BSIZE 10 // Board size #define BLACK_STONE "○" // 바탕색이 검정이면 이게 검은색 #define WHITE_STONE "●" #define CROSS "┼" #define BLACK 1 // 배열에 저장하는 값 #define WHITE -1 int isValid(int data[BSIZE][BSIZE], int row, int col){ if (row<0 || col <0 || row>=BSIZE || col >= BSIZE) return 0; if (data[row][col] == 0) return 1; else return 0; } int getPosition(int pos[2]){ printf("행번호와 열번호를 입력하세요: "); if (scanf("%d%d", &pos[0], &pos[1]) == 2) return 1; else return 0; } void draw(int data[BSIZE][BSIZE]){ for (int c=0; c<BSIZE; c++){ printf("%2d", c); } printf("\n"); for (int r=0; r<BSIZE; r++){ printf("%1d", r); for (int c=0; c<BSIZE; c++){ if (data[r][c] == WHITE) printf(WHITE_STONE); else if (data[r][c] == BLACK) printf(BLACK_STONE); else printf(CROSS); } printf("\n"); } printf("\n"); } int count_h(int data[BSIZE][BSIZE], int row, int col){ int i,j, count=0; for (i=2;i<8;i++){ for(j=0;j<10;j++){//배열 전체 검사 if(data[j][i-2]==data[row][col]&&data[j][i-1]==data[row][col] &&data[j][i]==data[row][col]&&data[j][i+1]==data[row][col] &&data[j][i+2]==data[row][col]){//가로 검사 count++; } else if(data[i-2][j]==data[row][col]&&data[i-1][j]==data[row][col] &&data[i][j]==data[row][col]&&data[i+1][j]==data[row][col] &&data[i+2][j]==data[row][col]){//세로 검사 count++; } } } for (i=2;i<8;i++){ for(j=2;j<8;j++){ if(data[j-2][i-2]==data[row][col]&&data[j-1][i-1]==data[row][col] &&data[j][i]==data[row][col]&&data[j+1][i+1]==data[row][col] &&data[j+2][i+2]==data[row][col]){//왼쪽 위에서 오른쪽 아래로 가는 대각선 검사 count++; } else if(data[j+2][i-2]==data[row][col]&&data[j+1][i-1]==data[row][col] &&data[j][i]==data[row][col]&&data[j-1][i+1]==data[row][col] &&data[j-2][i+2]==data[row][col]){//왼쪽 아래에서 오른쪽 위로 가는 대각선 검사 count++; } } } return count; } int win(int data[BSIZE][BSIZE], int row, int col){ if (count_h(data, row, col) >= 1) {//최소 한번의 오목만 있어도 승리 return 1; } else { return 0; } } int main(){ int board[BSIZE][BSIZE] = { }; // 전부 0으로 초기화. int turn = BLACK; int pos[2]; // 0번은 행번호, 1번은 열번호 // 먼저 빈 판을 그려준다. draw(board); while (getPosition(pos)){ if (isValid(board, pos[0], pos[1])){ board[pos[0]][pos[1]] = turn; draw(board); if (win(board, pos[0], pos[1])){ if (turn == WHITE){ printf("백(%s)이 이겼습니다.\n", WHITE_STONE); } else { printf("흑(%s)이 이겼습니다.\n", BLACK_STONE); } return 0; } turn = -turn; } else { printf("놓을 수 없는 위치입니다.\n"); continue; } } return 0; } | cs |