Tic Tac toe help

Reply

Join Date: Mar 2005
Posts: 3
Reputation: sonaj is an unknown quantity at this point 
Solved Threads: 0
sonaj sonaj is offline Offline
Newbie Poster

Tic Tac toe help

 
0
  #1
Mar 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,587
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Tic Tac toe help

 
0
  #2
Mar 6th, 2005
Since the user can specify the size of your playing board, you need to create a matrix dynamically:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. int i, j, size;
  7. char **board;
  8.  
  9. printf("Enter the board size: ");
  10. if (scanf("%d", &size) != 1) {
  11. fprintf(stderr, "Invalid input\n");
  12. return EXIT_FAILURE;
  13. }
  14.  
  15. /* Create the board dynamically */
  16. board = malloc(size * sizeof *board);
  17. /* Always test for failure */
  18. if (board == NULL) {
  19. fprintf(stderr, "Memory allocation failure\n");
  20. return EXIT_FAILURE;
  21. }
  22.  
  23. for (i = 0; i < size; i++) {
  24. board[i] = malloc(size * sizeof *board[i]);
  25. if (board[i] == NULL) {
  26. fprintf(stderr, "Memory allocation failure\n");
  27.  
  28. /* Walk back and release the memory already allocated */
  29. while (--i >= 0)
  30. free(board[i]);
  31. free(board);
  32.  
  33. return EXIT_FAILURE;
  34. }
  35. }
  36.  
  37. /* Initialize the board */
  38. for (i = 0; i < size; i++) {
  39. for (j = 0; j < size; j++)
  40. board[i][j] = '\0';
  41. }
  42.  
  43. /* Release the memory allocated */
  44. for (i = 0; i < size; i++)
  45. free(board[i]);
  46. free(board);
  47.  
  48. return EXIT_SUCCESS;
  49. }
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:
  1. char player_marker = 'X'; /* For example */
  2.  
  3. if (board[i][j] != '\0')
  4. fprintf(stderr, "Cell already taken\n");
  5. else
  6. board[i][j] = player_marker;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 3
Reputation: sonaj is an unknown quantity at this point 
Solved Threads: 0
sonaj sonaj is offline Offline
Newbie Poster

Re: Tic Tac toe help

 
0
  #3
Mar 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 2
Reputation: roberto_manero is an unknown quantity at this point 
Solved Threads: 0
roberto_manero's Avatar
roberto_manero roberto_manero is offline Offline
Newbie Poster

Re: Tic Tac toe help

 
0
  #4
Mar 6th, 2005
Narue thanks for the info hope we could share more info next time....
~arigato gozaimasu~ :mrgreen:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 2
Reputation: roberto_manero is an unknown quantity at this point 
Solved Threads: 0
roberto_manero's Avatar
roberto_manero roberto_manero is offline Offline
Newbie Poster

Re: Tic Tac toe help

 
0
  #5
Mar 6th, 2005
sonaj i hope we get this prog right if not we'll see each other during summer classes.... T_T /sob.....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,587
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Tic Tac toe help

 
0
  #6
Mar 6th, 2005
>~arigato gozaimasu~
dou itashi mashite
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 3
Reputation: sonaj is an unknown quantity at this point 
Solved Threads: 0
sonaj sonaj is offline Offline
Newbie Poster

Re: Tic Tac toe help

 
0
  #7
Mar 7th, 2005
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,587
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Tic Tac toe help

 
0
  #8
Mar 7th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 2
Reputation: kshama002 is an unknown quantity at this point 
Solved Threads: 0
kshama002 kshama002 is offline Offline
Newbie Poster

Tic Tac toe help.plz........

 
0
  #9
Mar 10th, 2005
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............
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC