Advertisement
Guest User

chess.cpp

a guest
Feb 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include "chessBoard.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int move( int** chess_board, int dim, int i_move, int x, int y, int* px, int* py, point* offset )
  6. {
  7.     *px = x + offset[ i_move ].x;
  8.     *py = y + offset[ i_move ].y;
  9.  
  10.     if( *px < 0 || *px >= dim || *py < 0 || *py >= dim || chess_board[ *px ][ *py ] != 0 )
  11.         return 0;
  12.     return 1;
  13. }
  14.  
  15. int root( int** chess_board, int dim, int nr_move, int x, int y, point* offset )
  16. {
  17.     chess_board[ x ][ y ] = nr_move;
  18.  
  19.     if( nr_move == dim * dim )
  20.         return 1;
  21.    
  22.     int nx, ny;
  23.     for( int i = 0; i < HORSE_MOVES; i++ )
  24.         if( move( chess_board, dim, i, x, y, &nx, &ny, offset ) )
  25.             if( root( chess_board, dim, nr_move + 1, nx, ny, offset ) )
  26.                 return 1;
  27.     chess_board[ x ][ y ] = 0;
  28.  
  29.     return 0;
  30. }
  31.  
  32. int** createChessBoard(int dim)
  33. {
  34.     int** tab = ( int** )calloc( dim, sizeof( int* ) );
  35.     if( !tab )
  36.     {
  37.         printf( " BLAD : alokacji pamieci\n\n" );
  38.         return NULL;
  39.     }
  40.     for( int i = 0; i < dim; i++ )
  41.     {
  42.         tab[ i ] = ( int* )calloc( dim, sizeof( int ) );
  43.         if( !tab[ i ] )
  44.         {
  45.             free( tab );
  46.             printf( " BLAD : alokacji pamieci\n\n" );
  47.             return NULL;
  48.         }
  49.     }
  50.     return tab;
  51. }
  52.  
  53. void freeChessBoard( int*** pTab)
  54. {
  55.     for( int i = 0; i < CHESSBOARD_SIZE; i++ )
  56.         free( (*pTab)[ i ] );
  57.     free( *pTab );
  58.     *pTab = NULL;
  59. }
  60.  
  61. void printChessBoard( int** tab ,int dim)
  62. {
  63.     for( int i = 0; i < dim; i++ )
  64.     {
  65.         printf( " %d\t", dim - i );
  66.         for( int j = 0; j < dim; j++ )
  67.             printf( "%2d ", tab[ j ][ dim-1-i ] );
  68.         printf( "\n\n" );
  69.     }
  70.     printf( "\n\t" );
  71.     for( int i = 0; i < dim; i++ )
  72.         printf( "%2d ", i+1 );
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement