We Have a Winnah!

The game of Tic-Tac-Toe is played on a simple 3-by-3 grid, what C programmers would call a matrix. It’s not The Matrix, of course, but it’s a simple array in which players can battle.

Coding a Tic-Tac-Toe game would involve multiple steps, such as creating the game matrix, accepting input to place an X or an O, plus a function to determine whether or not a winning move has been made. That last steps is this month’s exercise.

Oh, no: I wouldn’t dare put a full-on Tic-Tac-Toe game as an exercise. It’s something you probably want to handle one step at a time. This exercise is an important step, but not the entire code.

To prevent burnout, I’ve listed the bulk of the code below. The only thing missing is the tictactoe() function, which evaluates the matrix to determine whether a valid winning combination has occurred. Your task this month is to write that tictactoe() function.

#include <stdio.h>

void showgrid(int *array);
int tictactoe(int *array);

int main()
{
    int sample1[] = { 0, 0, 1, 0, 1, 0, 1, 0, 0 };
    int result;

    showgrid(sample1);
    result = tictactoe(sample1);
    if(result)
        puts("That's a winner!");
    else
        puts("No winnger.");

    return(0);
}

void showgrid(int *array)
{
    int x;

    puts("TIC-TAC-TOE:");
    for(x=0;x<9;x+=3)
    {
        printf("%d %d %d\n",
            array[x],
            array[x+1],
            array[x+2]);
    }
}

int tictactoe(int *array)
{
}

Please give this one a shot before you click the link below to view my solution. Remember that my solution is merely one of many; it’s not the right solution, nor is it the perfect solution.

Exercise Solution

Leave a Reply