2 Replies - 9066 Views - Last Post: 25 November 2008 - 03:02 PM Rate Topic: -----

#1 entropypl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 24-November 08

[C++] Tic Tac Toe with Variable Size Board

Posted 24 November 2008 - 03:14 PM

We've just started learning arrays and the what-not in my C++ class. Our assignment is to create a tic tac toe program that has a user-set variable size board (3x3, 4x4, 9x9, etc). He gave us an example 3x3 tic tac toe program listed below a week back or so and told us to play around with it to make it multi-sized. I've started playing with it slightly, but for the most part, am I just going to change the constant values of 3 to variables? I thought you could not set an array to size variable?

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

char matrix[3][3];  /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
  char done;

  printf("This is the game of Tic Tac Toe.\n");
  printf("You will be playing against the computer.\n");

  done =  ' ';

  do {
	done = check(); /* see if winner */
	if(done!= ' ') break; /* winner!*/
	done = check(); /* see if winner */
  } while(done== ' ');

  if(done=='X') printf("You won!\n");
  else printf("I won!!!!\n");
  disp_matrix(); /* show final positions */

  return 0;

/* Initialize the matrix. */
void init_matrix(void)
  int i, j;

  for(i=0; i<3; i++)
	for(j=0; j<3; j++) matrix[i][j] =  ' ';

/* Get a player's move. */
void get_player_move(void)
  int x, y;

  printf("Enter X,Y coordinates for your move: ");
  scanf("%d%*c%d", &x, &y);

  x--; y--;

  if(matrix[x][y]!= ' '){
	printf("Invalid move, try again.\n");
  else matrix[x][y] = 'X';

/* Get a move from the computer. */
void get_computer_move(void)
  int i, j;
  for(i=0; i<3; i++){
	for(j=0; j<3; j++)
	  if(matrix[i][j]==' ') break;
	if(matrix[i][j]==' ') break;

  if(i*j==9)  {
	matrix[i][j] = 'O';

/* Display the matrix on the screen. */
void disp_matrix(void)
  int t;

  for(t=0; t<3; t++) {
	printf(" %c | %c | %c ",matrix[t][0],
			matrix[t][1], matrix [t][2]);
	if(t!=2) printf("\n---|---|---\n");

/* See if there is a winner. */
char check(void)
  int i;

  for(i=0; i<3; i++)  /* check rows */
	if(matrix[i][0]==matrix[i][1] &&
	   matrix[i][0]==matrix[i][2]) return matrix[i][0];

  for(i=0; i<3; i++)  /* check columns */
	if(matrix[0][i]==matrix[1][i] &&
	   matrix[0][i]==matrix[2][i]) return matrix[0][i];

  /* test diagonals */
  if(matrix[0][0]==matrix[1][1] &&
	   return matrix[0][0];

  if(matrix[0][2]==matrix[1][1] &&
	   return matrix[0][2];

  return ' ';

Is This A Good Question/Topic? 1

Replies To: [C++] Tic Tac Toe with Variable Size Board

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 941
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: [C++] Tic Tac Toe with Variable Size Board

Posted 24 November 2008 - 03:38 PM

Correct that arrays declared within your code are statically sized (Meaning they cannot ever change size, because the amount of memory allocated for them is a hard-wired constant set by the compiler). The same is true with all variables which you declare in your program - the compiler relies solely on knowing the exact size of everything in bytes in order to create the arrays and variables which you declare within your program, and also relies on the fact that these sizes must not and cannot ever change in order to keep the data they contain meaningful and well-defined.

However, it is possible (and necessary in most larger programs) to write a program which will "grab" (allocate) memory at runtime - so you may grab a chunk of memory at any time you like when the program runs, and dictate its size using some calculated or user-entered value. This is usually referred to as dynamic memory allocation - Dynamic because its not controlled by your compiler, nor are any sizes set into stone within your program, instead these values are determined by the program itself, and regulated within the program (Which means the "safety net" of the compiler isn't there to help)

Be aware that once memory is allocated, it still cannot be 'resized' per-se (Because it needs to remain contiguous, and there's no guarantee that the memory chunk hasn't been boxed-in by something else running on your system by the time you decide that it needs to be bigger), but you can fake it pretty well just by repeating the same trick over and over.
So, If you have a dynamically allocated array (Which is "just" a load of bytes lined up next to each other, nothing more), yet need the size of this memory to 'change' you can similarly create a new array of the appropriate size (again, at runtime, using some value stored in a variable) and copy the data of the original array across to the new, bigger chunk of memory. (and then remembering to de-allocate the old one, because you don't need that any more)

If this sounds a little bit more complicated than you'd like, then that's because it usually is. Most programmers try to limit their use of dynamic memory management to when its necessary, however there's little escape from it in C programming. The tools you'll need here are malloc and free (both of which are found in <stdlib.h> )

- One more note - Since dynamically allocated memory has a size which is unknown at compile time, the only way you can refer to it within a program is to create a pointer variable to keep track of it (and usually another variable which keeps track of the size of that memory). If you're unfamiliar with pointers, then dynamic memory allocation is one way to experiment and learn (Just beware of the minefield of poor and misleading information about pointers and arrays which engulfs us online)

This post has been edited by Bench: 24 November 2008 - 03:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 entropypl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 24-November 08

Re: [C++] Tic Tac Toe with Variable Size Board

Posted 25 November 2008 - 03:02 PM

Thanks for the well typed reply. Considering my experience level and scope of the course, I've decided setting a constant for the size of the matrix will work alright.


We will play on a variable-size board. Your program should define the following integer constants:

determines both the width and height of the game board (which is always square)
determines the number of Xs or Os in a row (either horizontally, vertically, or diagonally) that a player needs in order to win

so that the game can be played on different size boards simply by changing these values.

I've completed my disp_matrix function and it works flawlessly, now onto the hardest part, checking for a winner with a variable sized board AND variable sized WIN_LENGTH. Will a double loop work for the check() function? I was thinking I would have to totally butcher it and start anew.

This post has been edited by entropypl: 25 November 2008 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1