1. ## beginner tic-tac-toe problems

(I've looked around on the internet for several hours now and was unable to come up with a solution)
So I've been messing around with some examples from the book I'm reading, and I've just finished reading a section that had a nice little tic-tac-toe example, but it was only two-player, so I looked around and found this (single player tic-tac-toe with a minimax algorithm) and thought I'd try and put the two together using the example from the book as the base. But I've run in to some trouble (which I expected since I'm a complete noob).

Firstly, the win() function is not working and the game will always end up being a draw. These exact expressions were working before I put them into their own function.

Secondly, the bolded printf("Y") doesn't print, I know that since I haven't filled the second do-while statement with any expressions it'll get stuck but I can't figure out why

Lastly, I haven't been able to figure out how I would implement a minimax algorithm similar to the one described here.

sorry if this is a lot of code, wasn't sure what I could cut that would still give a good overview of everything it's doing.
Code:
```/* Tic-Tac-Toe */
#include <stdio.h>

void win(char board[3][3], int winner, int player, int line)
{
/* Check for a winning line - diagonals first */
if((board[0][0]==board[1][1] && board[0][0]==board[2][2])||(board[0][2]==board[1][1] && board[0][2]==board[2][0]))
{
winner = player;
}
else
{    /* Check rows and columns for a winning line */
for(line = 0; line <= 2; line++)
{
if((board[line][0]==board[line][1] && board[line][0]==board[line][2])||(board[0][line]==board[1][line] && board[0][line]==board[2][line]))
{
winner = player;
}
}
}
}

int main(void)
{
int player = 0;                      /* Player number - 1 or 2               */
int winner = 0;                      /* The winning player                   */
int choice = 0;                      /* Square selection number for turn     */
int computer_choice = 0;
int row = 0;                         /* Row index for a square               */
int column = 0;                      /* Column index for a square            */
int line=0;                          /* Row or column index in checking loop */
int player_count=0;

char board[3][3] = {                 /* The board */
{'1','2','3'},           /* Initial values are reference numbers */
{'4','5','6'},           /* used to select a vacant square for   */
{'7','8','9'}            /* a turn.                              */
};

printf("How many players are going to play?\n"); /*select amount of players between 1 and 2*/
scanf(" %d", &player_count);
if(player_count < 1 || player_count > 2)
{
printf("invalid selection, choose between 1 or 2 players!\n");
return 0;
}
if(player_count == 1)
{
printf("The computer will be Player 2\n");
}
/* The main game loop. The game continues for up to 9 turns */
/* As long as there is no winner                            */
for(int i = 0; i<9 && winner==0; i++)
{
/* Display the board */
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
player = i%2 + 1;                 /* Select player */

if(player_count == 2)
{
/* Get valid player square selection */
do
{
printf("\nPlayer %d, please enter the number of the square where you want to place your %c: ", player, (player==1)?'X':'O');
scanf(" %d", &choice);
row = --choice/3;                /* Get row index of square    */
column = choice%3;               /* Get column index of square */
}while(choice<0 || choice>9 || board[row][column]>'9');
/* Insert player symbol */
board[row][column] = (player == 1) ? 'X' : 'O';
}
if(player_count == 1)
{
player = 1;
do /*player's turn*/
{
printf("\nPlayer %d, please enter the number of the square where you want to place your %c: ", player, (player==1)?'X':'O');
scanf("%d", &choice);
row = --choice/3;                /* Get row index of square    */
column = choice%3;               /* Get column index of square */
}while(choice<0 || choice>9 || board[row][column]>'9');
/* Insert player symbol */
board[row][column] = (player == 1) ? 'X' : 'O';
printf("Y"); /*temporary printf to ensure that we don't get stuck in an infinity loop*/
do /*computer turn*/
{
/*minimax algorithm.... hopefully*/
}while(choice<0 || choice>9 || board[row][column]>'9');
}
win(board, winner, player, line); /*check if the game is won*/
}
/* Game is over so display the final board */
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

/* Display result message */
if(winner == 0)
printf("\nHow boring, it is a draw\n");
else
printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n",
winner);
return 0;
}```

2. win() never changes the value of 'winner', since win() takes its 'winner' argument by value.

Try:

Code:
```void win(char board[3][3], int* winner, int player, int line) {
/* Check for a winning line - diagonals first */
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2])
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0])) {
*winner = player;
} else { /* Check rows and columns for a winning line */
for (line = 0; line <= 2; line++) {
if ((board[line][0] == board[line][1]
&& board[line][0] == board[line][2])
|| (board[0][line] == board[1][line]
&& board[0][line] == board[2][line])) {
*winner = player;
}
}
}
}```
And when you call win() from main, it should be:

Code:
`win(board, &winner, player, line);`

3. Originally Posted by Absurd
-snip-
Ok, that fixed the win() issue, I get what I was doing wrong there thanks. Any suggestions for the other two problems I have?

4. So, I decided to try and just implement this version of minimax straight into my modified program, while I've dramatically cut down the compiler warnings (GCC), I still have one that happens several times throughout the file, as I am no where near close to somewhat understanding how pointers fully work I haven't been able to solve them.
Code:
```program5_08.c: In function ‘minimax’:program5_08.c:27:27: warning: passing argument 2 of ‘win’ from incompatible pointer type [-Wincompatible-pointer-types]
int winning = win(board, &winner, player);
^
program5_08.c:5:5: note: expected ‘int *’ but argument is of type ‘int **’
int win(char board[3][3], int* winner, int player)
^
program5_08.c:40:40: warning: passing argument 3 of ‘minimax’ from incompatible pointer type [-Wincompatible-pointer-types]
int thisScore = -minimax(board, 2, &winner);
^
program5_08.c:24:5: note: expected ‘int *’ but argument is of type ‘int **’
int minimax(char board[3][3], int player, int* winner)
^
program5_08.c: In function ‘computer_move’:
program5_08.c:69:40: warning: passing argument 3 of ‘minimax’ from incompatible pointer type [-Wincompatible-pointer-types]
int tempScore = -minimax(board, 2, &winner);
^
program5_08.c:24:5: note: expected ‘int *’ but argument is of type ‘int **’
int minimax(char board[3][3], int player, int* winner)
^```
I've included the C file as an attachment as I don't want to keep posting large amounts of code straight onto the post.