   TicTacToe
  I made a text-based TicTacToe game, and I would like some C&C on it.  I also would like some advice on implementing ai.  I have some code for future ai, but I'm unsure if I'm headed in the right direction.  So without further ado, here's the code:main.h    #ifndef MAIN_H#define MAIN_H#include "game.h"#include "ai.h"#include <iostream>#include <windows.h>#include <conio.h>using namespace std;#endifmain.cpp #include "main.h"int main(){CGame *Game = new CGame;bool run = true;while (run){system("cls");cout<<"TicTacToe"<<endl<<endl;cout<<"1. Single Player"<<endl;cout<<"2. Multiplayer"<<endl;cout<<"3. About"<<endl;cout<<"4. Exit"<<endl<<endl;char input[1];cout<<"Pick a number from the menu: ";cin>>input;Game->IsDigit(input);switch(input[0]){case '1':run = Game->Main(true);break;case '2':run = Game->Main(false);break;case '3':Game->About();break;case '4':run = false;break;default:cout<<"That isn't a number on the menu.";getch();break;}}delete Game;return 0;}game.h #ifndef GAME_H#define GAME_H#include <iostream>#include <conio.h>#include <windows.h>#include <stdlib.h>#include <time.h>using namespace std;class CGame {public:CGame();void About();void IsDigit(char input[1]);bool Main(bool AI);void Init();void UpdateBoard();void GetInput(bool turn1 = false);void NextTurn();void CheckInput();void CheckTaken();bool CheckWin();private:bool AI;bool playing;char board[9];int turns;int turn;char input[1];};#endifgame.cpp #include "game.h"CGame::CGame(){srand(time(NULL));}void CGame::About() {system("cls");cout<<"TicTacToe"<<endl<<endl<<"Version: 1.0"<<endl<<"By: Magriep"<<endl<<"Contact: Magriep@hotmail.com"<<endl<<endl<<"Press any key to return the the main menu.";getch();}void CGame::IsDigit(char input[1]){while(!isdigit(input[0])){cout<<"That isn't a number, please enter a number. ";cin>>input;}}void CGame::Init(){turns = 0;for(int i = 0; i < 9; i++){int j = i + 1;itoa(j,&board[i],10);}UpdateBoard();turn = rand() % 2;GetInput(true);NextTurn();}bool CGame::Main(bool AI){CGame::AI = AI;Init();do{UpdateBoard();GetInput();NextTurn();} while(!CheckWin());UpdateBoard();if(turns == 9)cout<<"It's a tie!<<endl<<endl";if(turn == 0 && turns != 9)cout<<"O has won!"<<endl<<endl;if(turn == 1 && turns != 9)cout<<"X has won!"<<endl<<endl;cout<<"Would you like to play again (Y/N)? ";cin>>input;input[0] = toupper(input[0]);while(input[0] != 'Y' && input[0] != 'N'){cout<<"Please select Y or N. ";cin>>input;input[0] = toupper(input[0]);}switch(input[0]){case 'Y':return true;break;case 'N':return false;default:cout<<"Error in the program, closing...";exit(0);}}void CGame::UpdateBoard(){system("cls");cout<<endl<<" "<<board[0]<<" | "<<board[1]<<" | "<<board[2]<<endl<<"---+---+---"<<endl<<" "<<board[3]<<" | "<<board[4]<<" | "<<board[5]<<endl<<"---+---+---"<<endl<<" "<<board[6]<<" | "<<board[7]<<" | "<<board[8]<<endl<<endl;}void CGame::GetInput(bool turn1) {if(turn1 == true && turn == 0){cout<<"X goes first. ";cin>>input;CheckInput();board[atoi(&input[0]) - 1] = 'X';turns++;}if(turn1 == true && turn == 1){cout<<"O goes first. ";cin>>input;CheckInput();board[atoi(&input[0]) - 1] = 'O';turns++;}if(turn1 == false && turn == 0){cout<<"It's X's turn. ";cin>>input;CheckInput();board[atoi(&input[0]) - 1] = 'X';turns++;}if(turn1 == false && turn == 1){cout<<"It's O's turn. ";cin>>input;CheckInput();board[atoi(&input[0]) - 1] = 'O';turns++;}}void CGame::NextTurn(){turn ^= 1;}void CGame::CheckInput(){IsDigit(input);while(atoi(&input[0]) < 1 || atoi(&input[0]) > 9){cout<<"Pick a number that's on the board. ";cin>>input;IsDigit(input);}CheckTaken();}void CGame::CheckTaken(){while(board[atoi(&input[0]) - 1] == 'X' || board[atoi(&input[0]) - 1] == 'O'){cout<<"That space is taken, please select an open space. ";cin>>input;CheckInput();}}bool CGame::CheckWin(){// Checking for horizontal winsif(board[0] == board[1] && board[1] == board[2])return true;if(board[3] == board[4] && board[4] == board[5])return true;if(board[6] == board[7] && board[7] == board[8])return true;// Checking for verticalif(board[0] == board[3] && board[3] == board[6])return true;if(board[1] == board[4] && board[4] == board[7])return true;if(board[2] == board[5] && board[5] == board[8])return true;// Checking for diaganalif(board[0] == board[4] && board[4] == board[8])return true;if(board[6] == board[4] && board[4] == board[2])return true;if(turns == 9)return true;elsereturn false;}ai.h#ifndef AI_H#define AI_H#endif ai.cpp#include "ai.h"   [edited by - Magriep on February 13, 2004 12:41:45 AM][edited by - Magriep on February 13, 2004 12:43:25 AM]
  Wouldn''t it be easier to compile, and zip everything together then upload it? Not bad by the way.   R.I.P. Mark OsbackSolo Pa Mi GenteVG-Force;  Your code isn''t half bad. When I made my game I had an object for each player I would use when I wanted to make a move. There were two types; one that would ask the user what to do, and another one that was the AI.It works out nicely because it''s easy to let the player choose who they want to be, and you can even have computer vs computer, and there''s no extra code for any of those cases.As for AI, I found brute force searching for a win didn''t work very well. In most cases it found that assuming you play perfectly, there''s no way it can win, and so it just basically commited suicide.One method I found that worked pretty well was to brute force trying to maximize the turns it would take the opponent to win, and in the the case of a tie, try to minimise the number of turns it would take for us to win. There are some places it could make better choices, but for the most part it made a pretty good opponent.My game is here if you want to try it. ;  Hehe, here''s my (one file!) source: #include <iostream.h>#include <stdlib.h>#include <conio.h>char matrix[3][3];char go;int choiceRow;int choiceCol;int compChoice;int x;bool won;bool squareOneR1;bool squareTwoR1;bool squareThreeR1;bool squareOneR2;bool squareTwoR2;bool squareThreeR2;bool squareOneR3;bool squareTwoR3;bool squareThreeR3;void startup(void) {bool won = false;bool squareOneR1 = false;bool squareTwoR1 = false;bool squareThreeR1 = false;bool squareOneR2 = false;bool squareTwoR2 = false;bool squareThreeR2 = false;bool squareOneR3 = false;bool squareTwoR3 = false;bool squareThreeR3 = false; system("cls"); cout << "\nWelcome to TIC-TAC-TOE!" << endl; cout << "Would you like to play now? (Y/N): "; cin >> go;if (go != ''y'' && go != ''Y'') { system("cls"); cout << "\nThanks for playing...\n" << endl; system("pause"); system("exit");}}void dispMatrix(void) {//the play boxsystem("cls"); //clears DOS box firstcout<<"\n\t\t 1   2   3\n"<<endl;cout<<"\t\t   1  "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;cout<<"\t\t---|---|---\n";cout<<"\t\t   2  "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;cout<<"\t\t---|---|---\n";cout<<"\t\t   3  "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";}void askUser(void) { //ask user choice cout << "Enter the ROW: "; cin >> choiceRow; cout << "\nEnter the COLUMN: "; cin >> choiceCol;}void setChoice(void) { //sets the user''s choice into the matrix matrix[(choiceRow - 1)][(choiceCol - 1)] = ''X'';}void compMove(void) { if (won == false) {if (matrix[1][1] != ''X'' && matrix[1][1] != ''O'') { matrix[1][1] = ''O'';}else if (matrix[2][2] != ''X'' && matrix[2][2] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[2][2] != ''X'' && matrix[2][2] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[2][0] != ''X'' && matrix[2][0] != ''O'') { matrix[2][0] = ''O'';}else if (matrix[0][0] != ''X'' && matrix[0][0] != ''O'') { matrix[0][0] = ''O'';}else if (matrix[0][2] != ''X'' && matrix[0][2] != ''O'') { matrix[0][2] = ''O'';}else if (matrix[0][1] != ''X'' && matrix[0][1] != ''O'') { matrix[0][1] = ''O'';}else if (matrix[1][0] != ''X'' && matrix[1][0] != ''O'') { matrix[2][2] = ''O'';}else if (matrix[1][2] != ''X'' && matrix[1][2] != ''O'') { matrix[1][2] = ''O'';}}}void checkWin(void) { if ((matrix[0][0] == ''X'' && matrix[0][1] == ''X'' && matrix[0][2] == ''X'') ||(matrix[0][0] == ''X'' && matrix[1][1] == ''X'' && matrix[2][2] == ''X'') ||(matrix[0][0] == ''X'' && matrix[1][0] == ''X'' && matrix[2][0] == ''X'') ||(matrix[1][0] == ''X'' && matrix[1][1] == ''X'' && matrix[1][2] == ''X'') ||(matrix[2][0] == ''X'' && matrix[2][1] == ''X'' && matrix[1][2] == ''X'') ||(matrix[2][0] == ''X'' && matrix[1][1] == ''X'' && matrix[0][2] == ''X'') ||(matrix[0][1] == ''X'' && matrix[1][1] == ''X'' && matrix[2][1] == ''X'') ||(matrix[0][2] == ''X'' && matrix[1][2] == ''X'' && matrix[2][2] == ''X'')) {  cout << "\nCongratulations, you won! :)" << endl;  system("pause");  won = true;}  else if ((matrix[0][0] == ''O'' && matrix[0][1] == ''O'' && matrix[0][2] == ''O'') ||(matrix[0][0] == ''O'' && matrix[1][1] == ''O'' && matrix[2][2] == ''O'') ||(matrix[0][0] == ''O'' && matrix[1][0] == ''O'' && matrix[2][0] == ''O'') ||(matrix[1][0] == ''O'' && matrix[1][1] == ''O'' && matrix[1][2] == ''O'') ||(matrix[2][0] == ''O'' && matrix[2][1] == ''O'' && matrix[1][2] == ''O'') ||(matrix[2][0] == ''O'' && matrix[1][1] == ''O'' && matrix[0][2] == ''O'') ||(matrix[0][1] == ''O'' && matrix[1][1] == ''O'' && matrix[2][1] == ''O'') ||(matrix[0][2] == ''O'' && matrix[1][2] == ''O'' && matrix[2][2] == ''O'')) {  cout << "\nSorry, you lost! :(" << endl;  won = true;  system("pause");}  else {   cout << "\nNo Winner..." << endl;   won = false;  }}int main() { startup(); do {    dispMatrix();    askUser();    setChoice();    checkWin();    compMove();    checkWin();    dispMatrix(); } while (won == false); system("cls"); cout << "\n\tThanks for playing DSHAW Tic-Tac-Toe :)\n\n" << endl; system("pause"); system("exit");}... you can download the .exe from my server. Clicky. Note that I believe the source is a little bit outdated, but as far as I know the .exe is current. I had so many versions of that program. *sigh*  The memories.- CD   Brought to you by Code_Dark;  Ekim_Gram: Sorry, didn''t think of that.  I''ll remember to do that for future projects I post.  Could someone help explain the MinMax Algorithm? I read about it on AI Depot and I was completely lost.  If there''s something I need to learn before using something like that, please tell me so I can read about it. 