www.pudn.com > tictactoewin.zip > tictactoewin.cpp


#include   
#include   
#include   
using namespace std;  
 
int map[4][4];                             // the array for holding the tic tac toe game  
int computer_win=-1;                       // 0 -- computer win, 1 -- computer lose, 2-- Draw  
 
void show_game(int computer) {  
        int i,j;  
        int com=computer;                  // 0 -- for using O ,   1 -- for using X  
        for (i=1;i<=3;i++) {  
            for (j=1;j<=3;j++) {  
                cout<<" ";  
                if ((map[i][j]==1) && (com==0)) cout<<"O";  
                else if ((map[i][j]==1) && (com==1)) cout<<"X";  
                else if ((map[i][j]==5) && (com==0)) cout<<"X";  
                else if ((map[i][j]==5) && (com==1)) cout<<"O";  
                else cout<<" ";  
                if (j==3)  
                    cout<>user_choose;  
                } while ( user_choose!='X' && user_choose!='O');  
 
                if (user_choose=='X')  
                        computer=0;     // computer use "O"  
                else  
                        computer=1;     // computer use "X"  
 
                // computer First ??  0 -- computer first  
                computer_term=rand()%2;  
                computer_first=computer_term;  
 
                if (computer_first==1)  
                        cout<<"Computer moves first."<>xin>>yin;  
                                while ( map[xin][yin]!=0 || xin>3 ||  
                                        xin<1 || yin>3 || yin<1){// check for valid move  
                                        cout<<"Illegal move! Re-enter."<>xin>>yin; // row column  
                                }  
                                number_of_user_move++;  
                                map[xin][yin]=5; //assign choice to map  
                                computer_term=1; // set to computer term  
                        }  
                        check_win(); // check for user win  
                        check_draw(); // check for draw  
                        show_game(computer);  
                } while (computer_win==-1);  
                if (computer_win==0)  
                        cout<<"Computer win!!!"<>quit_or_not;  
 
        } while ( quit_or_not=='C' || quit_or_not=='c' );  
 
                return 1;  
}  
 
//--------------------------------------------------------------------------