i need help on some code im having trouble with its an x and o ai i havent finished the strategy function.

im running a linux os and i need help with a segmentation fault

also some help to make the code better would be appreciated

//dtai-II
//concept by temi coding by david
#include<iostream>// well duh, for input out put
#include<string>//for strings
#include<fstream>//for saving
#include<stdlib.h>//for exit()

using namespace std;//standard library

void checkwin(string board[9],int turn);
void of(string board[9]);
void def(string board[9]);
void strat(string board[9]);
void humanturn(string board[9],int pick);
void putb(string board[9]);

int main()//gonna return zero
{
    cout<<"1.top left 2.top middle 3.top right 4.center left 5.center middle 6.center right 7.bottom left 8.bottom middle 9.bottom right";
    int turn;//turn will always = 1 or 2 execpt when ending program
    int pick;//human pick
    string board[9];//virtual board
    board[1]="-";
    board[2]="-";
    board[3]="-";
    board[4]="-";
    board[5]="-";
    board[6]="-";
    board[7]="-";
    board[8]="-";
    board[9]="-";
    void putb(string board[9]);
    turn=1;
    if (turn=1){
        void of(string board[9]);
        void putb(string board[9]);
        void checkwin(string board[9]);
        turn=2;
        main();
    }
    else if (turn=2){
        cout<<"choose";
        void checkwin(string board[9]);
        void humanturn(string board[9],int pick);
        turn=1;
        main();
    }
    else {
        cout<<"turn = null!, quiting";
        cin.get();
        exit(0);
    }
}
//checks winner
void checkwin(string board[9],int turn)
{
    if( board[1] == "x" && board[2]=="x" && board[3]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[4]=="x" && board[5]=="x" && board[6]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[7]=="x" && board[8]=="x" && board[9]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[1]=="x" && board[4]=="x" && board[7]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[2]=="x" && board[5]=="x" && board[8]=="x"){
        cout<<"computer wins!";
    }
    else if (board[3]=="x" && board[6]=="x" && board[9]=="x"){
        cout<< "computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[1]=="x" && board[5]=="x" && board[9]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if (board[3]=="x" && board[5]=="x" && board[7]=="x"){
        cout<<"computer wins!";
        cin.get();
        turn=3;
    }
    else if( board[1]== "o" && board[2]=="o" && board[3]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[4]=="o" && board[5]=="o" && board[6]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[7]=="o" && board[8]=="o"&& board[9]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[1]=="o" && board[4]=="o" && board[7]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[2]=="o" && board[5]=="o" && board[8]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[3]=="o" && board[6]=="o" && board[9]=="o"){
        cout<< "human wins!";
        cin.get();
        turn=3;
    }
    else if (board[1]=="o" && board[5]=="o" && board[9]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
    else if (board[3]=="o" && board[5]=="o" && board[7]=="o"){
        cout<<"human wins!";
        cin.get();
        turn=3;
    }
}
//draw the board much simpler with string class
void putb(string board[9])
{
    cout<<board[1]<<board[2]<<board[3]<<"\n";
    cout<<board[4]<<board[5]<<board[6]<<"\n";
    cout<<board[7]<<board[8]<<board[9]<<"\n";
}
void of(string board[9])
{
    if (board[1]=="x" && board[2] =="x" && board[3]=="-"){
        board[3]="x";
    }
    else if (board[1]=="x" && board[2]=="-" && board[3]=="x"){
        board[2]="x";
    }
    else if (board[1]=="-" && board[2]=="x" && board[3]=="x"){
        board[1]="x";
    }
    else if (board[4]=="x" && board[5]=="x" && board[6]=="-"){
        board[6]="x";

    }
    else if (board[4]=="x" && board[5]=="-" && board[6]=="x"){
        board[5]="x";
    }
    else if (board[4]=="-" && board[5]=="x" && board[6]=="x"){
        board[4]="x";
    }
    else if (board[7]=="x" && board[8]=="x" && board[9]=="-"){
        board[9]="x";
    }
    else if (board[7]=="x" && board[8]=="-" && board[9]=="x"){
        board[8]="x";
    }
    else if (board[7]=="-" && board[8]=="x" && board[9]=="x"){
        board[7]="x";
    }
    else if (board[1]=="x" && board[4]=="x" && board[7]=="-"){
        board[7]="x";
    }
    else if (board[1]=="x" && board[4]=="-" && board[7]=="x"){
        board[4]="x";
    }
    else if (board[1]=="-" && board[4]=="x" && board[7]=="x"){
        board[1]="x";
    }
    else if (board[2]=="x" && board[5]=="x" && board[8]=="-"){
        board[8]="x";
    }
    else if (board[2]=="x" && board[5]=="-" && board[8]=="x"){
        board[5]="x";
    }
    else if (board[2]=="-" && board[5]=="x" && board[8]=="x"){
        board[2]="x";
    }
    else if (board[3]=="x" && board[6]=="x" && board[9]=="-"){
        board[9]="x";
    }
    else if (board[3]=="x" && board[6]=="-" && board[9]=="x"){
        board[6]="x";
    }
    else if (board[3]=="-" && board[6]=="x" && board[9]=="x"){
        board[3]="x";
    }
    else if (board[1]=="x" && board[5]=="x" && board[9]=="-"){
        board [9]=="x";
    }
    else if (board[1]=="x" && board[5]=="-" && board[9]=="x"){
        board[5]="x";
    }
    else if (board[1]=="-" && board[5]=="x" && board[9]=="x"){
        board[1]="x";
    }
    else if (board[3]=="x" && board[5]=="x" && board[7]=="-"){
        board[7]="x";
    }
    else if (board[3]=="x" && board[5]=="-" && board[7]=="x"){
        board[5]="x";
    }
    else if (board[3]=="-" && board[5]=="x" && board[7]=="x"){
        board[3]="x";
    }
    else{
        void def(string board[9]);
    }
}
void def(string board[9])
{
    if (board[1]=="o" && board[2] =="o" && board[3]=="-"){
        board[3]="x";
    }
    else if (board[1]=="o" && board[2]=="-" && board[3]=="o"){
        board[2]="x";
    }
    else if (board[1]=="-" && board[2]=="o" && board[3]=="o"){
        board[1]="x";
    }
    else if (board[4]=="o" && board[5]=="o" && board[6]=="-"){
        board[6]="x";
    }
    else if (board[4]=="o" && board[5]=="-" && board[6]=="o"){
        board[5]="x";
    }
    else if (board[4]=="-" && board[5]=="o" && board[6]=="o"){
        board[4]="x";
    }
    else if (board[7]=="o" && board[8]=="o" && board[9]=="-"){
        board[9]="x";
    }
    else if (board[7]=="o" && board[8]=="-" && board[9]=="o"){
        board[8]="x";
    }
    else if (board[7]=="-" && board[8]=="o" && board[9]=="o"){
        board[7]="x";
    }
    else if (board[1]=="o" && board[4]=="o" && board[7]=="-"){
        board[7]="x";
    }
    else if (board[1]=="o" && board[4]=="-" && board[7]=="o"){
        board[4]="x";
    }
    else if (board[1]=="-" && board[4]=="o" && board[7]=="o"){
        board[1]="x";
    }
    else if (board[2]=="o" && board[5]=="o" && board[8]=="-"){
        board[8]="x";
    }
    else if (board[2]=="o" && board[5]=="-" && board[8]=="o"){
        board[5]="x";
    }
    else if (board[2]=="-" && board[5]=="o" && board[8]=="o"){
        board[2]="x";
    }
    else if (board[3]=="o" && board[6]=="o" && board[9]=="-"){
        board[9]="x";
    }
    else if (board[3]=="o" && board[6]=="-" && board[9]=="o"){
        board[6]="x";
    }
    else if (board[3]=="-" && board[6]=="o" && board[9]=="o"){
        board[3]="x";
    }
    else if (board[1]=="o" && board[5]=="o" && board[9]=="-"){
        board [9]=="x";
    }
    else if (board[1]=="o" && board[5]=="-" && board[9]=="o"){
        board[5]="x";
    }
    else if (board[1]=="-" && board[5]=="o" && board[9]=="o"){
        board[1]="x";
    }
    else if (board[3]=="o" && board[5]=="o" && board[7]=="-"){
        board[7]="x";
    }
    else if (board[3]=="o" && board[5]=="-" && board[7]=="o"){
        board[5]="x";
    }
    else if (board[3]=="-" && board[5]=="o" && board[7]=="o"){
        board[3]="x";
    }
    else{
        cout<<"halla back";//place holder for strat
    }
}
void humanturn(string board[9],int pick)
{
    if (pick==1 && board[1]=="-"){
        board[1]="o";
    }
    else if (pick==1 && board[1]=="x" || board[1]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==2 && board[2]=="-"){
        board[2]="o";
    }
    else if (pick==2 && board[2]=="x" || board[2]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==3 && board[3]=="-"){
        board[3]="o";
    }
    else if (pick==3 && board[3]=="x" || board[3]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==4 && board[4]=="-"){
        board[4]="o";
    }
    else if (pick==4 && board[4]=="x" || board[4]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==5 && board[5]=="-"){
        board[5]="o";
    }
    else if (pick==5 && board[5]=="x" || board[5]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==6 && board[6]=="-"){
        board[6]="o";
    }
    else if (pick==6 && board[6]=="x" || board [6]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==7 && board[7]=="-"){//wow line 345 didnt expect the program  to be that long
        board[7]="o";
    }
    else if (pick==7 && board[7]=="x" || board[7]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==8 && board[8]=="-"){
        board[8]="o";
    }
    else if (pick==8 && board[8]=="x" || board[8]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else if (pick==9 && board[9]=="-"){
        board[9]="o";
    }
    else if (pick==9 && board[9]=="x" || board[9]=="o"){
        cout<<"not a valid move";
        void humanturn(string board[9],int pick);
    }
    else{
        cout<<"not valid! what the crap are you thinking";
        void humanturn(string board[9],int pick);
    }
}

First, I'd like to say that although the effort is impressive, you are approaching this rather... crudely; there are a few things you can do to shorten your code(by A LOT). First of all, since your board is initialized to character '-', you can automatically assumed there will be nothing else on your board besides 'x', 'o', and '-.' That being said, you do not need all those and conditions, just if(board[x] = '-'). Secondly, you can encompase all your ifs with ONE if statement rather than repeating the said condition every time. Also, a switch would be preferable for this problem. Lastly, rather than manually inputing every possible move, think of other ways to determine the AI's moves(for example, calculating the sum of each row/column/diagnal to see if and where the opponent might win) While tic tac toe is by no means a "short" code, anything over 200 lines is over the top.

Oh and make use of loops to display and set your board

for(x = 0; x < 9; x++)
board[x]='-';

int a = 0;
for(y = 0; y < 3; y++) {
for(x = 0; x < 3; x++) {
    cout<<board[a]<<" ";
    a++;
}
cout<<endl;
}

first loop is for setting board, second is for displaying; isn't it much nicer?

thank you for the tips. i did create an algorithm for finding the ai's move but since it encompases alot of moves and i wanted this to be easily figuered out by new commers to c++ i didnt implement it. but i will do the board[x] it helps out alot

also could you elaborate on the if board[x]

also could you elaborate on the if board[x]

for(x = 0; x < 9; x++)
board[x]='-';

I believe SeeTheLite's point is that the above is superior to doing this:

board[0] = '-';
board[1] = '-';
board[2] = '-';
board[3] = '-';
board[4] = '-';
board[5] = '-';
board[6] = '-';
board[7] = '-';
board[8] = '-';

and there are other places where you could significantly shorten the code by using loops and using a variable (like x) as the array index rather than hard coding. Hence in your checkwin function, this code:

if( board[1] == "x" && board[2]=="x" && board[3]=="x"){
        cout<<"computer wins!";
	    cin.get();
	    turn=3;
    }
    else if (board[4]=="x" && board[5]=="x" && board[6]=="x"){
        cout<<"computer wins!";
	    cin.get();
	    turn=3;
    }
    else if (board[7]=="x" && board[8]=="x" && board[9]=="x"){
        cout<<"computer wins!";
	    cin.get();
	    turn=3;
    }

could be changed to a single if-statement inside of a loop using x as the loop counter variable.

for (int x = 1; x <= 7; x+=3)
{
    if( board[?] == "x" && board[?]=="x" && board[?]=="x"){
        cout<<"computer wins!";
	    cin.get();
	    turn=3;
    }
}

Replace the question marks above with terms based on x. Also, consider changing your string array to a char array since each string is either "-", "o", or "x" (one character long).

when using char strings how would i display the board (is it the same as did)

never mind and thank you both i just started implementing what you said plus i put in my algorithm and its going great you guys where a HUGE help

No problem, sorry I couldn't get back to you on the if statement, but I didn't see this thread on the most recent >_<

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.