Hey guys, I wrote a TicTacToe program at my friends house in like 10 minutes, so I am sure I can improve some things. Can someone please give me some idea's on either stuff to add or stuff I can improve on (make more efficient).

#include <iostream>
using namespace std;

void InitializeBoard(char board[][3]);
void PrintBoard(char board[][3]);
void TakeInput(char board[][3], char marker);
char ChangeMarker(char marker);
bool CheckWin(char board[][3]);
bool CheckTie(char board[][3]);

int main()
{
	char board[3][3];
	char currentMarker = 'X';
	InitializeBoard(board);
	while (true)
	{
		PrintBoard(board);
		TakeInput(board, currentMarker);
		if (CheckWin(board))
			break;
		if (CheckTie(board))
			break;
		currentMarker = ChangeMarker(currentMarker);
	}
	PrintBoard(board);
	if (CheckTie(board))
		cout << "You tied!" << endl;
	else
		cout << "Player " << currentMarker << " has won!" << endl;
	cin.ignore();
	cin.get();
	return 0;
}

void InitializeBoard(char board[][3])
{
	board[0][0] = '1';
	board[0][1] = '2';
	board[0][2] = '3';
	board[1][0] = '4';
	board[1][1] = '5';
	board[1][2] = '6';
	board[2][0] = '7';
	board[2][1] = '8';
	board[2][2] = '9';
}

void PrintBoard(char board[][3])
{
	cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
	cout << "---------" << endl;
	cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
	cout << "---------" << endl;
	cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
	cout << "---------" << endl;
}

void TakeInput(char board[][3], char marker)
{
	char number;
	cout << "Please enter what number to want: ";
	cin >> number;
	for (int row = 0; row < 3; row++)
		for (int col = 0; col < 3; col++)
			if (board[row][col] == number)
				board[row][col] = marker;
}

char ChangeMarker(char marker)
{
	if (marker == 'X')
		return 'O';
	else
		return 'X';
}

bool CheckWin(char board[][3])
{
	for (int i = 0; i < 3; i++)
	{
		if((board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X') || (board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O'))
			return true;
		if((board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') || (board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O'))
			return true;
	}
	if ((board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') || (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O'))
		return true;
	if ((board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') || (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O'))
		return true;
	return false;
}

bool CheckTie(char board[][3])
{
	for (int x = 0; x < 3; x++)
		for (int y = 0; y < 3; y++)
			if (board[x][y] != 'X' && board[x][y] != 'O')
				return false;
	return true;
}

Thanks for any feedback.

Make it more concise :

void InitializeBoard(char board[][3]){
	board[0][0] = '1';
	board[0][1] = '2';
	board[0][2] = '3';
	board[1][0] = '4';
	board[1][1] = '5';
	board[1][2] = '6';
	board[2][0] = '7';
	board[2][1] = '8';
	board[2][2] = '9';
}

is the same as

for(int r = 0;r != 3; ++r){
 for(int c = 0; c != 3; ++c){
    board[r][c] = '1' + 3*r+c;
 }
}

This code :

void TakeInput(char board[][3], char marker)
{
	char number;
	cout << "Please enter what number to want: ";
	cin >> number;
	for (int row = 0; row < 3; row++)
		for (int col = 0; col < 3; col++)
			if (board[row][col] == number)
				board[row][col] = marker;
}

is the same as :

int n = 0;
cin >> n;
int r = 0, c = 0;
if(n < 0 || n > 9) { /* error, invalid input */ }
r = n/4;
if(r == 0) c = n - 1;
else if(r == 1) c = n - 4;
else c = n - 7;
if(board[r][c] == 'X' || board[r][c] == 'O'){ /* error, invalid input ask again*/}

This code

char ChangeMarker(char marker)
{
	if (marker == 'X')
		return 'O';
	else
		return 'X';
}

I like this better :

return marker == 'X'? 'O' : 'X';

Your checkWin could use some renovation as well.

This article has been dead for over six months. Start a new discussion instead.