CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Tic Tac Toe

  1. #1
    Join Date
    Dec 2022
    Posts
    3

    Lightbulb Tic Tac Toe

    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:

    Code:
    #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();
    	}
    }
    Last edited by 2kaud; December 23rd, 2022 at 04:40 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Tic Tac Toe

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

    Code:
    } 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

    Code:
    choice = (rand() % 8) + 1;
    gives an index of 1 - 8 so board[0] will never be used. Use:

    Code:
    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:

    Code:
    } 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.
    Last edited by 2kaud; December 23rd, 2022 at 04:45 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2022
    Posts
    3

    Re: Tic Tac Toe

    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.
    Code:
    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.

  4. #4
    Join Date
    Dec 2022
    Posts
    3

    Re: Tic Tac Toe

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

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

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Tic Tac Toe

    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 ??
    Last edited by 2kaud; December 27th, 2022 at 10:57 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Tic Tac Toe

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

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Tic Tac Toe

    but still looping
    How do you check for a draw - all elements used but no winner?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured