Can't figure out how to make this work.

Hi! I recently made a tic-tac-toe program in C++, and I lately thought of more optimized ways to write it, but I can't get it to work as I intend it to, heres the part of the code that I want to change:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
		// Prompt the player for a move
		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);


Instead of this wall of code, I want to implement a "while" loop to do it instead. I hope you guys get what the jest of what I'm saying here.

Note that the code above was NOT written by me, the one I have written instead uses an array for the board. Thats the only difference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		// Prompt the player for a move
		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;
			if(cNextMove < '1' ||  cNextMove > '9') {
				std::cout << "Invalid Move. Try again." << std::endl;
				bValidMove = false;
			}
			else if (cNextMove == cSquare[cNextMove - '1']) {
				cSquare[cNextMove - '1'] = cPlayerMark;
			}
		} while (!bValidMove);

	
Thanks! I altered it a bit and got it to work!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
            do  
            {
                char cNextMove;
                std::cin >> cNextMove;
                if(cNextMove < '1' ||  cNextMove > '9')
                {
                    std::cout << "Invalid Move, fool! Try again!" << std::endl;
                    bValidMove = false;
                }
                else if (cNextMove == caBoard[cNextMove - '1'] && caBoard[cNextMove - '1'] != 'X' && caBoard[cNextMove - '1'] != 'O')
                {
                   caBoard[cNextMove - '1'] = cMarker;
                   bValidMove = true;
                }

                else
                {
                    std::cout << "Invalid Move, fool! Try again!" << std::endl;
                    bValidMove = false;
                }

            } while (!bValidMove);


in case someone needs this.
Topic archived. No new replies allowed.