2017-11-28 10 views
0

현재 CS 클래스에 C를 사용하여 tic-tac-toe 프로그램을 작성 중이며, 실행시 오류가 계속 발생하여 내 이동을 위치 [2,3]에 배치하지 않습니다. , [3,1], [3,2], [3,3], 본질적으로 단지 맨 위 행에 이동과 두 번째 행의 왼쪽 두 행을 놓을 수있게합니다. 나는 그것이 내 check_legal_option 기능과 관련이 있다고 가정하고 있지만 문제가 무엇인지 알 수 없다. 어떤 도움을 주시면 감사하겠습니다.배열의 특정 값을 피하는 기능

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 
#include <stdbool.h> 

void clear_table(char table[3][3]); 
void generate_player2_move(char table[3][3],int *row,int *col); 
int check_table_full(char table[3][3]); 
int check_three_in_a_row(char table[3][3]); 
void display_table(char table[3][3]); 
int check_legal_option(char table[3][3],int moveRow,int moveCol); 
void update_table(char table[3][3],int pRow,int pCol); 

int main(void){ 
    char emptyTable[3][3]; 
    int row,col,cpuRow,cpuCol,full,three,legal; 
    printf("This program plays the game of tic-tac-toe\n"); 
    clear_table(emptyTable); 
    printf("\n"); 
    full = check_table_full(emptyTable); 
    three = check_three_in_a_row(emptyTable); 

    while(three == 0 && full == 0){ 

    display_table(emptyTable); 
    printf("Player 1 enter your selection [row,col]: "); 
    scanf("%d,%d",&row,&col); 
    legal=check_legal_option(emptyTable,row,col); 
    printf("%d",legal); 
    while(legal!=1){ 
     printf("Player 1 enter your selection [row,col]: "); 
     scanf("%d,%d",&row,&col); 
     legal=check_legal_option(emptyTable,row,col); 
    } 
    update_table(emptyTable,row-1,col-1); 
    generate_player2_move(emptyTable,&cpuRow,&cpuCol); 
    full = check_table_full(emptyTable); 
    three = check_three_in_a_row(emptyTable); 
    } 
    if(three == 1){ 
    printf("Congratulations, Player 1 wins!\n"); 
    } 
    else if(three == 2){ 
    printf("Congratulations, Computer wins!\n"); 
    } 
    else{ 
    printf("Game over, no player wins.\n"); 
    } 
} 

void clear_table(char table[3][3]){ 
    for(int i=0;i<3;i++){ 
    for(int j=0;j<3;j++){ 
     table[i][j] = '_'; 
    } 
    } 
} 

void generate_player2_move(char table[3][3],int *row,int *col){ 
    srand (time(NULL)); 
    do{ 
    *row = rand()%3; 
    *col = rand()%3; 
    int legal = check_legal_option(table,*row,*col); 
    }while(!check_legal_option(table,*row,*col)); 
    table[*row][*col] = 'O'; 
} 

int check_table_full(char table[3][3]){ 
    if(table[0][0] == table[0][1] && table[0][1] == table[0][2] && table[0][2] == table[1][0] && table[1][0] == table[1][1] && table[1][1] == table[1][2] && table[1][2] 
    == table[2][0] && table[2][0] == table[2][1] && table[2][1] == table[2][2] && table[0][0] != '_' && check_three_in_a_row(table) == 0){ 
return 3; 
} 
    return 0; 
} 

int check_three_in_a_row(char table[3][3]){ 
    for(int i=0;i<3;i++){ 

    if(table[i][0] == table[i][1] && table[i][1] == table[i][2] && table[i][0] == 'X'){ 
     return 1; 
    } 
    if(table[i][0] == table[i][1] && table[i][1] == table[i][2] && table[i][0] == 'O'){ 
     return 2; 
    } 
    } 
    for(int j=0;j<3;j++){ 
    //checks if player 1 or CPU gets vertical win 
    if(table[0][j] == table[1][j] && table[1][j] == table[2][j] && 
table[0][j] == 'X'){ 
     return 1; 
    } 
    if(table[0][j] == table[1][j] && table[1][j] == table[2][j] && table[0][j] == 'O'){ 
     return 2; 
    } 
    } 
    //checks if player 1 or CPU gets diagonal win one way 
    if(table[0][0] == table[1][1] && table[1][1] == table[2][2] && table[0][0] == 'X'){ 
    return 1; 
    } 
    if(table[0][0] == table[1][1] && table[1][1] == table[2][2] && table[0][0] == 'O'){ 
    return 2; 
    } 
    //checks if player 1 or CPU gets diagonal win another way 
    if(table[0][2] == table[1][1] && table[1][1] == table[2][0] && table[0][2] == 'X'){ 
    return 1; 
    } 
    if(table[0][2] == table[1][1] && table[1][1] == table[2][0] && table[0][2] == 'O'){ 
    return 2; 
    } 
    //if no one has three in a row, return 0 
    return 0; 
} 

void display_table(char table[3][3]){ 
    printf("The current state of the game is:"); 
    for(int i=0;i<3;i++){ 
    printf("\n"); 
    for(int j=0;j<3;j++){ 
     printf("%c ",table[i][j]); 
    } 
    } 
    printf("\n"); 
} 

int check_legal_option(char table[3][3],int moveRow,int moveCol){ 
    if(table[moveCol][moveCol]=='_'){ 
    return 1; 
    } 
    return 0; 
} 

void update_table(char table[3][3],int pRow,int pCol){ 
    table[pRow][pCol] = 'X'; 
} 
+2

하나의 명백한 문제는 입력 행과 열이 범위 내에 있는지 확인하지 않는 것입니다. 합법적 인 값은 0-2처럼 보이지만 다른 사람이 3 이상 또는 음수를 입력하면 범위를 벗어난 색인을 생성합니다. –

+0

OT :'srand'는 한 번만 호출해야합니다. 일반적으로'srand' 호출은'main'의 시작 부분에 있습니다. – user3386109

답변

1

check_legal 함수는 열과 행 모두에서 movecol을 사용합니다.

관련 문제