Help with efficiency!

I am trying to practice my skills in C++. I just created this Tic Tac Toe game from scratch and don't really know if I did it in a very effective way, especially when determining a winner. Any suggestions on how to create more efficient code are welcome!

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

void setGrid(char grid[3][3]);
int newTurn(bool p1turn, char grid[3][3]);
void refreshGrid(char grid[3][3]);
int setO(bool p1turn, char grid[3][3]);
int setX(bool p1turn, char grid[3][3]);
int checkOwin(char grid[3][3]);
void title();

int main()
{
 bool p1turn = 1;
 char grid[3][3];
 title();
 system("cls");
 setGrid(grid);
 newTurn(p1turn,grid); 
 return 0;  
}
void title()
{
     cout << "---------------------------------------------"<<endl;
     cout << "------- Tic ---------------------------------"<<endl;
     cout << "-------------------- Tac --------------------"<<endl;
     cout << "---------------------------------- Toe ------"<<endl;
     cout << "---------------------------------------------"<<endl;
     cout << "   Player 1 = 'O'          Player 2 = 'X'    "<<endl;
     getch();
     }
int checkOwin(char grid[3][3])
{
    int i, j, k, l = 0;
 //look vertically
        for(i=0;i<3;i++)
        {
         j=0;
            while(grid[i][j]=='O')
            {
            if(j==2)
            {
            system("cls");
            refreshGrid(grid);
            cout << "Player 1 wins!"<<endl;
            getch();
            return 1;
                    }
            j++;
                                  }              
                        } 
  //look horizontally
           for(k=0;k<3;k++)
        {
         l=0;
            while(grid[l][k]=='O')
            {
            if(l==2)
            {
            system("cls");
            refreshGrid(grid);
            cout << "Player 1 wins!"<<endl;
            getch();
            return 1;
                    }
            l++;
                                  }              
                        }  
                    
  //look diagonally
         if(grid[0][0] == 'O' &&  grid[1][1] == 'O' && grid[2][2] == 'O')
         {
            system("cls");
            refreshGrid(grid);
            cout << "Player 1 wins!"<<endl;
            getch();
            return 1;    
                       }
          if(grid[0][2] == 'O' &&  grid[1][1] == 'O' && grid[2][0] == 'O')
         {
            system("cls");
            refreshGrid(grid);
            cout << "Player 1 wins!"<<endl;
            getch();
            return 1;    
                       }
}
int checkXwin(char grid[3][3])
{
    int i, j, k, l = 0;
    
 //look vertically
        for(i=0;i<3;i++)
        {
         j=0;
            while(grid[i][j]=='X')
            {
            if(j==2)
            {
            system("cls");
            refreshGrid(grid);
            cout << "Player 2 wins!"<<endl;
            getch();
            return 1;
                    }
            j++;
                                  }              
                        }  
  //look horizontally
           for(k=0;k<3;k++)
        {
         l=0;
            while(grid[l][k]=='X')
            {
            if(l==2)
            {
            system("cls");
            refreshGrid(grid);
            cout << "Player 2 wins!"<<endl;
            getch();
            return 1;
                    }
            l++;
                                  }              
                        }  
 //look diagonally
         if(grid[0][0] == 'X' &&  grid[1][1] == 'X' && grid[2][2] == 'X')
         {
            system("cls");
            refreshGrid(grid);
            cout << "Player 2 wins!"<<endl;
            getch();
            return 1;    
                       }
          if(grid[0][2] == 'X' &&  grid[1][1] == 'X' && grid[2][0] == 'X')
         {
            system("cls");
            refreshGrid(grid);
            cout << "Player 2 wins!"<<endl;
            getch();
            return 1;    
                       }
}
int setO(char grid[3][3], bool p1turn)
{
 int row;
 int col;
 cout << "Player 1, please select a row: ";
 cin >> row;
 cout << "Please select a column: ";
 cin >> col;
 
 if(grid[col-1][row-1] == '-')
 {
 grid[col-1][row-1] = 'O';
 if(checkOwin(grid)==1)
 {
  return 0;
                       }
 p1turn = 0;
 newTurn(p1turn, grid);
                       }else{cout<<"Invalid move, try again"; getch(); system("cls"); refreshGrid(grid); setO(grid,p1turn);}  
}
int setX(char grid[3][3], bool p1turn)
{
 int row;
 int col;
 cout << "Player 2, please select a row: ";
 cin >> row;
 cout << "Please select a column: ";
 cin >> col;
 
 if(grid[col-1][row-1] == '-')
 {
 grid[col-1][row-1] = 'X';
  if(checkXwin(grid)==1)
 {
  return 0;
                       }
 p1turn = 1;

 newTurn(p1turn, grid);
                       }else{cout<<"Invalid move, try again"; getch(); system("cls"); refreshGrid(grid); setX(grid,p1turn);}  
}
int newTurn(bool p1turn, char grid[3][3])
{
    refreshGrid(grid);
    switch (p1turn)
    {
    case 0:
         if(setX(grid,p1turn)==1)
         {
         return 0;
                                 }
         break;
    case 1:
         if(setO(grid,p1turn)==1)
         {
          return 0;
                                 }
         break;
           }
     }
void refreshGrid(char grid[3][3])
{
     system("cls");
     int x,y;
     int row = 1;
      for(y=0;y<3;y++)
    {
                    cout << row;
         for(x=0;x<3;x++)
         {
         cout << " "<<grid[x][y]<<" ";                       
                         } 
                         cout << '\n';
                         row++;      
                    }
         cout << "  1  2  3" << endl;
     }
void setGrid(char grid[3][3])
{
     int x, y;
     int row = 1;
    for(y=0;y<3;y++)
    {
                    cout << row;
         for(x=0;x<3;x++)
         {
         grid[x][y] = '-';
         cout << " "<<grid[x][y]<<" ";                       
                         } 
                         cout << '\n';
                         row++;      
                    }
         cout << "  1  2  3"<< endl;
     
     }
Check out the allegro library.
Its a library designed specifically for efficient gaming. Its simple to learn and has a LOT of flexibility- it can be used to create any type of game quickly and smartly. It might be learn the library just through the API, so check out http://www.cppgameprogramming.com/cgi/nav.cgi?page=allegbasics
On the right menu, it guides you through the allegro library. I learnt and became efficient in the library in just an hour. One disadvantage is that allegro isn't standard- you have to separately download the library, but its worth it. To give you an example, below me I have included a Tic Tac Toe game using allegro. You might not completely understand it because your unfamiliar with the library but it gives you a general idea of the library. Enjoy.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#include <allegro.h>

BITMAP *xSprite;
BITMAP *oSprite;

int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; //This will be used to keep
                                             //track of the Xs and Os
int curSquare = 0; //This will keep track of the current square
                    //the selector is on
                                             
int turn = 1; //This will keep track of whose turn it is
              //1 Will be for X and 2 for O
              
int x = 0; //X and Y position of selector
int y = 0;

int tempX = 0; //holds temporary values used to clear selector
int tempY = 0;

void setupBoard(){ //This function will draw in the grid

    acquire_screen();
    
    line( screen, 200, 0, 200, 480, makecol( 255, 255, 255));
    line( screen, 400, 0, 400, 480, makecol( 255, 255, 255));
    line( screen, 0, 150, 680, 150, makecol( 255, 255, 255));
    line( screen, 0, 300, 680, 300, makecol( 255, 255, 255));
    
    rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));
        
    release_screen();
    
}    

void updateBoard(){ //draws in selector

    rect( screen, tempX+1, tempY+1, tempX + 199, tempY + 149, makecol( 0, 0, 0));  
    rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));  
    rest(100);
}  

void announceWinner(){ //Announces the winner

        
         if( turn == 1){
                   textout_ex( screen, font, "X Wins!!!!",  300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));  
         } else {
                  textout_ex( screen, font, "O Wins!!!!",  300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));  
         }  


}

void checkWin(){ //checks for a winner
    
    if( board[0] == turn && board[1] == turn &&  board[2] == turn){
        announceWinner();
    } else if( board[0] == turn &&  board[3] == turn  && board[6] == turn){
        announceWinner();
    } else if( board[0] == turn &&  board[4] == turn  && board[8] == turn){
        announceWinner();
    } else if( board[1] == turn &&  board[4] == turn  && board[7] == turn){
        announceWinner();
    } else if( board[2] == turn &&  board[4] == turn  && board[6] == turn){
        announceWinner();
    } else if( board[2] == turn &&  board[5] == turn  && board[8] == turn){
        announceWinner();
    } else if( board[3] == turn &&  board[4] == turn  && board[5] == turn){
        announceWinner();
    } else if( board[6] == turn &&  board[7] == turn  && board[8] == turn){
        announceWinner();
    }
    
}    

    

void drawXO(){ //draws in the X and O
   
   acquire_screen();
   
   if(turn == 1){
    draw_sprite( screen, xSprite, x, y);
    board[curSquare] = 1;
    checkWin();
    ++turn;   
  } else if( turn == 2){
    draw_sprite( screen, oSprite, x, y);
    board[curSquare] = 2;
    checkWin();
    --turn;
  }
  
  release_screen();
  
  rest(100);
  
  }      

void moveBox(){ //takes input
    
    clear_keybuf();
    tempX = x;
    tempY = y;
    
    if( key[KEY_UP] && y != 0){
    
        y -= 150;
        curSquare -=3;
        updateBoard();
            
    } else if( key[KEY_DOWN] && y != 300){
    
        y += 150;
        curSquare +=3;
        updateBoard();
            
    }  else if( key[KEY_RIGHT] && x != 400){
    
        x += 200;
        ++curSquare;
        updateBoard();
            
    } else if( key[KEY_LEFT] && x != 0){
    
        x -= 200;
        --curSquare;
        updateBoard();
            
    } else if( key[KEY_ENTER] && board[curSquare] == 0){
    
        drawXO();
            
    }          
    
}    

int main(){
 
    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    xSprite = load_bitmap( "x.bmp", NULL);
    
    oSprite = load_bitmap( "o.bmp", NULL);

    setupBoard();
    
    while( !key[KEY_ESC]){
        moveBox();    
        
    }    
    
    destroy_bitmap( xSprite);
    destroy_bitmap( oSprite);
    
    return 0;
    
}   
END_OF_MAIN();


Thanks alot, I will look into it.
Topic archived. No new replies allowed.