   Tic Tac Toe Help!
  Hello, I've created a Tic Tac Toe clone (im newb) and i got a problem with the AI (where the comp picks its move). Can anyone show me a way to get around my problem  and how to get around something like this in the furture.-- Any Help, suggestions, advice is Appreciated// Nuclear Rabbit// May 25, 06// Tic Tac Toe#include <iostream>#include <string>#include <vector>#include <ctime>#include <algorithm>using namespace std;///////////////////////////////////////////////////////////////////////////////////////////// QUIT GAME FUNCTION///////////////////////////////////////////////////////////////////////////////////////////void quitGame(){system("PAUSE");system("CLS");    cout << "|================================|\n" << "|  |\n" << "|Thank You for Playing!|\n" << "|  |\n" << "|--------------------------------|\n" << "|  |\n" << "|    Bye.    |\n" << "|  |\n" << "|================================|\n\n";system("PAUSE");}///////////////////////////////////////////////////////////////////////////////////////////// PLAY GAME FUNCTION///////////////////////////////////////////////////////////////////////////////////////////void playGame(){bool win = false;int comp;vector<string> board;board.push_back("1");board.push_back("2");board.push_back("3");board.push_back("4");board.push_back("5");board.push_back("6");board.push_back("7");board.push_back("8");board.push_back("9");string move;while(!win){system("CLS");cout << "\n\n\n\n\n\n\t\t\t\t  " << board[0] << " | " << board[1] << " | " << board[2] << endl << "\t\t\t\t=============\n" << "\t\t\t\t  " << board[3] << " | " << board[4] << " | " << board[5] << endl << "\t\t\t\t=============\n" << "\t\t\t\t  " << board[6] << " | " << board[7] << " | " << board[8] << endl << endl << "\t\t\t\t  Your Move: ";cin >> move;srand(time(0));random_shuffle(board.begin(), board.end());cout << "O";//TESTING KEYSif(move == "1")board[0] = "X";if(move == "2")board[1] = "X";if(move == "3")board[2] = "X";if(move == "4")board[3] = "X";if(move == "5")board[4] = "X";if(move == "6")board[5] = "X";if(move == "7")board[6] = "X";if(move == "8")board[7] = "X";if(move == "9")board[8] = "X";// CHECKING IF PLAYER WONif(board[0] == "X" && board[1] == "X" && board[2] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}if(board[0] == "X" && board[4] == "X" && board[8] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}if(board[0] == "X" && board[3] == "X" && board[6] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}if(board[1] == "X" && board[4] == "X" && board[7] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}if(board[2] == "X" && board[5] == "X" && board[8] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}if(board[6] == "X" && board[7] == "X" && board[8] == "X"){cout << "\nGood Job! You Won!\n\n";win = true;quitGame();}//CHECKING IF COMPUTER WONif(board[0] == "O" && board[1] == "O" && board[2] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}if(board[0] == "O" && board[4] == "O" && board[8] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}if(board[0] == "O" && board[3] == "O" && board[6] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}if(board[1] == "O" && board[4] == "O" && board[7] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}if(board[2] == "O" && board[5] == "O" && board[8] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}if(board[6] == "O" && board[7] == "O" && board[8] == "O"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}}}///////////////////////////////////////////////////////////////////////////////////////////// MAIN FUNCTION///////////////////////////////////////////////////////////////////////////////////////////int main(){  bool menuDone = false;  while(!menuDone)  {char menu;cout << endl << endl;cout << "\t\t\t|===========================|\n" << "\t\t\t|  |\n" << "\t\t\t|    Welcome!|\n" << "\t\t\t|  |\n" << "\t\t\t|---------------------------|\n" << "\t\t\t|  |\n" << "\t\t\t|    (P) Play   (Q) Quit    |\n" << "\t\t\t|  |\n" << "\t\t\t|---------------------------|\n" << "\t\t\t|  |\n" << "\t\t\t| Game Created By |\n" << "\t\t\t|Brand0n The Great|\n" << "\t\t\t|  |\n" << "\t\t\t|===========================|\n\n" << "\t\t\t\tMenu Selection: ";cin >> menu;if(menu == 'p'){playGame();menuDone = true;}if(menu == 'q'){quitGame();menuDone = true;}if(menu != 'p' && menu != 'q'){menuDone = false;}  }return 0;}
  What exactly is the problem you are having?   ;  Heres a little list of problems i have so far:- The comp only picks one move, then you keep picking till you win- Sometimes, the comp picks a aqaure where your piece is, and puts an "O" there, and overides the "X".;  I made a few changes you might find interresting:// Nuclear Rabbit// May 25, 06// Tic Tac Toe#include <iostream>#include <string>#include <sstream>#include <vector>using namespace std;///////////////////////////////////////////////////////////////////////////////////////////// QUIT GAME FUNCTION///////////////////////////////////////////////////////////////////////////////////////////void quitGame(){system("PAUSE");system("CLS");    cout << "|================================|\n" << "|  |\n" << "|Thank You for Playing!|\n" << "|  |\n" << "|--------------------------------|\n" << "|  |\n" << "|    Bye.    |\n" << "|  |\n" << "|================================|\n\n";system("PAUSE");}///////////////////////////////////////////////////////////////////////////////////////////// COMPUTER MOVE FUNCTION///////////////////////////////////////////////////////////////////////////////////////////int computerMove(const vector<string>& board){    // simple algorithm, pick the first slot found...    for(int i=0; i<9; i++)   if(board != "X" && board != "0")  return i;    return 0;}///////////////////////////////////////////////////////////////////////////////////////////// PLAY GAME FUNCTION///////////////////////////////////////////////////////////////////////////////////////////void playGame(){    stringstream convert;bool win = false;int comp, dmove;    vector<string> board;    board.push_back("1");    board.push_back("2");    board.push_back("3");    board.push_back("4");    board.push_back("5");    board.push_back("6");    board.push_back("7");    board.push_back("8");    board.push_back("9");string move;while(!win){system("CLS");cout << "\n\n\n\n\n\n\t\t\t\t  " << board[0] << " | " << board[1] << " | " << board[2] << endl << "\t\t\t\t=============\n" << "\t\t\t\t  " << board[3] << " | " << board[4] << " | " << board[5] << endl << "\t\t\t\t=============\n" << "\t\t\t\t  " << board[6] << " | " << board[7] << " | " << board[8] << endl << endl << "\t\t\t\t  Your Move: ";cin >> move;//TESTING KEYSconvert << move;convert >> dmove;convert.clear();   if(dmove <= 9 && dmove >= 1)   {  if(board[dmove-1] != "0" && board[dmove-1] != "X") board[dmove-1] = "X";  else  { cout << "Slot is taken, try again" << endl; system("PAUSE"); continue;  }   }   // CHECKING IF PLAYER WON   if(board[0] == "X" && board[1] == "X" && board[2] == "X" ||  board[0] == "X" && board[4] == "X" && board[8] == "X" ||  board[0] == "X" && board[3] == "X" && board[6] == "X" ||  board[1] == "X" && board[4] == "X" && board[7] == "X" ||  board[2] == "X" && board[5] == "X" && board[8] == "X" ||  board[2] == "X" && board[4] == "X" && board[6] == "X" || // new  board[3] == "X" && board[4] == "X" && board[5] == "X" || // new  board[6] == "X" && board[7] == "X" && board[8] == "X")   {  cout << "\nGood Job! You Won!\n\n";  win = true;  quitGame();   }   comp = computerMove(board);   board[comp] = "0";//CHECKING IF COMPUTER WONif(board[0] == "0" && board[1] == "0" && board[2] == "0" ||  board[0] == "0" && board[4] == "0" && board[8] == "0" ||  board[0] == "0" && board[3] == "0" && board[6] == "0" ||  board[1] == "0" && board[4] == "0" && board[7] == "0" ||  board[2] == "0" && board[5] == "0" && board[8] == "0" ||  board[2] == "0" && board[4] == "0" && board[6] == "0" || // new  board[3] == "0" && board[4] == "0" && board[5] == "0" || // new  board[6] == "0" && board[7] == "0" && board[8] == "0"){cout << "\nAww! You Lost!\n\n";win = true;quitGame();}}}///////////////////////////////////////////////////////////////////////////////////////////// MAIN FUNCTION///////////////////////////////////////////////////////////////////////////////////////////int main(){  bool menuDone = false;  while(!menuDone)  {char menu;cout << endl << endl;cout << "\t\t\t|===========================|\n" << "\t\t\t|  |\n" << "\t\t\t|    Welcome!|\n" << "\t\t\t|  |\n" << "\t\t\t|---------------------------|\n" << "\t\t\t|  |\n" << "\t\t\t|    (P) Play   (Q) Quit    |\n" << "\t\t\t|  |\n" << "\t\t\t|---------------------------|\n" << "\t\t\t|  |\n" << "\t\t\t| Game Created By |\n" << "\t\t\t|Brand0n The Great|\n" << "\t\t\t|  |\n" << "\t\t\t|===========================|\n\n" << "\t\t\t\tMenu Selection: ";cin >> menu;if(menu == 'p'){playGame();menuDone = true;}else if(menu == 'q'){quitGame();menuDone = true;}  }return 0;}The computerMove function is very simple. For info about how to make an "intelligent" computer:clickyclickyedit:make sure the computer fills the board with zeros, not the letter 'O'.Here is a slightly more interresting computerMove functionint computerMove(const vector<string>& board){    int index;    while(1)    {   index = rand() % 9;   if(board[index] != "X" && board[index] != "0")  return index;    }    return 0;}Also use the srand function only once at the beginning of main,and make sure there is at least one free slot on the board before you execute the computerMove function, or it might get stuck in a forever loop.[Edited by - pulpfist on May 27, 2006 1:06:51 AM]