# Visual C++ & C++ Programming > C++  (Non Visual C++ Issues) >  Tic Tac Toe

## ManiaKsLV

Hello,

I try to figure out where i made mistake with computer player.

void computerZ() goes in infinity loop. PvP works fine but computer part don't. Can someone with experiece explain me what i do wrong?

Code below:



```
#include <iostream>
#include <ctime>
using namespace std;
//Izveidoju masivu ar 9 laukumiem un aizpildiju ar tuksam vertibam izmantoju char lai varu attelot X un O
char board[9] = { '1','2','3','4','5','6','7','8','9' };

//Izveidojam funkcijas kuras tiks izsauktas
void showBoard();
void resetBoard();
void playerX();
void playerO();
void computerZ();
void modePvp();
void modeCvp();
int checkBoard(char sym);
char checkWin();
void gameMode();

void gameMode() {
	int mode;
	cout << "Izveleties speles rezimu:" << endl;
	cout << "Pret datoru - 1" << endl;
	cout << "Pret citu speletaju - 2" << endl;
	cin >> mode;

	switch (mode) {
		case 1:
			modeCvp();
			break;
		case 2:
			modePvp();
			break;
		default:
			cout << "Izvelaties speles rezimu:" << endl;
			break;
	}
}

void modePvp() {
	while (true) {
		system("CLS");
		showBoard();
		if (checkBoard('X') == checkBoard('O')) {
			cout << "Speletaja X gajiens." << endl;
			playerX();
		} else {
			cout << "Speletaja O gajiens." << endl;
			playerO();
		}


		char winner = checkWin();

		if (winner == 'X') {
			system("CLS");
			showBoard();
			cout << "Speletajs 'X' uzvarejis speli!" << endl;
			break;
		} else if (winner == 'O') {
			system("CLS");
			showBoard();
			cout << "Speletajs 'O' uzvarejis speli!" << endl;
			break;
		} else if (winner == 'D') {
			system("CLS");
			showBoard();
			cout << "Neizskirts!" << endl;
			break;
		}
	}
}

void modeCvp() {
	while (true) {
		system("CLS");
		showBoard();
		//Dators neveic gajienu kamer abiem ir vienads gajienu skaits
		if (checkBoard('X') == checkBoard('O')) {
			cout << "Speletaja X gajiens." << endl;
			playerX();
		} else {
		//Datora gajienu izsaucam
			void computerZ();
		}

		//Parbaudam vinnetaju
		char winner = checkWin();
		if (winner == 'X') {
			system("CLS");
			showBoard();
			cout << "Speletajs X uzvarejis speli!" << endl;
			break;
		} else if (winner == 'O') {
			system("CLS");
			showBoard();
			cout << "So speli uzvareja dators!" << endl;
			break;
		} else if (winner == 'D') {
			cout << "Neizskirts!" << endl;
			break;
		}
	}
}

char checkWin() {
//Parbaudam horizontali speles uzvaretaju
	if (board[0] == board[1] && board[1] == board[2])
		return board[0];
	if (board[3] == board[4] && board[4] == board[5])
		return board[3];
	if (board[6] == board[7] && board[7] == board[8])
		return board[6];
		//Parbaudam vertikali speles uzvaretaju
	if (board[0] == board[3] && board[3] == board[6])
		return board[0];
	if (board[1] == board[4] && board[4] == board[7])
		return board[1];
	if (board[2] == board[5] && board[5] == board[8])
		return board[2];
		//Parbaudam diagonales speles uzvaretaju
	if (board[0] == board[4] && board[4] == board[8])
		return board[0];
	if (board[2] == board[4] && board[4] == board[6])
		return board[2];

		//Parbauda gajienu kopskaitu vai nav aviarak par speles laukumu
	if (checkBoard('X') + checkBoard('O') < 9)
	//Atgriez turpinat
		return 'C';
	else
	//Atgriez neizskirts
		return 'D';
}

void computerZ() {

	srand(time(0));
	int choice;
	do {
		choice = (rand() % 8) + 1;
		cout << choice << endl;
	//} while (board[choice] != ' ');
	} while (board[choice] != 'X' && board[choice] != 'O');
	board[choice] = 'O';
}

void playerX() {
	while (true) {
		cout << "Speletajs X izvelas poziciju no (1-9): " << endl;
		int choice;
		cin >> choice;
		//Masivs sakas no 0-8 tie ir 9 simboli tapēc i--
		choice--;
		//Parbaude vai ievade ir pareizaja diapazona
		if (choice < 0 || choice > 8) {
			cout << "Izvelies skaitli no (1-9)" << endl;
		}
		//Parbaudam izvele vai ir briva
		else if (board[choice] == 'O') {
			cout << "Pozicija ir aiznemta! Izvelies citu poziciju no (1-9)" << endl;
		}
		//Parbaudam izvele vai ir briva
		else if (board[choice] == 'X') {
			cout << "Pozicija ir aiznemta! Izvelies citu poziciju no (1-9)" << endl;
		} else {
			board[choice] = 'X';
			break;
		}
	}
}

void playerO() {
	while (true) {
		cout << "Speletajs O izvelas poziciju no (1-9): " << endl;
		int choice;
		cin >> choice;
		//Masivs sakas no 0-8 tie ir 9 simboli tapec i--
		choice--;
		//Parbaude vai ievade ir pareizaja diapazona
		if (choice < 0 || choice > 8) {
			cout << "Izvelies skaitli no (1-9)" << endl;
		}
		//Parbaudam izvele vai ir briva
		else if (board[choice] == 'X') {
			cout << "Pozicija ir aiznemta! Izvelies citu poziciju no (1-9)" << endl;
		}
		//Parbaudam izvele vai ir briva
		else if (board[choice] == 'O') {
			cout << "Pozicija ir aiznemta! Izvelies citu poziciju no (1-9)" << endl;
		} else {
			board[choice] = 'O';
			break;
		}
	}
}

int checkBoard(char sym) {
//Skaitisim cik simbolu jau ievietots laukaa
	int total = 0;
	for (int i = 0; i < 9; i++) {
		if (board[i] == sym)
			total += 1;
	}
	return total;
};

void showBoard() {
//Sazimesim grafiski laukumu
	cout << " -----------" << endl;
	cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
	cout << " -----------" << endl;
	cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
	cout << " -----------" << endl;
	cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
	cout << " -----------" << endl;
}

void resetBoard() {
//Atjauno masiva sakotnejas vertibas, lai sakot jaunu speli nav pedejas speles vertibas
	board[0] = '1';
	board[1] = '2';
	board[2] = '3';
	board[3] = '4';
	board[4] = '5';
	board[5] = '6';
	board[6] = '7';
	board[7] = '8';
	board[8] = '9';
}

int main() {

	gameMode();
	char choice = 'n';
	cout << "Velaties spelet vel? (y/n)";
	cin >> choice;
	if (choice == 'Y' || choice == 'y') {
		resetBoard();
		//system("CLS"); atjauno consoli lai neveidojas saraksts.
		system("CLS");
		main();
	}
}
```

----------


## 2kaud

Please use code tags when posting code so that the code is readable.



```
} while (board[choice] != ' ');
```

What happens if no element of board is a ' ' ? You'll get an infinite loop.

Also, srand() is only needed once at the beginning of main().

board is indexed 0 - 8 but



```
choice = (rand() % 8) + 1;
```

gives an index of 1 - 8 so board[0] will never be used. Use:



```
choice = rand() % 9;
```

But I think the main problem is that board is initialised with '1' to '9' and not ' '. Shouldn't the check be:



```
} while (board[choice] != 'X' && board[choice] != 'O');
```

but again - what about if there are no board elements that aren't already an X or an O ??

PS. In main(), you're recursively calling main(). This is not allowed.  main() can't call main(). Use a loop instead.

----------


## ManiaKsLV

Thanks for advice with tags for code.

PS. In main(), you're recursively calling main(). This is not allowed. main() can't call main(). Use a loop instead.

this should work.


```
int main() {

	char choice = 'n';
	do  {
		gameMode();
		resetBoard();
		cout << "Velaties spelet vel? (y/n)";
		cin >> choice;
		system("CLS");

	} while (choice == 'Y' || choice == 'y');
}
```


i think this is problem. At start array board[9] were empty. And then i added numbers for easy play. No 0 and 1 typing etc.

----------


## ManiaKsLV

founded  mistake but still looping... void computerZ(); should be without void  :Sick: 



```
void modeCvp() {
	while (true) {
		system("CLS");
		showBoard();
		if (checkBoard('X') == checkBoard('O')) {
			cout << "Speletaja X gajiens." << endl;
			playerX();
		} else {
			void computerZ();
		}
```

----------


## 2kaud

> void computerZ();


Yes - This is a function declaration. It doesn't call the function. As you said, you need to remove void here. Then the function computerZ will be executed.

What does 'Speletaja X gajiens.' mean in English - as Goggle translate doesn't translate! What are you trying to do here as at any point in the game the number of X can equal the number of O ??

----------


## GCDEF

If the code in the OP is still current, board[choice]  is never getting set in your loop.

----------


## 2kaud

> but still looping


How do you check for a draw - all elements used but no winner?

----------

