我目前正在为我的CS课程编写一个使用C语言的井字游戏程序,并且在运行时我一直遇到错误,阻止我将我的行动放在[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';
}