minseo-jung / study-c

0 stars 0 forks source link

오목 코드 #40

Open wonny25 opened 5 years ago

wonny25 commented 5 years ago

define _CRT_SECURE_NO_WARNINGS

include

include

include

define INIT_CHAR '+'

define ARR_SIZE 19

define ERR_POINT_X_RANGE -101

define ERR_POINT_Y_RANGE -102

define ERROR_POINT_ALREADY -103

void init_board(char (b)[ARR_SIZE]); void print_board(char (b)[ARR_SIZE]); int input_to_xy(char x, int y, int ret_x, int ret_y); int putdown_stone(char(b)[ARR_SIZE], int x, int y, char stone); int check_max_length(char(b)[19], int row, int col);

int main() { int count = 0; char board[ARR_SIZE][ARR_SIZE];

char b = INIT_CHAR;
char w = INIT_CHAR;

printf("Player0 stone char: ");
scanf("%c", &b);
rewind(stdin);
w = b;

while (b == w) {
    w = '\0';
    printf("Player1 stone char: ");
    scanf("%c", &w);
    rewind(stdin);
}
printf("\n");

init_board(board);
print_board(board);

int turn = 0;
char x;
int y;
int err = 0;
char curr_stone = b;
while (1) {
    if (turn == 0) {
        printf("Player0 stone(%c) at ", b);
        curr_stone = b;
    }
    else {
        printf("Player1 stone(%c) at ", w);
        curr_stone = w;
    }

    scanf("%c%d", &x, &y);
    rewind(stdin);

    // 좌표 확인
    int rx, ry;
    err = input_to_xy(x, y, &rx, &ry);
    if (err == ERR_POINT_X_RANGE) {
        printf("wrong X point\n");
        continue;
    } 
    else if (err == ERR_POINT_Y_RANGE)
    {
        printf("wrong Y point\n");
        continue;
    }

    err = putdown_stone(board, rx, ry, curr_stone);
    if (err == ERROR_POINT_ALREADY)
    {
        printf("already put down point\n");
        continue;
    }
    count++;

    if (count > 8 && check_max_length(board, ry, rx) == 1)
    {
        print_board(board);
        if (turn == 0) {
            printf("Player0 stone(%c) Win\n", curr_stone);
        }
        else {
            printf("Player1 stone(%c) Win\n", curr_stone);
        }
        break;
    }

    print_board(board);

    if (turn == 0) {
        turn = 1;
    }
    else {
        turn = 0;
    }
}

system("pause");
return 0;

}

int check_max_length(char(*b)[ARR_SIZE], int row, int col) { int cnt = 0; char stone = b[row][col];

// 가로
for (int i = col + 1; ; i++) {
    int j = row;

    if (i >= ARR_SIZE) {
        break;
    }

    if (b[j][i] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}
for (int i = col - 1; ; i--) {
    int j = row;

    if (i < 0) {
        break;
    }

    if (b[j][i] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}

if (cnt >= 4) {
    return 1;
}

// 세로
for (int i = row + 1; ; i++) {
    int j = col;

    if (i >= ARR_SIZE) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}
for (int i = row - 1; ; i--) {
    int j = col;

    if (i < 0) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}

if (cnt >= 4) {
    return 1;
}

// 7시에서 1시방향
for (int i = row - 1, j = col + 1; ; i--, j++) {

    if (i < 0 || j >= ARR_SIZE) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}
for (int i = row + 1, j = col - 1; ; i++, j--) {

    if (j < 0 || i >= ARR_SIZE) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}

if (cnt >= 4) {
    return 1;
}

// 11시에서 5시방향
for (int i = row - 1, j = col - 1; ; i--, j--) {

    if (i < 0 || j < 0) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}
for (int i = row + 1, j = col + 1; ; i++, j++) {

    if (j >= ARR_SIZE || i >= ARR_SIZE) {
        break;
    }

    if (b[i][j] == stone) {
        cnt++;
    }
    else
    {
        break;
    }
}

if (cnt >= 4) {
    return 1;
}

return 0;

}

int putdown_stone(char(*b)[ARR_SIZE], int x, int y, char stone) { if (b[y][x] != INIT_CHAR) { return ERROR_POINT_ALREADY; }

b[y][x] = stone;
return 0;

}

int input_to_xy(char x, int y, int ret_x, int ret_y) { if (x >= 65 && x <= 83) { *ret_x = x - 65; } else { // 범위 over return ERR_POINT_X_RANGE; }

if (y < 0 || y >= ARR_SIZE) {
    return ERR_POINT_Y_RANGE;
}
else {
    *ret_y = y;
}

return 0;

}

void init_board(char (*b)[ARR_SIZE]) { for (int i = 0; i < ARR_SIZE; i++) { for (int j = 0; j < ARR_SIZE; j++) { b[i][j] = INIT_CHAR; } } }

void print_board(char (*b)[ARR_SIZE]) { for (int i = 0; i < ARR_SIZE; i++) { if (i == 0) { printf(" "); for (int k = 65; k < 65 + 19; k++) { printf("%c", k); } printf("\n"); } for (int j = 0; j < ARR_SIZE; j++) { if (j == 0) printf("%2d ", i); printf("%c", b[i][j]); } printf("\n"); } }