Tic Tac Toe Bug issue

My program isn't running. can someone please help me find whats wrong and how to fix it. thank you

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
#include "stdafx.h"
#include <iostream>
using namespace std;
class game
{
private:
    char arr[4][4];
    bool PlayerTurn;
    void init()
    {
        for (int i=0;i<4;i++)
        {
            for (int j= 0;j<4;j++)
    if (i==0)
    {
        arr [0][1]='0';
        arr[0][2]='1';
        arr [0][3]='2';
    }
else if (j==0)
{
    arr[1][0]='0';
    arr[2][0]='1';
    arr[3][0]='2';
}
            
else
{arr[i][j]='*';
}
        }
    }
}

bool getPlayerMove(int x, int y, bool turn)
{if(arr[x][y]!='*')
{
    cout<<"the move is not valid, please try again"<< endl;
    return false;
}
if (turn)
{
    arr[x][y]='x';
}
    else
    {
        arr[x][y]='0';
        
    }
    return true;
}
bool isThereADraw()
{
    for (int i=0;i<4;i++)
    {
        for (int j=0;j<4;j++)
        {
            if (arr[i][j]=='x')
            {
                return false;
            }
        }
    }
    return true;
}
bool isDiagonalWin(char a)
{
 if(arr[0][0]==a&&arr[1][1]==a&&arr[2][2]==a)
 {
     return true;
 }
if (arr[0][2]==a&&arr[1][1]==a&&arr[2][0]==a)
{
    return true;
}
    return false;
}
bool isColumnWin(int i, char a)
{
if(arr[0][i]==a&&arr[1][i]==a&&arr[2][i]==a)
{
    return true;
}
    return false;
}
bool isLineWin(int i,char a)
{
if(arr[i][0]==a&&arr[i][1]==a&&arr[i][2]==a)
{
    return true;
}
    return false;
}
bool isThereAWinner(char a)
{
    for(int i=0;i<4;i++)
    {
        if(isLineWin(i,a))
            
        {
            return true;
        }
    }
   if(isColumnWin(i,a))
   {
       return true;
   }
}
    if(isDiagonalWin(a))
    {
        return true;
    }
    return false;
}
public:
game()
{
    init();
    playerTurn=true;
    
}
void printBoard()
{
    cout<<endl;
    cout<<" "<<arr[0][0] << " | "<<arr[0][1] << " | " << arr[0][2] << " | " <<arr[0][3] << endl;
    cout<<"-------------" <<endl;
    cout<< " " <<arr[1][0]<< " | " << arr[1][1]<< " | "<< arr[1][2]<< " | "<< arr[1][3] <<endl;
    cout<<"-------------"<<endl;
    cout<<" "<<arr[2][0]<< " | " <<arr[2][1]<< " | "<<arr[2][2]<< " | "<<arr[2][3]<<endl;
    cout<<"-------------"<<endl;
    cout<<" " <<arr[3][0]<< " | "<<arr[3][1]<<" | " <<arr[3][2]<<" | " <<arr[3][3]<<endl;
    cout<<endl;
}
bool promptUserStart()
{
    init();
    char reps;
    cout<< "do you want to play? y/n: ";
    cin>>reps;
    if (reps=='y'||reps=='Y')
    {
        playerTurn=true;
        return true;
    }
    else
    {
        cout<<"Thank you for playing" <<endl;
        return false;
    }
    return false
}

void startGame()
{
    int x,y;
    if(playerTurn)
    {
        printBoard();
        cout<<"Player One"<<endl;
        cout<<" choose row 1 to 4"<<endl;
        cin>>x;
        cout<<"choose column 1 to 4"<<endl;
        cin>>y;
        if(getPlayerMove(x-1,y-1,playerTurn))
        {
            playerTurn= false;
            if(isThereAWinner('x'))
            {
                
                cout<< " Player 1 is the Winner!!" <<end;
                printBoard();
                return;
            }
            else if(isThereADraw())
            {
                cout<<"the game is a draw" <<endl;
                printBoard();
                return;
            }
            startGame();
        }
        else
        {
            playerTurn=true;
            startGame();
        }
    }
    else
    {
        printBoard();
        cout<<"player two"<<endl;
        cout<<" choose row 1 to 4"<<endl;
        cin>>x;
        cout<<" choose column 1 to 4"<< endl;
        cin>>y;
        if(getPlayerMove(x-1, y-1, playerTurn))
        {
            playerTurn=true;
            if(isThereAWinner('0'))
            {
                cout<< " player 2 is the Winner!!"<<endl;
                printBoard();
                return;
            }
            else if(isThereADraw())
            {
                cout<<"the game is a draw"<<endl;
                printBoard();
                return;
            }
            startGame();
        }
    }
}
};

int main()
{
    game game1;
    while(game1.promptUserStart())
    {
        game1.startGame();
        
    }
    return 0;
}
Last edited on
My program isn't running.


That's pretty broad. Can you be a little more specific? What is it doing vs. what is it supposed to do?

Have you built this up piece by piece? Start by initializing the board and displaying it. Then accept valid input and display the result. Then add input validation (gracefully handle invalid input). Lastly add in the isThereAWinner() function.

By the way, you use a 4x4 grid for your game, initializing the row and column headers to their indices. However, you ask for row and column from 1 - 4 (lines 159, 161, 191, 193). You don't need to store the header information in the grid itself--you can generate that while you are displaying the grid. But however you decide to handle that, make sure you are working with the correct elements of your 2-dimensional array.
Registered users can post here. Sign in or register to post.