【发布时间】:2018-05-14 16:54:55
【问题描述】:
我正在尝试制作一款名为 Tic-Tac-Toe-Tomek 的游戏(井字游戏的变体)。目前,我正在制作获胜检查系统。
我使用输入“X”、“O”、“T”和“。”空间
我使用 printf 来查看这些功能是如何工作的。稍后我将删除它们。
输出将是
如果 x 获胜,最后的输出将是 'X'
如果 o 获胜,最后的输出将是 'o'
如果有draw,最后的输出将是'='
如果游戏正在进行,最后的输出将是 '?'
我的问题是函数 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 0 check 0 x=1 o=0 中输出是如何产生的
-
函数 chek 如果有 x 或 t 它将向 xwin 添加 1 或者如果有 o 或 t 将向 owin 添加 1 如果 xwin ==4 它将打印 x ,对于 owin dia2 是检查从 [0][3] dia2 开始的第二条对角线的函数是行数,检查是数字列,x xwin o 的总和是 owin
-
dia2 0 check 0 x=1 o=0 mean in function dia2 row 0 colum 0 the ckeck is 1 x and 0 o
-
它真的很复杂......我正在尝试
-
这是没用的,因为输出与我检查时发布的完全不同
标签: c function printf function-parameter