My first game! Tic-tac-toe!

Hello!
I am new to these forums, and I want to show you my first game - tic tac toe. I think my code is a bit complicated, it could be done easier. and could you tell me what is wrong, because sometimes, and especially when its a draw, programs just stops without showing what it is supposed to show.
Thanks for your opinions!

Here's my code:


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void game();//function for making moves
void winning();//function for showing winning screen
void losing();
void draw();

int main()
{
    srand(time(0));//seeding the random number generator
    int z;
    char sl;//selection
    for(z=0; z<=1; z++) //a loop for showing menu afer each game
    {
    cout<<"******************************************"<<endl;
    cout<<"*        T I C - T A C - T O E   !       *"<<endl;
    cout<<"******************************************"<<endl<<endl;
    cout<<"Start game? [y/n] ";
    cin>>sl;//insert selection
    if(sl=='y')
    {
        cout<<""<<endl;
        cout<<"You write X."<<endl<<endl<<endl;
        system("cls");//clear console screen
        game();//call game function
        z=0;
    }
    if(sl=='n')
    {
        cout<<"If you dont want to play..."<<endl;
        z=1; // loop ends, app closes
    }
    }

    return 0;
}

void game()
{
    char s[9];//a character array for storing X and O for each square in table
    int i=0;//loop index
    int inp,j=0,outp,op,temp2,t,w;//some variables for certain operations
    int e=9999;//loop end
    for(op=0;op<9;op++) //sets character array values
    {
        s[op]=' ';
    }

    //showing table;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;

    while(i<=e)//loop starts
    {
        //selection and AI output

        cout<<""<<endl;
        cout<<"Select your X field: "<<endl<<endl;
        cout<<"   1 2 3"<<endl<<"   4 5 6"<<endl<<"   7 8 9"<<endl;
        cin>>inp; //input
        s[inp-1]='X';//character variable will be equal to X

        //AI program
        outp=rand()%9;
        for(j=0;j<9;j++)
        {
            if((s[j]=='X' || s[j]=='O') && j==outp) //checking if generated number
                                    // is not equal to the field in which something was written
            {
                temp2=outp;
                outp=rand()%9;
                for(t=0;t<20;t++)
                {
                    if(temp2==outp) outp=rand()%9;
                    j=0;
                }
            }
        }
        s[outp]='O';//output
        // AI makes its move

        system("cls"); //clears screen

        //showing table;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;

         //checking if someone has won
        for(j=0;j<3;j++) //several situations
        {
            if((s[j]=='X' && s[j+3]=='X' && s[j+6]=='X') || (s[j]=='O' && s[j+3]=='O' && s[j+6]=='O'))
            {
                if(s[j]=='X')
                {
                    winning(); //calls function
                }
                if(s[j]=='O')
                {
                    losing();
                }
                return; //if someone has won, returns to menu (int main)
            }
        }
        for(j=0;j<7;j=j+3)
        {
            if((s[j]=='X' && s[j+1]=='X' && s[j+2]=='X') || (s[j]=='O' && s[j+1]=='O' && s[j+2]=='O'))
            {
                if(s[j]=='X')
                {
                    winning();
                }
                if(s[j]=='O')
                {
                    losing();
                }
                return;
            }
        }
        if((s[0]=='X' && s[4]=='X' && s[8]=='X') || (s[0]=='O' && s[4]=='O' && s[8]=='O'))
        {
            if(s[0]=='X')
            {
                winning();
            }
            if(s[0]=='O')
            {
                losing();
            }
            return;
        }
        if((s[6]=='X' && s[4]=='X' && s[2]=='X') || (s[6]=='O' && s[4]=='O' && s[2]=='O'))
        {
            if(s[6]=='X')
            {
                winning();
            }
            if(s[6]=='O')
            {
                losing();
            }
            return;
        }
        //checking for draw:
        if((s[0]=='X' || s[0]=='O') && (s[1]=='X' || s[1]=='O') && (s[2]=='X' || s[2]=='O') &&
           (s[3]=='X' || s[3]=='O') && (s[4]=='X' || s[4]=='O') && (s[5]=='X' || s[5]=='O') &&
           (s[6]=='X' || s[6]=='O') && (s[7]=='X' || s[7]=='O') && (s[8]=='X' || s[8]=='O'))
        {
            draw();
            return;
        }
    }
}

void winning() //winning screen functions
{
    cout<<""<<endl<<endl;
    cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
    cout<<"*-------------------------------------------------------------*"<<endl;
    cout<<"*-                                                           -*"<<endl;
    cout<<"*-                  Y O U    W O N    ! ! !                  -*"<<endl;
    cout<<"*-                                                           -*"<<endl;
    cout<<"*-------------------------------------------------------------*"<<endl;
    cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
    cout<<""<<endl<<endl;
}

void losing()
{
    cout<<""<<endl<<endl;
    cout<<"C O M P U T E R    W O N. "<<endl;
    cout<<""<<endl<<endl;
}

void draw()
{
    cout<<""<<endl<<endl;
    cout<<" I T' S    A   D R A W ! "<<endl;
    cout<<""<<endl<<endl;
}

there is a problem with your game
you can choose a chosen choice (also the computer can do it)
You can put an X over a O and when you put another command it´s again the computer´s turn. One time I won it said computer wins
Yeah, thats a mistake, i will add "if" checking for this :) but you haven't said why app crashes when its a draw, because i can't find out.
New code! Here i corrected all the mistakes and it works how it is supposed to do. Still, AI is a bit stupid, but i will update it :)


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void game();//function for making moves
void winning();//function for showing winning screen
void losing();
void draw();

int main()
{
    srand(time(0));//seeding the random number generator
    int z;
    char sl;//selection
    for(z=0; z<=1; z++) //a loop for showing menu afer each game
    {
    cout<<"                 ******************************************"<<endl;
    cout<<"                 *          T I C - T A C - T O E  !      *"<<endl;
    cout<<"                 ******************************************"<<endl<<endl;
    cout<<"                              Start game? [y/n] ";
    cin>>sl;//insert selection
    if(sl=='y')
    {
        cout<<""<<endl;
        cout<<"You write X."<<endl<<endl<<endl;
        system("cls");//clear console screen
        game();//call game function
        z=0;
    }
    if(sl=='n')
    {
        cout<<"If you dont want to play..."<<endl;
        z=1; // loop ends, app closes
    }
    }

    return 0;
}

void game()
{
    char s[9];//a character array for storing X and O for each square in table
    int i=0;//loop index
    int inp,j=0,outp,op,temp2,t,w;//some variables for certain operations
    int e=9999;//loop end
    for(op=0;op<9;op++) //sets character array values
    {
        s[op]=' ';
    }

    //showing table;                     ;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;

    while(i<=e)//loop starts
    {
        //selection and AI output

        cout<<""<<endl;
        cout<<"Select your X field: "<<endl<<endl;
        cout<<"   1 2 3"<<endl<<"   4 5 6"<<endl<<"   7 8 9"<<endl;
        cin>>inp; //input
        w=0;
        t=w+2;
        for(w=0;w<t;w++)
        {
            if(s[inp-1]=='O' || s[inp-1]=='X')
            {
                cout<<"You have chosen the wrong square. Choose again:  ";
                cin>>inp;
                t=w+2;
            }
            else
            {
                s[inp-1]='X';//character variable will be equal to X
                w=t;
            }
        }

        //AI program
        outp=rand()%9;
        for(j=0;j<9;j++)
        {
            if(s[j]=='X' && j==outp) //checking if generated number is not equal to the field in which something was written
            {
                outp=rand()%9;
                j=0;
            }
            if(s[j]=='O' && j==outp) //checking if generated number is not equal to the field in which something was written
            {
                outp=rand()%9;
                j=0;
            }
        }
        if(s[outp]!='X' && s[outp]!='O') s[outp]='O';//output
        // AI makes its move

        system("cls"); //clears screen

        //showing table;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;
        cout<<"| "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<" |"<<endl;
        cout<<" --- --- --- "<<endl;

         //checking if someone has won
        for(j=0;j<3;j++) //several situations
        {
            if((s[j]=='X' && s[j+3]=='X' && s[j+6]=='X') || (s[j]=='O' && s[j+3]=='O' && s[j+6]=='O'))
            {
                if(s[j]=='X')
                {
                    winning(); //calls function
                }
                if(s[j]=='O')
                {
                    losing();
                }
                return; //if someone has won, returns to menu (int main)
            }
        }
        for(j=0;j<7;j=j+3)
        {
            if((s[j]=='X' && s[j+1]=='X' && s[j+2]=='X') || (s[j]=='O' && s[j+1]=='O' && s[j+2]=='O'))
            {
                if(s[j]=='X')
                {
                    winning();
                }
                if(s[j]=='O')
                {
                    losing();
                }
                return;
            }
        }
        if((s[0]=='X' && s[4]=='X' && s[8]=='X') || (s[0]=='O' && s[4]=='O' && s[8]=='O'))
        {
            if(s[0]=='X')
            {
                winning();
            }
            if(s[0]=='O')
            {
                losing();
            }
            return;
        }
        if((s[6]=='X' && s[4]=='X' && s[2]=='X') || (s[6]=='O' && s[4]=='O' && s[2]=='O'))
        {
            if(s[6]=='X')
            {
                winning();
            }
            if(s[6]=='O')
            {
                losing();
            }
            return;
        }
        //checking for draw:
        if((s[0]=='X' || s[0]=='O') && (s[1]=='X' || s[1]=='O') && (s[2]=='X' || s[2]=='O') &&
           (s[3]=='X' || s[3]=='O') && (s[4]=='X' || s[4]=='O') && (s[5]=='X' || s[5]=='O') &&
           (s[6]=='X' || s[6]=='O') && (s[7]=='X' || s[7]=='O') && (s[8]=='X' || s[8]=='O'))
        {
            draw();
            return;
        }
    }
}

void winning() //winning screen functions
{
    cout<<""<<endl<<endl;
    cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
    cout<<"*-------------------------------------------------------------*"<<endl;
    cout<<"*-                                                           -*"<<endl;
    cout<<"*-                  Y O U    W O N    ! ! !                  -*"<<endl;
    cout<<"*-                                                           -*"<<endl;
    cout<<"*-------------------------------------------------------------*"<<endl;
    cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
    cout<<""<<endl<<endl;
}

void losing()
{
    cout<<""<<endl<<endl;
    cout<<"C O M P U T E R    W O N. "<<endl;
    cout<<""<<endl<<endl;
}

void draw()
{
    cout<<""<<endl<<endl;
    cout<<" I T' S    A   D R A W ! "<<endl;
    cout<<""<<endl<<endl;
}

Haven't tested it; but I have an observation:

The function void game() is way too long. Consider refactoring into more manageable, smaller functions.

For instance, showing the table could be in one function, handling user input in another, the AI part in a third function, and check for win/draw in a fourth.
Topic archived. No new replies allowed.