My Tic Tac Toe program

I made this Tic Tac Toe program it works perfectly. I just wanted some suggestions on how i could improve and how i could shrink the code and or make it faster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
using namespace std;
string board[3][3] ;
void initializeBoard()
{
    for(int i = 0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            board[i][j]= "." ;
        }
    }
}
void printBoard()
{
    for(int i = 0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout<<board[i][j]<<"\t" ;
        }
        cout<<"\n\n" ;
    }
}
void place(int x , int y , int turn)
{
    if(turn==1)
        board[x][y]="X" ;
   else
        board[x][y]="O" ;
}
bool checkLegal(int x,int y)
{
    if(x>2 || y>2 || !(board[x][y]=="."))
    {
       return true ;
    }

}
bool notwon()
{
  for(int i = 0; i < 3; i++)
        {
            if((board[i][0] + board[i][1] + board[i][2]) == "XXX" || (board[i][0] + board[i][1] + board[i][2]) == "OOO")
            {
                return false;
            }
            else if((board[0][i] + board[1][i] + board[2][i]) == "XXX" || (board[0][i] + board[1][i] + board[2][i]) == "OOO")
            {
                return false;
            }
        }
        if ((board[0][0] + board[1][1] + board[2][2]) == "XXX" || (board[0][0] + board[1][1] + board[2][2]) == "OOO")
        {
            return false;
        }
       else if((board[0][2] + board[1][1] + board[2][0]) == "XXX" || (board[0][2] + board[1][1] + board[2][0]) == "OOO")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
bool draw()
{
    for(int i = 0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(board[i][j]== ".")
                return false ;
        }
    }
    return true ;
}
int main()
{   initializeBoard() ;
    int turn = 1;
    int x ;
    int y ;
    while(1)
{   if(notwon()&&!(draw()))
   {
    printBoard();
    cout<<"Enter coord of Player "<<turn<<" :" ;
    cin>>x>>y ;
    if(checkLegal(x,y))
        continue ;
    place(x,y,turn) ;
    if(turn==1)
        {turn=2;}
    else if (turn==2)
        {turn=1;}
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ;
}
else if(draw())
{
    cout <<"Game is drawn " ;
    break ;
}
else
{        if(turn==1)
        {turn=2;}
    else if (turn==2)
        {turn=1;}
    cout<<"Player "<<turn<<" has won " ;
    break ;
}}}
Why not each space on the board equal a char instead of a string?
Since the common man playing the game is unaware of the subscripting of arrays you can make minor arrangements to make input user friendly e.g the user may want to place x at row 3 and column 3 and so he inputs 3 3 but thats in valid as the correct input should have been 2 2.

some other minor improvements just make the interface more simple & easy to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>

using namespace std;

string board[3][3] ;

void initializeBoard()
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<3;j++)
            board[i][j]= "." ;
}
void printBoard()
{
    for(int i = 0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            cout<<board[i][j]<<"\t" ;
        cout<<"\n\n" ;
    }
}
void place(int x , int y , int turn)
{
    x--,y--;
    if(turn==1)
        board[x][y]="X" ;
   else
        board[x][y]="O" ;
}
bool checkLegal(int x,int y)
{
    if(x>3 || y>3 || !(board[x-1<0?0:x-1][y-1<0?0:y-1]==".")||x<1||y<1)
       return true ;

}
bool notwon()
{
  for(int i = 0; i < 3; i++)
        {
            if((board[i][0] + board[i][1] + board[i][2]) == "XXX" || (board[i][0] + board[i][1] + board[i][2]) == "OOO")
                return false;
            else if((board[0][i] + board[1][i] + board[2][i]) == "XXX" || (board[0][i] + board[1][i] + board[2][i]) == "OOO")
                return false;
        }
        if ((board[0][0] + board[1][1] + board[2][2]) == "XXX" || (board[0][0] + board[1][1] + board[2][2]) == "OOO")
            return false;
       else if((board[0][2] + board[1][1] + board[2][0]) == "XXX" || (board[0][2] + board[1][1] + board[2][0]) == "OOO")
            return false;
        else
            return true;
    }
bool draw()
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<3;j++)
            if(board[i][j]== ".")
                return false ;
    return true ;
}
int main()
{   initializeBoard() ;
    int turn = 1;
    int x ;
    int y ;
    while(1)
{   if(notwon()&&!(draw()))
   {
    printBoard();
    cout<<"Enter pos of Player (1 to 3)  "<<turn<<" :" ;
    cin>>x>>y ;
    if(checkLegal(x,y))
    {
        cout<<"\nInvalid input. Please enter a number between 1 and 3."<<endl<<endl;
        continue ;
    }
    place(x,y,turn) ;
    if(turn==1)
        {turn=2;}
    else if (turn==2)
        {turn=1;}
    cout<<"\n";
}
else if(draw())
{
    printBoard();
    cout <<"Game is drawn " ;
    break ;
}
else
{
    printBoard();
    if(turn==1)
        {turn=2;}
    else if (turn==2)
        {turn=1;}
    cout<<"Player "<<turn<<" has won " ;
    break ;
}}}


May i please know why are you making this game? Is it a a school project or for a friend. Or is it just out of your own interest.

We may use file to add statistics to make it a bit more interesting. :)
Last edited on
checkLegal only returns true and sometimes returns nothing at all (resulting in undefined behavior.)

Your indentation and bracket placement could be more consistent (main, in particular, is something of an eyesore.)

Other than that, it looks pretty decent to me, although I didn't take the time to try it out.
@cirey

may I please know where is the problem with checkLegal.
* I have edited my previous reply.

1
2
3
4
5
bool checkLegal(int x,int y)
{
    if(x>3 || y>3 || !(board[x-1<0?0:x-1][y-1<0?0:y-1]==".")||x<1||y<1)
       return true ;
}
checkLegal seems to work fine
Im just making it for personal interest and trying to learn c++
i used string because i couldnt get adding chars work to check who won
Last edited on
Topic archived. No new replies allowed.