Error Message

I’m making a Tic-Tac-Toe game, when I ran the program this was my output. How can I fix this?

Would you like to play Tic-Tac-Toe? (Y/N)
error: failed to launch '/Users/connorwillimas/Library/Developer/Xcode/DerivedData/Williams9-ejczipgccqoijlgrbjpxvtedophh/Build/Products/Debug/Williams9' -- failed to get the task for process 20245

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
/*
 Connor Williams
 Williams9.cpp
 CSC1610
 November 21, 2012
 This is a Tic-Tac-Toe game.
 */
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>

#define size 3
#define width 2

using namespace std;

void line(int);
void row();
void displayBoard();
void begin();
void endGame();
void gamePlay(char);
void getInput(int&,int&);
void computerMove(int&,int&);
bool checkBoard();
bool checkComp();
bool checkPlayer();
void ask();

char moves[size][size];

int main()
{
    ask();
    return 0;
}
void ask()
{
    char ans;
    cout <<"Would you like to play Tic-Tac-Toe? (Y/N)"<<endl;
    cin >> ans;
    gamePlay(ans);
}
void gamePlay(char ans)
{
    switch(ans)
    {
        case 'y':
        case 'Y':
            begin();
            break;
        case 'n':
        case 'N':
            endGame();
            break;
        default:
            ask();
            cout << "Invalid Character"<<endl;
    }
}
void line(int row)
{
    cout<<setw(width)<<moves[row][0]<<setw(width)<<'|';
    cout<<setw(width)<<moves[row][1]<<setw(width)<<'|';
    cout<<setw(width)<<moves[row][2]<<endl;
}
void row()
{
    cout <<"---|---|---"<<endl;
}
void displayBoard()
{
    line(0);
    row();
    line(1);
    row();
    line(2);
}
void begin()
{
    for (int row=0;row<size;row++)
        for (int col=0;col<size;col++)
            moves[row][col] = ' ';
    displayBoard();
    int row=0;
    int col=0;
    getInput(row,col);
    
}
void getInput(int& row, int& col)
{
    cout << "Please enter a row [1 - 3]: ";
    cin >> row;
    cout << "Please enter a column [1 - 3]: ";
    cin >> col;
    if (row < 4 && row > 0 && col < 4 && col > 0)
    {
        if (moves[row-1][col-1]==' ')
        {
            moves[row-1][col-1] = 'X';
            displayBoard();
        }
        else
        {
            cout <<"Invalid Position please choose again"<<endl;
            getInput(row,col);
        }
    }
    else
    {
        cout <<"Invalid Position please choose again"<<endl;
        getInput(row,col);
    }
    computerMove(row,col);
}
void computerMove(int& row,int& col)
{
    row = rand()%2;
    col = rand()%2;
    cout <<"Computer chose: "<<row+1<<','<<col+1<<endl;
    if (moves[row][col]==' ')
    {
        moves[row][col]='O';
        displayBoard();
        getInput(row,col);
    }
    else
        computerMove(row,col);
}
bool checkBoard()
{
    if (checkPlayer()==true)
    {
        cout <<"Player Wins"<<endl;
        return true;
    }
    else if (checkComp()==true)
    {
        cout << "Computer Wins"<<endl;
        return true;
    }
    else if (moves[0][0]==' '||moves[0][1] == ' '||moves[0][2]==' '||moves[1][0]==' '||moves[1][1]==' '||moves[1][2]==' '||moves[2][0]==' '||moves[2][1]==' '||moves[2][2] == ' ')
    {
        return false;
    }
    else
    {
        cout <<"It's a draw!"<<endl;
        return true;
    }
}
bool checkComp()
{
    if (moves[0][0] == 'O' && moves[0][1] == 'O' && moves[0][2] == 'O')
    {
        return true;
    }
    if (moves[1][0] == 'O' && moves[1][1] == 'O' && moves[1][2] == 'O')
    {
        return true;
    }
    if (moves[2][0] == 'O' && moves[2][1] == 'O' && moves[2][2] == 'O')
    {
        return true;
    }
    if (moves[0][0] == 'O' && moves[1][0] == 'O' && moves[2][0] == 'O')
    {
        return true;
    }
    if (moves[0][1] == 'O' && moves[1][1] == 'O' && moves[2][1] == 'O')
    {
        return true;
    }
    if (moves[0][2] == 'O' && moves[1][2] == 'O' && moves[2][2] == 'O')
    {
        return true;
    }
    if (moves[0][0] == 'O' && moves[1][1] == 'O' && moves[2][2] == 'O')
    {
        return true;
    }
    if (moves[0][2] == 'O' && moves[1][1] == 'O' && moves[2][0] == 'O')
    {
        return true;
    }
    else
        return false;
}
bool checkPlayer()
{
    
    if (moves[0][0] == 'X' && moves[0][1] == 'X' && moves[0][2] == 'X')
    {
        return true;
    }
    if (moves[1][0] == 'X' && moves[1][1] == 'X' && moves[1][2] == 'X')
    {
        return true;
    }
    if (moves[2][0] == 'X' && moves[2][1] == 'X' && moves[2][2] == 'X')
    {
        return true;
    }
    if (moves[0][0] == 'X' && moves[1][0] == 'X' && moves[2][0] == 'X')
    {
        return true;
    }
    if (moves[0][1] == 'X' && moves[1][1] == 'X' && moves[2][1] == 'X')
    {
        return true;
    }
    if (moves[0][2] == 'X' && moves[1][2] == 'X' && moves[2][2] == 'X')
    {
        return true;
    }
    if (moves[0][0] == 'X' && moves[1][1] == 'X' && moves[2][2] == 'X')
    {
        return true;
    }
    if (moves[0][2] == 'X' && moves[1][1] == 'X' && moves[2][0] == 'X')
    {
        return true;
    }
    else
        return false;
}
void endGame()
{
    
    cout << "Game Over. Thanks for playing!";
}
Runs beautifully 4 me
-
but for 1 single exception...

When it is searching for a solution, especially when there is NO solution, then it goes into an infinite loop, .

(which finally crashes my sys ( after about 1 minute)

It displays the grid, and the "X" & "O" char's correctly

I had the tree corners covered ( (1,1), (1,3), (3,1)
The program did not actually show my last move on the screen, but I could see that it was searching for the next available move

- reminded me of the 1985 film "War Games" where the computer began trying-out all possibilities.

This can be seen by adding this line as the first line of the computerMove() subrt'n:
 
getchar(); // #include <stdio.h> 



Because you only have 11 possible positions in the grid, then I suggest that you store all 11 values in a 1-d array (vector)
1-9, + all , + none = 11.

You could also keep a counter ( i_cntr = 1, i_cntr <11, i_cntr++)
since only (about) 11 total moves are allowed, but an Array[11] would be cleaner.


1
2
    else
        computerMove(row,col);


Also, rather than calling recursively computerMove(), you might, rather, try placing this subrt'n into a WHILE loop. That way it can run for a longtime without overflowing the stack.

Lastly, when you have it De-Bugged, then PLEASE think about trying to consolidate your code - especially the checkComp, and the checkPlayer subrt'ns which can be condensed into about 2-3 lines.

Last edited on
Incis B wrote:
Because you only have 11 possible positions in the grid, then I suggest that you store all 11 values in a 1-d array (vector)
1-9, + all , + none = 11.


The solution is fine with a 2d array - I have seen good solutions with either 1d or 2d arrays, it's too much to ask the OP to change it now.

@CPDubs

Line 143 would be much better as a for loop - you should always avoid long conditions like that - especially when there is a pattern.

With the checkplayer & checkcomp functions - these could be combined into 1 function that takes an X or O as an argument.

HTH

Topic archived. No new replies allowed.