Define a function
bool isWin(char board[][3], char target);

that returns true if character target has a winning position on this tic-tac-toe board. That means three occurences of target in a line, column or along either diagonal. For example if target is 'O' and if board[0][2], board[1][1] and board[2][0] all equal 'O' then the function will return true (since there are three O's along the reverse diagonal). The function will return false if there is no row, column or diagonal having three occurences of target. As a preconditino we stipulate that the function be called with an array argument containing exactly three rows. Note that target can be any character : not necessarily just 'X' or 'O'.

The driver for your function will read ten characters from the console: the first nine represent the board and the tenth is the target character . So if the input is X X X O O O _ _ _ Z the output would be 0 (meaning the function returns false ) since that board doesn't contain three Z's in a row. On the other hand input X X X O O O _ _ _ X would output 1 (for true ) since that board contains three X's in a row. Similarly X X X O O O _ _ _ O would generate output 1 since the board also contains three O's in a row.

bool isXWin3by3(char board[][3], char target)
{
for(int i = 0; i < 3; i++)
{
if(board[i][0] == target && board[i][1] == target && board[i][2] == target)
{
return true;
}
}
for(int i = 0; i < 3; i++)
{
if(board[0][i] == target && board[1][i] == target && board[2][i] == target)
{
return true;
}
}
if(board[0][0] == target && board[1][1] == target && board[2][2] == target)
{
return true;
}
if(board[0][2] == target && board[1][1] == target && board[2][0] == target)
{
return true;
}
return false;
}

As of now, MyProgrammingLab is saying this solution is not correct. Is there something I need to add or fix? Any help would be appreciated.

2
Contributors
1