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);
}
}