Saturday, August 31, 2013

-TicTacToe 2D Arrays - C++

I coded this yesterday because I realized I haven't really created any game with C and because many people here are looking for 2d arrays help. This is a basic tic-tac-toe game with customizable size. You can make it 3x3, 5x5, 10x10 - your wish - by changing the rows and columns preprocessors to your like. This source could be helpful for beginners, like myself, trying to learning multi-dimensional arrays.

Also, for all of those who are using system("CLS"). I've included an alternate function using windows functions. Basically, it gets the output handle to the screen and gets the number of characters on the screen and fills them with ' '(spaces). Done.

With highlights: http://pastebin.com/b8NBK1qZ
#include 
#include 

#define b_col  3
#define b_row  3

void clear_screen()
{
 HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
 DWORD bytes_write, size;
 COORD coord = {0, 0};
 CONSOLE_SCREEN_BUFFER_INFO csbi;

 GetConsoleScreenBufferInfo(output_handle, &csbi);
 
 size = csbi.dwSize.X * csbi.dwSize.Y;
 
 FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write);
 SetConsoleCursorPosition(output_handle, coord);
}

void clear_board(char game_board[][b_col])
{
 for(int i = 0; i < b_row; i++)
  for(int j = 0; j < b_col; j++)
   game_board[i][j] = '-';
}

void print_board(char game_board[][b_col])
{
 for(int i = 0; i < b_row; i++)
 {
  for(int j = 0; j < b_col; j++)
   printf("%c   ", game_board[i][j]);
  printf("\n\n\n");
 }
}

bool place_xo(char game_board[][b_col], int row, int col, char x_o)
{

 if(game_board[row - 1][col - 1] != '-') //if coordinates given is not empty
   return false;
 else
 {
  game_board[row - 1][col - 1] = x_o;
  clear_screen(); //clear screen. don't use system("CLS");
  print_board(game_board); //print board.
  return true;
 }
}

int check_row_col(char game_board[][b_col], char x_o, unsigned int f_row = 0, unsigned int f_col = 0)
{
 int counter = 0;
 if(f_row != 0) //checks rows
 {
  for(int i = 0; i < b_col; i++)
   if(game_board[f_row - 1][i] == x_o)
    counter++;
 }
 else if(f_col != 0) //checks columns
 {
  for(int i = 0; i < b_row; i++)
   if(game_board[i][f_col - 1] == x_o)
    counter++;
 }
 else if(f_col == 0 && f_col == 0) //diagonal
 {
  int d_col = 0;
  for(int d_row = 0; d_row < b_row; d_row++, d_col++)
   if(game_board[d_row][d_col] == x_o)
    counter++;
  if(counter == b_row)
   return counter;
  else
  {
   d_col = b_col;
   counter = 0;
   for(int d_row = 0; d_row < b_row; d_row++, d_col--)
    if(game_board[d_row][d_col - 1] == x_o)
     counter++;
  }
 }
 return counter;
}

bool win_or_not(char game_board[][b_col], char x_o)
{
 for(int i = 1; i < b_col; i++) //checks all columns
 {
  if(check_row_col(game_board, x_o, 0, i) == b_row)
  {
   printf("\n\n\n%c won the game", x_o);
   return true;
  }
 }

 for(int i = 1; i < b_row; i++) //checks all rows
 {
  if(check_row_col(game_board, x_o, i, 0) == b_row)
  {
   printf("\n\n\n%c won the game", x_o);
   return true;
  }
 }

 if(check_row_col(game_board, x_o, 0, 0) == b_row) //diagonal checks
 {
   printf("\n\n\n%c won the  game", x_o);
   return true;
 }

 return false;
}

int main()
{
 if(b_row != b_col)
  return -1;
 char game_board[b_row][b_col];
 bool win_not = false;
 int row, col;
 clear_board(game_board); //clear board
 print_board(game_board); //print board

 bool x_turn = true;

 do
 {
  printf("--It's %c's turn--\n\n", x_turn ? 'X' : 'O');

  printf("Row: ");
  scanf("%d", &row);

  printf("Column: ");
  scanf("%d", &col);

  if(row > b_row || col > b_col)
  {
   printf("\nOut of bounds.\n\n");
   continue;
  }

  if(!place_xo(game_board, row, col, x_turn ? 'X' : 'O')) //place the 'x' and 'o'. x_turn ? 'X' : 'O' returns based on the bool value of x_turn
  {
   printf("\nCannot put %c into row %d, col %d.\n\n", x_turn ? 'X' : 'O', row, col);
   continue;
  }

  win_not = win_or_not(game_board, x_turn ? 'X' : 'O');

  x_turn = !x_turn;
 }while(win_not == false);
 getchar();
 getchar();
 return 0;
}

No comments:

Post a Comment