Game problem - Tic tac toe with multidimensional array

hi,
Problems :
1) Is there any way so that i can use "X" and "O" char instead of 9 and 0 int.??
2) if player - 1 choose field 2 . and player - 2 chooses field 2 .. how can i show error and ask for another input ?
3) is there any short coding trick for int check_result(); ?
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int row , column ;
int check_result(int board[][5])
{ if (row == 3)
    {
        if ( board[0][0] == board[0][1] && board[0][1] == board[0][2] )
        {return 1;
        else if ( board[1][0] == board[1][1] && board[1][1] == board[1][2] )
        {return 1;}
        else if ( board[2][0] == board[2][1] && board[2][1] == board[2][2] )
        {return 1;}
        else if ( board[0][0] == board[1][0] && board[1][0] == board[2][0] )
        {return 1;}
        else if ( board[0][1] == board[1][1] && board[1][1] == board[2][1] )
        {return 1;}
        else if ( board[0][2] == board[1][2] && board[1][2] == board[2][2] )
        { return 1;}
        else if ( board[0][0] == board[1][1] && board[1][1] == board[2][2] )
        { return 1; }
        else if ( board[0][2] == board[1][1] && board[1][1] == board[2][0] )
        { return 1;}
        else if ( board[0][0] != 1  && board[0][1] != 2  && board[0][2] != 3  && board[1][0] != 4  && board[1][1] != 5 &&
                  board[1][2] != 6  && board[2][0] != 7  && board[2][1] != 8  && board[2][2] != 9 )
        { return 0;}
        else { return -1 ; }
    }
    else if(row == 4)
    {
        if ( board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == board[0][3] )
        { return 1; }
        else if ( board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == board[1][3] )
        { return 1; }
        else if ( board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] == board[2][3] )
        { return 1; }
        else if ( board[3][0] == board[3][1] && board[3][1] == board[3][2] && board[3][2] == board[3][3] )
        { return 1; }
        else if ( board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] == board[3][0] )
        { return 1; }
        else if ( board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] == board[3][1] )
        { return 1; }
        else if ( board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] == board[3][2] )
        { return 1; }
        else if ( board[0][3] == board[1][3] && board[1][3] == board[2][3] && board[2][3] == board[3][3] )
        { return 1; }
        else if ( board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] )
        {  return 1; }
        else if ( board[0][3] == board[1][2] && board[1][2] == board[2][1] && board[2][1] == board[3][0] )
        {  return 1; }
        else if ( board[0][0] != 1  && board[0][1] != 2  && board[0][2] != 3  && board[0][3] != 4  &&
                  board[1][0] != 5  && board[1][1] != 6  && board[1][2] != 7  && board[1][3] != 8  &&
                  board[2][0] != 9  && board[2][1] != 10 && board[2][2] != 11 && board[2][3] != 12 &&
                  board[3][0] != 13 && board[3][1] != 14 && board[3][2] != 15 && board[3][3] != 16 )
        {
            return 0;
        }
        else{  return -1 ; }
    }
    else
    {
        if ( board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == board[0][3] && board[0][3] == board[0][4] )
        { return 1; }
        else if ( board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == board[1][3] && board[1][3] == board[1][4] )
        {return 1;}
        else if ( board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] == board[2][3] && board[2][3] == board[2][4] )
        {return 1;}
        else if ( board[3][0] == board[3][1] && board[3][1] == board[3][2] && board[3][2] == board[3][3] && board[3][3] == board[3][4] )
        {return 1;}
        else if ( board[4][0] == board[4][1] && board[4][1] == board[4][2] && board[4][2] == board[4][3] && board[4][3] == board[4][4] )
        {return 1; }
        else if ( board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] == board[3][0] && board[3][0] == board[4][0] )
        { return 1;}
        else if ( board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] == board[3][1] && board[3][1] == board[4][1] )
        { return 1;}
        else if ( board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] == board[3][2] && board[3][2] == board[4][2] )
        { return 1;}
        else if ( board[0][3] == board[1][3] && board[1][3] == board[2][3] && board[2][3] == board[3][3] && board[3][3] == board[4][3] )
        { return 1;}
        else if ( board[0][4] == board[1][4] && board[1][4] == board[2][4] && board[2][4] == board[3][4] && board[3][4] == board[4][4] )
        { return 1; }
        else if ( board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] )
        { return 1;}
        else if ( board[0][4] == board[1][3] && board[1][3] == board[2][2] && board[2][2] == board[3][1] && board[3][1] == board[4][0] )
        {return 1;}
        else if ( board[0][0] != 1  && board[0][1] != 2  && board[0][2] != 3  && board[0][3] != 4  && board[0][4] != 5 &&
                  board[1][0] != 6  && board[1][1] != 7  && board[1][2] != 8  && board[1][3] != 9  && board[1][4] != 10 &&
                  board[2][0] != 11 && board[2][1] != 12 && board[2][2] != 13 && board[2][3] != 14 && board[2][4] != 15 &&
                  board[3][0] != 16 && board[3][1] != 17 && board[3][2] != 18 && board[3][3] != 19 && board[3][4] != 20 &&
                  board[4][0] != 21 && board[4][1] != 22 && board[4][2] != 23 && board[4][3] != 24 && board[4][4] != 25 )
        { return 0;}
        else
        { return -1 ; } }
}

void display_board_update(int board[][5],int row ,int column)
{
    system("cls") ;
 cout << "    You Are Playing Tic Tac Toe Game.   \n\n\n\n\n" ;
 cout << "Player - 1 : '9' and player -2 : '0' .\n\n\n\n" ;

    for (int i = 0 ; i < row ; i++)
    {for (int j = 0 ; j < column ; j++)
        { cout << "\t" << board[i][j] ;
        }
        cout << endl ;
    }
}

void play_move(int board[][5])
{
    int player = 1 , p ;
    int choose_move ;
    int  mark ;

    do
    {
        display_board_update(board,row,column) ;
        player = (player % 2) ? 1 : 2 ;
        cout << "\n\n\n\nplayer " << player << " , please choose your move : " ;
        cin >> choose_move ;
        mark = (player % 2) ? 9 : 0 ;
        player = player++ ;

        if(row == 5 && choose_move <=25 || row == 4 && choose_move <=16 || row == 3 && choose_move <= 9)
        {
            for (int i = 0 ; i < row ;i++)
            {for (int j = 0 ; j < column ; j++)
                { while (choose_move == board[i][j])
                    {board[i][j]=mark ;
                    }
                }
            }
        }
        else
        { player-- ;
            cout << "Invalid Move.Please Press 'enter key' and choose again : " << endl;
            cin.ignore();
            cin.get();
        }
        p = check_result(board);
        if (p == 0)
        {
            display_board_update(board,row,column) ;
            cout << "\n\n\n\nResult : The Game Is Draw. \n" ;
            break ;
        }
    }while ( p != 1) ;
    if (p == 1)
    {
        display_board_update(board,row,column) ;
        cout << "\n\n\n\nResult : player " << --player << " is winner. " ;
    }
}

int main()
{
    int board[5][5] ;
    int x = 1 ;
    cout << "Enter the size of board (e.g. 4 for 4 * 4 or 5 for 5*5), max size is 5 * 5 : " ;
    cin >> row ;

    while (row <3 || row >5)
    {cout << " \n\nyour entered board size is either less than the default size of 3 * 3 or more than the size of 5 * 5. \n please enter again. :" ;
        cin >> row ;
    }
    column = row ;

    for (int i = 0 ; i < row ; i++)
    {for (int j = 0 ; j < column ; j++)
        { board[i][j] = x++  ;
            cout << "\t" << board[i][j] ;
        }
        cout << endl ;
    }
    play_move(board);
}
2) if player - 1 choose field 2 . and player - 2 chooses field 2 .. how can i show error and ask for another input ?
Check if field was already selected and display an error in this case. Wrap it all in a loop to try again.

3) is there any short coding trick for int check_result(); ?
Loops.

1) Is there any way so that i can use "X" and "O" char instead of 9 and 0 int.??
Yes, either output 'x' and 'o' instead of 0 and 9 in output function, or change your array to hold chars and use chars here.


Example of Tic-tac-toe implementation: http://cpp.sh/4tqm
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
 #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

enum field{
X = -1 ,  O , field_1=1, field_2=2, field_3 = 3, field_4 = 4, field_5 = 5, field_6 = 6, field_7 = 7, field_8 = 8, field_9 = 9,field_10=10,field_11=11, field_12=12,field_13=13,field_14=14,field_15=15,field_16=16,field_17=17,
field_18=18,field_19=19,field_20=20,field_21=21,field_22=22,field_23=23,
field_24=24,field_25=25
};

int row , column ;

int check_result(field board[][5])
{same codes as previous}
void display_board_update(field board[][5],int row ,int column)
{same as before}

void play_move(field board[][5])
{
    int player = 1 , p ;
    int choose_move ;
    field mark_field ;

    do
    {display_board_update(board,row,column) ;
        player = (player % 2) ? 1 : 2 ;
cout << "\n\n\n\nplayer " << player << " , please choose your move : " ;
cin >> choose_move ;
mark_field = (player % 2) ? X : O ;
 player++ ;

        while (choose_move != board[0][0] &&choose_move != board[0][1] &&choose_move != board[0][2] &&choose_move != board[0][3] &&choose_move != board[0][4] && choose_move != board[1][0] &&choose_move != board[1][1] &&choose_move != board[1][2] &&choose_move != board[1][3] &&choose_move != board[1][4] &&choose_move != board[2][0] &&choose_move != board[2][1] &&choose_move != board[2][2] &&choose_move != board[2][3] &&choose_move != board[2][4] &&choose_move != board[3][0] &&choose_move != board[3][1] &&choose_move != board[3][2] &&choose_move != board[3][3] &&choose_move != board[3][4] &&choose_move != board[4][0] &&choose_move != board[4][1] &&choose_move != board[4][2] &&choose_move != board[4][3] &&choose_move != board[4][4])
        { player-- ;
cout << "Invalid Move.Please Press 'enter key' and choose again : " << endl;
cout << "player " << player << " choose your move :" ;
cin >> choose_move ;
player++ ;}
        for (int i = 0 ; i < row ;i++)
        {for (int j = 0 ; j < column ; j++)
            {if ( choose_move == board[i][j])
                {board[i][j] = mark_field ;}
            }
        }
        p = check_result(board);
        if (p == 0)
        {display_board_update(board,row,column) ;
          cout << "\n\n\n\nResult : The Game Is Draw. \n" ;
          break ;}
    }while ( p != 1) ;
        if (p == 1)
        { display_board_update(board,row,column) ;
            cout << "\n\n\n\nResult : player " << --player << " is winner. \n" ;}
}

int main()
{
    field board[5][5] ;
    int x = 1 ;
    cout << "Enter the size of board (e.g. 4 for 4 * 4 or 5 for 5*5), max size is 5 * 5 : " ;
    cin >> row ;

    while (row <3 || row >5)
    {
        cout << " \n\nyour entered board size is either less than the default size of 3 * 3 or more than the size of 5 * 5. \n please enter again. :" ;
        cin >> row ;
    }
    column = row ;

    for (int i = 0 ; i < row ; i++)
    { for (int j = 0 ; j < column ; j++)
        { switch (x) { 
case 1 : board[i][j] = field_1 ; x++ ; break ;
case 2 : board[i][j] = field_2 ; x++ ; break ;
case 3 : board[i][j] = field_3 ; x++ ; break ; 
case 4 : board[i][j] = field_4 ; x++ ; break ;
case 5 : board[i][j] = field_5 ; x++ ; break ;
case 6 : board[i][j] = field_6 ; x++ ; break ; 
case 7 : board[i][j] = field_7 ; x++ ; break ; 
case 8 : board[i][j] = field_8 ; x++ ; break ; 
case 9 : board[i][j] = field_9 ; x++ ; break ; 
case 10: board[i][j] =field_10 ; x++ ; break ; 
case 11: board[i][j] =field_11 ; x++ ; break ; 
case 12: board[i][j] =field_12 ; x++ ; break ; 
case 13: board[i][j] =field_13 ; x++ ; break ; 
case 14: board[i][j] =field_14 ; x++ ; break ;
case 15: board[i][j] =field_15 ; x++ ; break ;
case 16: board[i][j] =field_16 ; x++ ; break ; 
case 17: board[i][j] =field_17 ; x++ ; break ; 
case 18: board[i][j] =field_18 ; x++ ; break ; 
case 19: board[i][j] =field_19 ; x++ ; break ;
case 20: board[i][j] =field_20 ; x++ ; break ; 
case 21: board[i][j] =field_21 ; x++ ; break ; 
case 22: board[i][j] =field_22 ; x++ ; break ;
case 23: board[i][j] =field_23 ; x++ ; break ; 
case 24: board[i][j] =field_24 ; x++ ; break ;
case 25: board[i][j] =field_25 ; x++ ; break ;}
        }
    }
    play_move(board);
}
 


thanks minipaa .. as you suggested, i tried to put it in loop and got success in solving my 2nd problem .
but unfortunately i couldn't make out for problem 1 and 3....
if you can give me some idea on how can loops be used for my problem 3 i.e. long list of codes to check the result ? and
also i don't know how to use enums or any other codes to mark the fields with 'X' and 'O' char instead of '-1' and '0' integers ..if you can help me with some coding suggestion then it will be huge help and motivation for me to think differently and enhance my limits of thinking....
thanks and regards ,
modi. :)
i don't know how to use enums
Enums are glorified integers. It is just more simple to use X and O than -1 and 1 for me. If you want, you can still use integer literals. YOu need to make changes to your display function and output 'X' or 'O' instead of -1 and 0.
Do something like
1
2
if (number == -1) std::cout << '0';
//... 


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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int row , column ;

int check_result(int board[][5])
{
   same as before .
}

string getsymbol (int x)
{
    switch (x)
    {
        case -1 : return "X " ; break;
        case  0 : return "O " ; break;
        case  1 : return "01" ; break;
        case  2 : return "02" ; break;
        case  3 : return "03" ; break;
        case  4 : return "04" ; break;
        case  5 : return "05" ; break;
        case  6 : return "06" ; break;
        case  7 : return "07" ; break;
        case  8 : return "08" ; break;
        case  9 : return "09" ; break;
        case  10 : return "10" ; break;
        case  11 : return "11" ; break;
        case  12 : return "12" ; break;
        case  13 : return "13" ; break;
        case  14 : return "14" ; break;
        case  15 : return "15" ; break;
        case  16 : return "16" ; break;
        case  17 : return "17" ; break;
        case  18 : return "18" ; break;
        case  19 : return "19" ; break;
        case  20 : return "20" ; break;
        case  21 : return "21" ; break;
        case  22 : return "22" ; break;
        case  23 : return "23" ; break;
        case  24 : return "24" ; break;
        case  25 : return "25" ; break;
        default  : cout << "Error ... something went wrong." ;
    }
}

void display_board_update(int board[][5],int row ,int column)
{
  same as before .
}

void play_move(int board[][5])
{
    int player = 1 , p ;
    int choose_move ;
    int  mark ;

    do
    {
        display_board_update(board,row,column) ;
        player = (player % 2) ? 1 : 2 ;
        cout << "player " << player << " , please choose your move : " ;
        cin >> choose_move ;
        mark = (player % 2) ? -1 : 0 ;
        player = player++ ;
        while (
            choose_move != board[0][0] &&choose_move != board[0][1] &&choose_move != board[0][2] &&choose_move != board[0][3] &&choose_move != board[0][4] &&
            choose_move != board[1][0] &&choose_move != board[1][1] &&choose_move != board[1][2] &&choose_move != board[1][3] &&choose_move != board[1][4] &&
            choose_move != board[2][0] &&choose_move != board[2][1] &&choose_move != board[2][2] &&choose_move != board[2][3] &&choose_move != board[2][4] &&
            choose_move != board[3][0] &&choose_move != board[3][1] &&choose_move != board[3][2] &&choose_move != board[3][3] &&choose_move != board[3][4] &&
            choose_move != board[4][0] &&choose_move != board[4][1] &&choose_move != board[4][2] &&choose_move != board[4][3] &&choose_move != board[4][4]
           )
        {
                player-- ;
                cout << "Invalid Move.Please Press 'enter key' and choose again : " << endl;
                cout << "player " << player << " choose your move :" ;
                cin >> choose_move ;
                player++ ;
        }
        for (int i = 0 ; i < row ;i++)
        {
            for (int j = 0 ; j < column ; j++)
            {
                if ( choose_move == board[i][j])
                {
                    board[i][j] = mark ;
                }
            }
        }

        p = check_result(board);
        if (p == 0)
        {
            display_board_update(board,row,column) ;
            cout << "\n\nResult : The Game Is Draw. \n\n" ;
            break ;
        }
    }while ( p != 1) ;
    if (p == 1)
    {
        display_board_update(board,row,column) ;
        cout << "\n\nResult : Player " << --player << " is winner. \n\n " ;
    }
}

int main()
{
    int board[5][5] ;
    int x = 1 ;

    cout << "Enter the size of board (e.g. 4 for 4 * 4 or 5 for 5*5), max size is 5 * 5 : " ;
    cin >> row ;

    while (row <3 || row >5)
    {
        cout << " \n\nyour entered board size is either less than the default size of 3 * 3 or more than the size of 5 * 5. \n please enter again. :" ;
        cin >> row ;
    }
    column = row ;

    for (int i = 0 ; i < row ; i++)
    {
        for (int j = 0 ; j < column ; j++)
        {
            board[i][j] = x++  ;
        }
    }
    play_move(board);
}


i just tried to use the link you provided to see if that can help and i get my problem solved ...!! thanks for the help :)
Topic archived. No new replies allowed.