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

Thread: Tic Tac Toe

Threaded View

  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

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