I am only a college student at a nearby university and i dont claim myself an expert but i am really having difficulty in trying to make a game called Tic Tac Toe in Turbo C.
our professor put some requirements for the game. First, the user may enter an equivalent number meaning this number is the size of the board. i used for statements to get around. our professor said that we will have to use arrays. my biggest problem is how to plot my x's and o's.

it is like this:

Enter a number: 4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Enter move: 4

only the four below becomes an X or an O

1 2 3 X
5 6 7 8
9 10 11 12
13 14 15 16

and still i havent got to the part where i must not overwrite my moves if i entered four on my last turn, i must not be able to occupy that slot again.

I am open for comments and suggestions. Thank you.

Since the user can specify the size of your playing board, you need to create a matrix dynamically:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int i, j, size;
  char **board;

  printf("Enter the board size: ");
  if (scanf("%d", &size) != 1) {
    fprintf(stderr, "Invalid input\n");
    return EXIT_FAILURE;
  }

  /* Create the board dynamically */
  board = malloc(size * sizeof *board);
  /* Always test for failure */
  if (board == NULL) {
    fprintf(stderr, "Memory allocation failure\n");
    return EXIT_FAILURE;
  }

  for (i = 0; i < size; i++) {
    board[i] = malloc(size * sizeof *board[i]);
    if (board[i] == NULL) {
      fprintf(stderr, "Memory allocation failure\n");

      /* Walk back and release the memory already allocated */
      while (--i >= 0)
        free(board[i]);
      free(board);

      return EXIT_FAILURE;
    }
  }

  /* Initialize the board */
  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++)
      board[i][j] = '\0';
  }

  /* Release the memory allocated */
  for (i = 0; i < size; i++)
    free(board[i]);
  free(board);

  return EXIT_SUCCESS;
}

This code doesn't do anything except take a size from standard input, build a matrix dynamically, initialize it to null characters, and destroy the matrix. Since each element is a char, you can put either an X or an O in it. To actually plot the moves, consider numbering each cell and letting the user choose which one to add the move to:

#include <stdio.h>
#include <stdlib.h>

void show_board(char **board, int size);

int main(void)
{
  int i, j, size;
  char **board;

  printf("Enter the board size: ");
  if (scanf("%d", &size) != 1) {
    fprintf(stderr, "Invalid input\n");
    return EXIT_FAILURE;
  }

  /* Create the board dynamically */
  board = malloc(size * sizeof *board);
  /* Always test for failure */
  if (board == NULL) {
    fprintf(stderr, "Memory allocation failure\n");
    return EXIT_FAILURE;
  }

  for (i = 0; i < size; i++) {
    board[i] = malloc(size * sizeof *board[i]);
    if (board[i] == NULL) {
      fprintf(stderr, "Memory allocation failure\n");

      /* Walk back and release the memory already allocated */
      while (--i >= 0)
        free(board[i]);
      free(board);

      return EXIT_FAILURE;
    }
  }

  /* Initialize the board */
  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++)
      board[i][j] = '\0';
  }

  /* Print the board */
  show_board(board, size);

  /* Release the memory allocated */
  for (i = 0; i < size; i++)
    free(board[i]);
  free(board);

  return EXIT_SUCCESS;
}

void show_board(char **board, int size)
{
  int i, j, k = 1;

  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++) {
      if (board[i][j] == '\0')
        printf("%3d", k++);
      else
        printf("%3c", board[i][j]);
    }
    printf("\n");
  }
}

When it comes to making sure that a cell isn't already taken, you only need to use a conditional statement:

char player_marker = 'X'; /* For example */

if (board[i][j] != '\0')
  fprintf(stderr, "Cell already taken\n");
else
  board[i][j] = player_marker;

Thank you very much, the help i needed was answered, i've tried to forget the countless times i didnt sleep, i need to pass it tomorrow, hopefully my colleagues will be able to pull out something. We will just need to edit and adjust some to pull out this case study.

sonaj i hope we get this prog right if not we'll see each other during summer classes.... :sad: T_T /sob.....

i found out how to use the program, but i am still having problem checking with the winner. somehow we needed to players, i initialized them to null characters... :confused:

>i am still having problem checking with the winner
If you don't want to try your hand at a clever solution, brute force it. There are only eight winning combinations for either player in traditional tic tac toe: first row across, second row across, third row across, first column down, second column down, third column down, diagonal from top left to bottom right, diagonal from bottom left to top right.

Get that working as a bunch of loops, and you'll see ways to generalize the test for any size board.

Hi all. I am Kshama Thaker......i have an assignment on Tic tac toe.. in c++ and i have to complete it within 2 days....as i am not professional i need big Help...........and again i have final exam of UNIx....i dont have time to prepare...so plz.help me............

This article has been dead for over six months. Start a new discussion instead.