Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

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);
Posted
Updated 9-Nov-17 11:56am
Comments
Jochen Arndt 8-Nov-17 9:03am    
I don't see a problem with that code.

Upon an invalid move you set the variable to false and the loop is executed again.

The logic looks correct: loop until a valid move is issued.

By the way using arrays (and loops) your code could be shorter.
 
Share this answer
 
Comments
Shubham_Kumar 8-Nov-17 9:00am    
Sir actually i have copied te portion of code so can you please help me to undesrstand the bool var used . Please
CPallini 8-Nov-17 9:50am    
See
http://www.codingmagic.com/CPPTutorials/CPPTutorial03.html
You could have just used a cSquare array and done:

if (cNextMove == cSquare[cNextMove] && cNextMove < 10) cSquare[cNextMove] = cPlayerMark;
else //[...]
 
Share this answer
 
Quote:
I want to understand the following code execution

A good way to understand how a program operate, it is to see it execute, the tool of choice is debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900