### New to C++ Help needed

HI, I'm in high school and making a tic tac toe problem player vs computer. I already have player vs player can you please change it to player vs computer thanks for the help.

here is the code

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'…

int checkwin();
void board();

int main()
{
int player = 1,i,choice;
char ch='y';
char mark;
do
{
board();
player=(player%2)?1:2;

cout << "Player " << player << ", enter a number: ";
cin >> choice;

mark=(player == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = mark;
else if (choice == 2 && square[2] == '2')

square[2] = mark;
else if (choice == 3 && square[3] == '3')

square[3] = mark;
else if (choice == 4 && square[4] == '4')

square[4] = mark;
else if (choice == 5 && square[5] == '5')

square[5] = mark;
else if (choice == 6 && square[6] == '6')

square[6] = mark;
else if (choice == 7 && square[7] == '7')

square[7] = mark;
else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move, Press Enter and Try Again ";

player--;
cin.ignore();
cin.get();
}
i=checkwin();

player++;
}while(i==-1);
board();
if(i==1)

cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}

int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])

return 1;
else if (square[4] == square[5] && square[5] == square[6])

return 1;
else if (square[7] == square[8] && square[8] == square[9])

return 1;
else if (square[1] == square[4] && square[4] == square[7])

return 1;
else if (square[2] == square[5] && square[5] == square[8])

return 1;
else if (square[3] == square[6] && square[6] == square[9])

return 1;
else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;
else
return -1;

}

void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";

cout << "Player 1 (X) - Computer (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << endl;
}
Double Post: http://cplusplus.com/forum/beginner/88482/

You need to code the logic of Tic Tac Toe to simulate a computer playing like a human would. Post what you have tried...

AND USE CODE TAGS! (Select your code and press the <> button).
Last edited on
Take this :
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247`` ``````#include #include using namespace std; char square[10] = {'o','1','2','3','4','5','6','7','8','9'}; int checkwin(); void board(); #define X 1 #define Y 0 bool SetItem(int nPosition, int mark){ if(square[nPosition] != nPosition + '0' || nPosition < 1 || nPosition > 9)return false; square[nPosition] = mark;return true;} bool IsEmpty(int nPosition){return (square[nPosition] == '0' + nIndex);} bool IsPlayer(bool Player, int nPosition){return (square[nPosition] == ((Player == X) ? 'X' : 'O'));} int ThinkColumn(bool Player, int nColumn){nColumn--;nColumn *= 3;//Try calculating this expression (E.g : 1;2;3) int nItems = 0; if(IsPlayer(Player, nColumn + 1) == true)nItems++; if(IsPlayer(Player, nColumn + 2) == true)nItems++; if(IsPlayer(Player, nColumn + 3) == true)nItems++; if(nItems == 2) { if(IsEmpty(nColumn + 1) == true)return nColumn + 1; if(IsEmpty(nColumn + 2) == true)return nColumn + 2; if(IsEmpty(nColumn + 3) == true)return nColumn + 3; } return 0; } int ThinkRow(bool Player, int nRow){ int nItems = 0; if(IsPlayer(Player, nRow + 0) == true)nItems++; if(IsPlayer(Player, nRow + 3) == true)nItems++; if(IsPlayer(Player, nRow + 6) == true)nItems++; if(nItems == 2) { if(IsEmpty(nRow + 0) == true)return nRow + 0; if(IsEmpty(nRow + 3) == true)return nRow + 3; if(IsEmpty(nRow + 6) == true)return nRow + 6; } return 0; } int ThinkOther(bool Player, int nLine){ int nItems = 0; if(nLine == 1){ if(IsPlayer(Player, 1) == true)nItems++; if(IsPlayer(Player, 5) == true)nItems++; if(IsPlayer(Player, 9) == true)nItems++; } else if(nLine == 2){ if(IsPlayer(Player, 3) == true)nItems++; if(IsPlayer(Player, 5) == true)nItems++; if(IsPlayer(Player, 7) == true)nItems++; } if(nItems == 2) //There are two player pieces { if(nLine == 1){ if(IsEmpty(1) == true)return 1; if(IsEmpty(5) == true)return 5; if(IsEmpty(9) == true)return 9; } else if(nLine == 2){ if(IsEmpty(3) == true)return 3; if(IsEmpty(5) == true)return 5; if(IsEmpty(7) == true)return 7; } } return 0; } int ThinkSpecificItem(bool Player, int nIndex) { if(IsPlayer(Player, nIndex)) { if(nIndex == 2 || nIndex == 8) { if(IsEmpty(4) == true && (nIndex == 2 && IsEmpty(1) == true) || (nIndex == 8 && IsEmpty(7) == true))return 4; if(IsEmpty(6) == true && (nIndex == 2 && IsEmpty(3) == true) || (nIndex == 8 && IsEmpty(9) == true))return 6; if(IsEmpty(5) == true)return 5; } else if(nIndex == 4 || nIndex == 6) { if(IsEmpty(2) == true && (nIndex == 4 && IsEmpty(1) == true) || (nIndex == 6 && IsEmpty(3) == true))return 2; if(IsEmpty(8) == true && (nIndex == 4 && IsEmpty(7) == true) || (nIndex == 6 && IsEmpty(9) == true))return 8; if(IsEmpty(5) == true)return 5; } else if(nIndex == 1) { if(IsPlayer(Player, 5) == true && IsEmpty(3) == true && (IsEmpty(2) == true || IsEmpty(9) == true))return 3; if(IsEmpty(2) == true && IsEmpty(3) == true)return 2; //if(IsPlayer(Player, 2) && IsPlayer(Player, 3))return 4; if(IsEmpty(4) == true && IsEmpty(7) == true)return 4; if(IsEmpty(5) == true)return 5; } else if(nIndex == 3) { if(IsPlayer(Player, 5) == true && IsEmpty(1) == true && (IsEmpty(2) == true || IsEmpty(7) == true))return 1; if(IsEmpty(2) == true && IsEmpty(1) == true)return 2; if(IsEmpty(6) == true && IsEmpty(9) == true)return 6; if(IsEmpty(5) == true)return 5; } else if(nIndex == 7) { if(IsPlayer(Player, 5) == true && IsEmpty(9) == true && (IsEmpty(8) == true || IsEmpty(3) == true))return 9; if(IsEmpty(4) == true && IsEmpty(1) == true)return 4; if(IsEmpty(8) == true && IsEmpty(9) == true)return 8; if(IsEmpty(5) == true)return 5; } else if(nIndex == 9) { if(IsPlayer(Player, 5) == true && IsEmpty(7) == true && (IsEmpty(8) == true || IsEmpty(1) == true))return 7; if(IsEmpty(6) == true && IsEmpty(3) == true)return 6; if(IsEmpty(8) == true && IsEmpty(7) == true)return 8; if(IsEmpty(5) == true)return 5; } } return 0; } void Think(bool Player) { char mark = (Player == X) ? 'X' : 'O'; bool bResult; int choice; bool Enemy = !Player; ///////////////Method 1...//////////////////////////////////////////////////// if((choice = ThinkOther(Player, 1)) != 0){square[choice] = mark;return;} if((choice = ThinkOther(Player, 2)) != 0){square[choice] = mark;return;} for(int b = 1; b <= 3; b++){ if((choice = ThinkColumn(Player, b)) != 0){square[choice] = mark;return;} if((choice = ThinkRow(Player, b)) != 0){square[choice] = mark;return;}} if((choice = ThinkOther(Enemy, 1)) != 0){square[choice] = mark;return;} if((choice = ThinkOther(Enemy, 2)) != 0){square[choice] = mark;return;} ///////////////Method 2...//////////////////////////////////////////////////////// bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 4) == true); if(bResult == true && IsEmpty(1) == true){square[1] = mark;return;} bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 6) == true); if(bResult == true && IsEmpty(3) == true){square[3] = mark;return;} bResult = (IsPlayer(Player, 4) == true && IsPlayer(Player, 8) == true); if(bResult == true && IsEmpty(7) == true){square[7] = mark;return;} bResult = (IsPlayer(Player, 6) == true && IsPlayer(Player, 8) == true); if(bResult == true && IsEmpty(7) == true){square[9] = mark;return;} ///////////////Method 3...//////////////////////////////////////////////////////// for(int a = 1; a <= 3; a++){ if((choice = ThinkColumn(Enemy, a)) != 0){square[choice] = mark;return;} if((choice = ThinkRow(Enemy, a)) != 0){square[choice] = mark;return;}} ///////////////Method 4...//////////////////////////////////////////////////////// for(int c = 1; c <= 9; c++) if((choice = ThinkSpecificItem(Player, c)) != 0){square[choice] = mark;return;} ///////////////Method 5...//////////////////////////////////////////////////////// do choice = rand() % 9 + 1;while(IsEmpty(choice) == false);square[choice] = mark; } ////////////////////////////////////////////////////////////////////////////////// int main() { srand(time(NULL)); bool player = X; char mark; int i, choice; cout << "Computer first? (Press Y) : "; cin >> mark; if(mark == 'y' || mark == 'Y')player = Y; system("cls"); //////////////////// do { board(); mark = (player == X) ? 'X' : 'O'; if(player == X) { cout << "Player " << player << " ("<< mark <<"), please enter a number : "; cin >> choice; if(SetItem(choice, mark) == false){ cout<<"Invalid move "; player = !player; //mark = (player == 1) ? 'O' : 'X'; cin.ignore(); cin.get(); } } else Think(player); i = checkwin(); player = !player;}while(i == -1); board(); if(i == 1) cout<<"==>\a Player "<< ((player == X) ? 'O' : 'X') <<" win "; return 0; } ////////////////////////////////////////////////////////////////////////////////// int checkwin() { if (square[1] == square[2] && square[2] == square[3])return 1; if (square[4] == square[5] && square[5] == square[6])return 1; if (square[7] == square[8] && square[8] == square[9])return 1; if (square[1] == square[4] && square[4] == square[7])return 1; if (square[2] == square[5] && square[5] == square[8])return 1; if (square[3] == square[6] && square[6] == square[9])return 1; if (square[1] == square[5] && square[5] == square[9])return 1; if (square[3] == square[5] && square[5] == square[7])return 1; for(int i = 1;i <= 9;i++)if(IsEmpty(i) == true)return -1; board(); cout<<"==>\aGame draw\n"; system("pause"); exit(0); return 0; } void board() { system("cls"); cout << "\n\n\tTic Tac Toe\n\n"; cout << "Player 1 (X) - Computer (O)" << endl << endl << endl; cout << "____________" << endl; cout << "| | | | " << endl; cout << "|" << square[1] << " | " << square[2] << " | " << square[3] << " | " << endl; cout << "____________" << endl; cout << "| | | | " << endl; cout << "|" << square[4] << " | " << square[5] << " | " << square[6] << " | " << endl; cout << "____________" << endl; cout << "| | | | " << endl; cout << "|" << square[7] << " | " << square[8] << " | " << square[9] << " | " << endl; cout << "____________" << endl; }``````

Enjoy!
If you need help with anything just ask here...
Last edited on
Topic archived. No new replies allowed.