我无法修改函数中的变量,所以当它 return 到 main 时,永远不会满足 while 的条件
I can't modify a variable in a function so when it return to the main the condition of while is never satisfied
基本上我想要变量 'End' 来控制 while 循环。在 while 循环中,有一个函数被调用来检查多个事物(是井字游戏)当函数看到三元组或 table 满(平局)时,它应该将变量 'End' 更改为 - 1 如果有人赢了或者 -2 如果是平局所以游戏应该结束。 (这不会发生)
printf("TURN %u\n", COUNT); //conta turno (inglese)
printf("Player %d:\n", Start_Num); //giocata player1
printf("ROW: ");
scanf("%u", &ROW);
if (ROW > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)"); //decisione mossa
scanf("%u", &ROW);
}
printf("COLUMN: ");
scanf("%u", &COLUMN);
if (COLUMN > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)");
scanf("%u", &COLUMN);
}
if (Start_Num == 1){ //assegnazione mossa all'array
TABLE[ROW][COLUMN] = P1_SIGN;
}
else {
TABLE[ROW][COLUMN] = P2_SIGN;
}
Print_Table_Full(TABLE); //funzioni
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
COUNT++;
}
while 循环不完整,但它应该可以正常工作
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P1_SIGN){ //seconda riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P1_SIGN){ //terza riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P1_SIGN){ //prima colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P1_SIGN){ //seconda colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P1_SIGN){ //terza colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P1_SIGN){ //diagonale 1
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P1_SIGN){ //diagonale 2
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
for(size_t i = 0; i < 3; i++){
for(size_t j = 0; j < 3; j++){
if(TABLE[i][j] == 'X' || TABLE[i][j] == 'O'){
Counter_Obj++;
}
}
}
if (Counter_Obj == 9){
puts("DRAW!");
End = -2;
}
return End;
}
我什至尝试过使用指针。变量 End 是
int End = 0;
在您的代码中,您应该更改 if 条件:
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN)
到
if((TABLE[0][0] == P1_SIGN) && (TABLE[0][1] == P1_SIGN) && (TABLE[0][2] == P1_SIGN))
因为 if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN)
被评估为
(TABLE[0][0] != 0) && (TABLE[0][1] != 0) && (TABLE[0][2] == P1_SIGN)
正如有人在评论中指出的那样,我不确定您的 if
条件是否符合您的预期。
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
例如,目前这段代码正在检查 TABLE[0][0]
和 TABLE[0][1]
是否为 (EDIT:) !=0
以及 TABLE[0][2]
是否等于P1_SIGN
。如果这是它应该的工作方式,那很好,无论哪种方式,您都应该按照评论中的建议(@MikeCAT)更改所有 if
。
另一个重要的事情是你如何使用你的函数。您要返回一个值 (End
) 但不会将其存储在任何地方,因此您应该这样做:
int result = Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
或者,如果您更喜欢使用指针:
/* function now returns void */
void Verify_Win_Or_Tie(char TABLE[][T], char P1_SIGN, char P2_SIGN, int* End, unsigned int Counter_Obj) // now End is a pointer to int
{
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
// updating pointed value
*End = -1; //valore di vincita
}
/* rest of code */
}
如何在main上使用它:
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, &End, Counter_Obj); // passing address of End
看来while循环的条件是错误的!
while(Result != -1) { //inizio gioco //condizione di vittoria o pareggio
printf("TURN %u\n", COUNT); //conta turno (inglese)
printf("Player %d:\n", Start_Num); //giocata player1
printf("ROW: ");
scanf("%u", &ROW);
if (ROW > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)"); //decisione mossa
scanf("%u", &ROW);
}
printf("COLUMN: ");
scanf("%u", &COLUMN);
if (COLUMN > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)");
scanf("%u", &COLUMN);
}
if (Start_Num == 1){ //assegnazione mossa all'array
TABLE[ROW][COLUMN] = P1_SIGN;
}
else {
TABLE[ROW][COLUMN] = P2_SIGN;
}
Print_Table_Full(TABLE); //funzioni
Result = Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
COUNT++;
}
while 条件之前是:
while (Result != -1 || Result != -2)
导致无限循环,但我仍然不知道为什么!
基本上我想要变量 'End' 来控制 while 循环。在 while 循环中,有一个函数被调用来检查多个事物(是井字游戏)当函数看到三元组或 table 满(平局)时,它应该将变量 'End' 更改为 - 1 如果有人赢了或者 -2 如果是平局所以游戏应该结束。 (这不会发生)
printf("TURN %u\n", COUNT); //conta turno (inglese)
printf("Player %d:\n", Start_Num); //giocata player1
printf("ROW: ");
scanf("%u", &ROW);
if (ROW > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)"); //decisione mossa
scanf("%u", &ROW);
}
printf("COLUMN: ");
scanf("%u", &COLUMN);
if (COLUMN > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)");
scanf("%u", &COLUMN);
}
if (Start_Num == 1){ //assegnazione mossa all'array
TABLE[ROW][COLUMN] = P1_SIGN;
}
else {
TABLE[ROW][COLUMN] = P2_SIGN;
}
Print_Table_Full(TABLE); //funzioni
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
COUNT++;
}
while 循环不完整,但它应该可以正常工作
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P1_SIGN){ //seconda riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P1_SIGN){ //terza riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P1_SIGN){ //prima colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P1_SIGN){ //seconda colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P1_SIGN){ //terza colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P1_SIGN){ //diagonale 1
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P1_SIGN){ //diagonale 2
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
for(size_t i = 0; i < 3; i++){
for(size_t j = 0; j < 3; j++){
if(TABLE[i][j] == 'X' || TABLE[i][j] == 'O'){
Counter_Obj++;
}
}
}
if (Counter_Obj == 9){
puts("DRAW!");
End = -2;
}
return End;
}
我什至尝试过使用指针。变量 End 是
int End = 0;
在您的代码中,您应该更改 if 条件:
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN)
到
if((TABLE[0][0] == P1_SIGN) && (TABLE[0][1] == P1_SIGN) && (TABLE[0][2] == P1_SIGN))
因为 if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN)
被评估为
(TABLE[0][0] != 0) && (TABLE[0][1] != 0) && (TABLE[0][2] == P1_SIGN)
正如有人在评论中指出的那样,我不确定您的 if
条件是否符合您的预期。
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
例如,目前这段代码正在检查 TABLE[0][0]
和 TABLE[0][1]
是否为 (EDIT:) !=0
以及 TABLE[0][2]
是否等于P1_SIGN
。如果这是它应该的工作方式,那很好,无论哪种方式,您都应该按照评论中的建议(@MikeCAT)更改所有 if
。
另一个重要的事情是你如何使用你的函数。您要返回一个值 (End
) 但不会将其存储在任何地方,因此您应该这样做:
int result = Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
或者,如果您更喜欢使用指针:
/* function now returns void */
void Verify_Win_Or_Tie(char TABLE[][T], char P1_SIGN, char P2_SIGN, int* End, unsigned int Counter_Obj) // now End is a pointer to int
{
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
// updating pointed value
*End = -1; //valore di vincita
}
/* rest of code */
}
如何在main上使用它:
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, &End, Counter_Obj); // passing address of End
看来while循环的条件是错误的!
while(Result != -1) { //inizio gioco //condizione di vittoria o pareggio
printf("TURN %u\n", COUNT); //conta turno (inglese)
printf("Player %d:\n", Start_Num); //giocata player1
printf("ROW: ");
scanf("%u", &ROW);
if (ROW > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)"); //decisione mossa
scanf("%u", &ROW);
}
printf("COLUMN: ");
scanf("%u", &COLUMN);
if (COLUMN > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)");
scanf("%u", &COLUMN);
}
if (Start_Num == 1){ //assegnazione mossa all'array
TABLE[ROW][COLUMN] = P1_SIGN;
}
else {
TABLE[ROW][COLUMN] = P2_SIGN;
}
Print_Table_Full(TABLE); //funzioni
Result = Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
COUNT++;
}
while 条件之前是:
while (Result != -1 || Result != -2)
导致无限循环,但我仍然不知道为什么!