Advertisement

tictactoe in c++

Started by September 12, 2020 08:15 PM
1 comment, last by Tom Sloper 4 years, 3 months ago

I am making the classic tic tac toe game using c++ using vectors. I am working on the computer player doing the O's and the human player doing the X's. I am storing the X's and O's in a vector. I am comparing the columns and rows of the X's and O's in the vector and when they are equal I want the O to be drawn in another column and row not the one drawn in the X space.

int main()
{
	srand(time(NULL));
	
	vector<int> o_row;
	vector<int> o_col;
	vector<int> x_row;
	vector<int> x_col;

	TicTacToe game;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter player X row (0-2): ";
		cin >> row_x;
		cout << endl;
		cout << "Enter player X col (0-2): ";
		cin >> col_x;
		cout << endl;

		x_row.push_back(row_x);
		x_col.push_back(col_x);

		game.player_X(row_x, col_x);
		game.drawBoard();

		row_o = rand() % 2 + 0;
		col_o = rand() % 2 + 0;
	
		o_row.push_back(row_o);
		o_col.push_back(col_o);

		cout << o_row[i] << endl;
		cout << o_col[i] << endl;

		if (x_row[i] == o_row[i] && x_col[i] == o_col[i])
		{
			cout << "Enter player X row (0-2): ";
			cin >> row_x;
			cout << endl;
			cout << "Enter player X col (0-2): ";
			cin >> col_x;
			cout << endl;
		}
		
		game.player_O(row_o, col_o);
		game.drawBoard();
	}

If you're just sharing, please create a blog. No question asked; thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement