gpt4 book ai didi

c 函数在调用时不打印并跳转到下一个函数

转载 作者:行者123 更新时间:2023-11-30 20:32:24 25 4
gpt4 key购买 nike

我正在尝试制作一款名为 Tic-Tac-Toe-Tomek(Tic-Tac-Toe 的变体)的游戏。目前,我正在制作胜利检查系统。我使用输入“X”、“O”、“T”和“.”用于空间
我使用 printf 来查看函数是如何工作的。稍后我将删除它们。输出将是

如果 x 获胜,最后的输出将为“X”

如果 o 获胜,最后的输出将为“o”

如果有绘制,最后的输出将为“=”

如果游戏正在进行中,最后的输出将为“?”

<小时/>

我的问题是函数 dia2_check(game[][4]) 不起作用因为它不打印支票。当输入为

...X
..X.
.X..
X...

输出应该是

some reports 
then
dia2 0 check 3 x=1 o=0
dia2 1 check 2 x=2 o=0
dia2 2 check 1 x=3 o=0
dia2 3 check 0 x=4 o=0
then x

但我得到的是

some reports then
dia 0 check 0 x=1 o=0
.
.
dia 3 check 3 x=4 o=0
( which all is right) then
draw 0 col 0 D 0
draw 0 col 1 D 0
.
.
.
draw 3 col 3 D 4

程序跳转检查dia2并开始检查draw。她是代码

        #include<stdio.h>

/*how the program will worke
A)set functions
1-row check
2-column check
3 diagonal check
4 the other diagonal check
5 draw and game in progress check
6 call functions (which will control and check all the possibilities using the
other functions )
B)main
will take the input pass it to the functions
*/


/////////////////////////////////////////////////////////////////////////
int row_check(char game[4][4])
{/*row check*/
int xwin;
int owin;
char x='X';
char o='O';

for(int i=0; i<4; i++){

xwin=0;
owin=0;
for(int j=0; j<4; j++){
if(game[i][j]=='X'||game[i][j]=='T'){
xwin++;
}
else if (game[i][j]=='O'||game[i][j]=='T'){
owin++;
}
printf("row %d col %d x=%d o=%d\n",i,j,xwin,owin);
}//close loop J//
printf(" reset check x=%d o=%d\n",xwin,owin);
if(xwin==4||owin==4){
break;
}

}//close loop i//

if(xwin==4){// the return will be used later in call_func//
return x;
}
else if(owin==4){
return o;
};
}//close row_check//


//////////////////////////////////////////////////////////////////////////////

int col_check(char game[][4])
{/*column check*/
int xwin=0;
int owin=0;
char x='X';
char o='O';

for(int j=0; j<4; j++){
xwin=0;
owin=0;
for(int i=0; i<4; i++){
if(game[i][j]=='X'||game[i][j]=='T')
xwin++;
else if (game[i][j]=='O'||game[i][j]=='T')
owin++;
printf("col %d col %d x=%d o=%d\n",i,j,xwin,owin);
}//close loop i//
printf(" reset check x=%d o=%d\n",xwin,owin);
if(xwin==4||owin==4)
break;
}//close loop j//
if(xwin==4){// the return will be used later in call_func//
return x;
}
else if(owin==4){
return o;
};
}//close row_check//
//////////////////////////////////////////////////////////////////////////


int dia_check( char game[][4])
{//diagonal check//
int xwin=0;
int owin=0;
int j=0;
char x='X';
char o='O';


for(int i=0; i<4; i++){ //row increase by 1//

if(game[i][j]=='X' || game[i][j]=='T')
xwin++;
else if (game[i][j]=='O' || game[i][j]=='T')
owin++;

printf("dia %d check %d x=%d o=%d\n",i,j,xwin,owin);
j++;
}//close loops i//
if(xwin==4){// the return will be used later in call_func//
printf("x\n");
return x;
}
else if(owin==4){
return o;
};

}//close dia_check//
//////////////////////////////////////////////////////////////////////////

int dia2_check( char game[][4])
{//the other diagonal check//
int xwin=0;
int owin=0;
int j=3;
char x='X';
char o='O';


for(int i=0; i>0; i++){ //row increase by 1//

if(game[i][j]=='X' || game[i][j]=='T'){
xwin++;
}
else if (game[i][j]=='O' || game[i][j]=='T'){
owin++;
}
printf("dia2 %d check %d x=%d o=%d\n",i,j,xwin,owin);
j--;
}//close loops i//
if(xwin==4){// the return will be used later in call_func//
return x;
}
else if(owin==4){
return o;
};
}//close dia2_check//
//////////////////////////////////////////////////////////////////////////


int draw_check(char game[][4])
{/*draw check */


int draw=0;
char d= '=';
char p= '?'; // p for game in progress//
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(game[i][j]=='X'||game[i][j]=='T'||game[i][j]=='O'){
draw++;

}
printf("draw %d col %d D %d\n",i,j,draw);
} //close j for loop//

}//close i loop
if(draw==16){//the return will be used later in call_func//
return d;
}
else{
return p;
}
}//close draw check//
//////////////////////////////////////////////////////////////////////////////

int call_func (char game[4][4])
{//call functions //

char x ;
char o ;
char d ;
char p ;
o,x=row_check(game);

if(x=='X'){
printf("X\n");
}
else if(o=='O'){
printf("O\n");
}
else{//first else//
x,o=col_check(game);
if(x=='X'){
printf("X");
}
else if(o=='O'){
printf("O\n");
}

else{//second else//
x,o=dia_check(game);
if(x=='X'){
printf("X");
}
else if(o=='O'){
printf("O\n");
}
else{// third else//
x,o=dia2_check(game);
if(x=='X'){
printf("X");
}
else if(o=='O'){
printf("O\n");
}
else{//forth else//
p,d=draw_check(game);
if(d=='=')
printf("=\n");
else if(p=='?'&& x!='X' &&o!='O')
printf("?\n");
}//close forth else//
}//close third else//
}//close second else//
}//close first else//
}//close call_func//
//////////////////////////////////////////////////////////////////////////////

int main()
{

char game[4][4];


//input to test the array //
printf("inputs are x,o T or . \n");
for (int i=0; i<4; i++){
for(int j=0; j<4; j++){
printf("game[%d][%d]\n",i,j);
scanf(" %c",&game[i][j]);
}
};
//output array//
for (int g=0; g<4; g++){
printf("\n");
for(int h=0; h<4; h++){
printf("%c",game[g][h]);
}
};
printf("\n");


printf("[OUTPUT]\n");
call_func(game);
}//close main//

我找不到问题出在哪里。

最佳答案

我发现函数dia2_check中的错误是循环

for(int i=0; i>0; i++)

应该是

for(int i=0; i>4; i++)

因为我总是从 0 开始,所以循环总是会跳转

现在我应该删除该帖子还是不?

关于c 函数在调用时不打印并跳转到下一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47578919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com