Я хочу понять следующее выполнение кода
Hi friends basically I am trying to write a basic tic tac toe program inspired by XOAX.NET code without AI and two player input.Now to verify whether a move is valid I am trying to put the verification test inside a loop with a boolean variable called bValidMove set to true now inside argument of while loop i put the condition !bvalidmove which means to run the loop till a player does not repeat same move and to do this I set the bool var to TRUE and if the condition above does not match the variable is set to FALSE now my question arises here If suppose the ELSE statment condition arises then var will be set to FALSE now while will check the var condition which is !validmove which means to execute till the validmove is TRUE but we have already set the var to FALSE. Please explain me the manner of execution i have put the code . Sorry for such long question kindly see the code to understand my situation correctly
Что я уже пробовал:
std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl; bool bValidMove; // Loop until we get a valid move do { char cNextMove; std::cin >> cNextMove; bValidMove = true; // Check for a valid move if (cNextMove == '1' && cSquare1 == '1') { cSquare1 = cPlayerMark; } else if (cNextMove == '2' && cSquare2 == '2') { cSquare2 = cPlayerMark; } else if (cNextMove == '3' && cSquare3 == '3') { cSquare3 = cPlayerMark; } else if (cNextMove == '4' && cSquare4 == '4') { cSquare4 = cPlayerMark; } else if (cNextMove == '5' && cSquare5 == '5') { cSquare5 = cPlayerMark; } else if (cNextMove == '6' && cSquare6 == '6') { cSquare6 = cPlayerMark; } else if (cNextMove == '7' && cSquare7 == '7') { cSquare7 = cPlayerMark; } else if (cNextMove == '8' && cSquare8 == '8') { cSquare8 = cPlayerMark; } else if (cNextMove == '9' && cSquare9 == '9') { cSquare9 = cPlayerMark; } else { std::cout << "Invalid Move. Try again." << std::endl; bValidMove = false; } } while (!bValidMove);
Jochen Arndt
Я не вижу проблемы с этим кодом.
При недопустимом перемещении вы устанавливаете переменную в false, и цикл выполняется снова.