tic tac toe input validation

I've put together a tic tac toe game that works aesthetically, but I haven't done any of the input validation. I'm looking for advice on how to go about this. Input validation should include the following:
1. announce which player wins
2. Change game so that CPU makes moves for 'O'
3. Modify the program so that anytime the player is about to win (aka, they have 2 of 3 x's in a row, the computer will block w/ an o)

At this point, I'm thinking that a ton of if statements is the best way to code this but I feel like there's a more efficient way to go about this that I'm missing. Advice is appreciated!


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

int main ()
{
	char board [3][3] = {{'*', '*','*'},
						 {'*','*','*'}, 
						 {'*' , '*', '*'}} ;



	for (int game =0; game < 9; game++)
	{




		/*
		input validation here

		*/
		for (int i =0; i<3; i++)			// Loop displays board
		{
			cout <<endl;
			for (int x=0; x<3; x++)
				{
				cout <<board [i][x];
				}
		}

		cout <<endl<<endl<<"Enter a row then column number for X: ";		// prompts X for move 
		int row;
		int column;
		char G = 'X';
	
		cin >> row >>column;
		board [row-1][column-1] = G;

		for (int i =0; i<3; i++)										//displays board
		{
			cout <<endl;
			for (int x=0; x<3; x++)
			{
				cout <<board [i][x];
			}
		}

		cout <<endl<<endl<<"Enter a row then column number for O: ";				//prompts O for a move 
		int row0;
		int column0;
		char Q = 'O';
	
		cin >> row0 >>column0;
		board [row0-1][column0-1] = Q;



	}


	int y;
	cin >>y;
	return 0;
}
Last edited on
You should really separate your code into functions. That way it will be clearer and easier to maintain.

As for input validation, here is old code from school Tic Tac Toe project which handles player turn. It cannot be used in your program as is, but can give you a general idea what to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
void do_turn(const fieldelement player, fieldelement board[3][3])
{
    std::cout << "Your move '" << getSymbol(player) << "'" << std::endl;
    int x, y;
    while(true) {
        std::cin >> x >> y;
        if(0 < x && x < 4 && 0 < y && y < 4)
            if(board[x-1][y-1] == NONE)
                break;
        std::cout << "Wrong move, try again" << std::endl;
    }
    board[x-1][y-1] = player;
}


And this is main loop (winner checking and game handling):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    fieldelement board[3][3] = {{NONE,NONE,NONE},
                                {NONE,NONE,NONE},
                                {NONE,NONE,NONE}};
    fieldelement player = X;
    while(checkGameState(board) == NONE) {
        printBoard(board);
        do_turn(player, board);
        player = (player == X)? O : X ;
    }
    printBoard(board);
    victoryMessage(checkGameState(board));
} 
Topic archived. No new replies allowed.