| | |
Tic Tac toe help
![]() |
•
•
Join Date: Mar 2005
Posts: 3
Reputation:
Solved Threads: 0
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.
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:
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:
When it comes to making sure that a cell isn't already taken, you only need to use a conditional statement:
C Syntax (Toggle Plain Text)
#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; }
#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");
}
} C Syntax (Toggle Plain Text)
char player_marker = 'X'; /* For example */ if (board[i][j] != '\0') fprintf(stderr, "Cell already taken\n"); else board[i][j] = player_marker;
I'm here to prove you wrong.
>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.
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.
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Tic Tac Toe Homework (C++)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the C Forum
- Previous Thread: can someone help...please with this prb
- Next Thread: Help reading some DLL file
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h







T_T /sob.....