SHARE
TWEET

Untitled

a guest Dec 15th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*-------------------------------------------------------------------------
  2.     Include files:
  3. -------------------------------------------------------------------------*/
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. /*-------------------------------------------------------------------------
  7.     Constants and definitions:
  8. -------------------------------------------------------------------------*/
  9. #define N 11
  10. #define space '_'
  11. #define player1 1
  12. #define player2 2
  13. #define x 'X'
  14. #define o 'O'
  15.  
  16. /*-------------------------------------------------------------------------
  17.     Function declaration
  18. -------------------------------------------------------------------------*/
  19. void print_welcome();
  20. void print_enter_board_size();
  21. void print_board(char board[N][N], int n);
  22. void print_player_turn(int player_index);
  23. void print_error();
  24. void print_winner(int player_index);
  25. void print_tie();
  26. void filltheemptyboard(char board[N][N], int boardsize);
  27. void fillforplayer1(char board[N][N], int boardsize);
  28. void fillforplayer2(char board[N][N], int boardsize);
  29. bool straightlineOfx(char board[N][N],int boardsize);
  30. bool straightlineOfo(char board[N][N],int boardsize);
  31. bool straightRowOfx(char board[N][N],int boardsize);
  32. bool straightRowOfo(char board[N][N],int boardsize);
  33. bool DiameterX(char board[N][N],int boardsize);
  34. bool DiameterO(char board[N][N],int boardsize);
  35. bool OppDiameterX(char board[N][N], int boardsize);
  36. bool OppDiameterO(char board[N][N], int boardsize);
  37. bool Player2Winner( char board [N][N],int boardsize);
  38. bool Player1Winner( char board [N][N],int boardsize);
  39. void TheWinner(char board[N][N],int boardsize);
  40. bool theboardisfull(char board[N][N], int boardsize);
  41. int TheGame(char board[N][N],int boardsize);
  42. int main()
  43. {
  44.     int boardsize;
  45.     print_welcome();
  46.     print_enter_board_size();
  47.     scanf("%d",&boardsize);
  48.     char board[N][N];
  49.     filltheemptyboard(board,boardsize);
  50.     TheGame(board, boardsize);
  51.    
  52.     return 0;
  53. }
  54.  
  55. //print welcome message
  56. //1 lines
  57. void print_welcome()
  58. {
  59.     printf("*** Welcome to AVOIDANCE TIC-TAC-TOE game ***\n\n");
  60. }
  61.  
  62. //print message to enter board size
  63. //1 lines
  64. void print_enter_board_size()
  65. {
  66.     printf("Please enter board size (1 to %d):\n", N);
  67. }
  68.  
  69. //print the board
  70. //7 lines
  71. void print_board(char board[N][N], int n)
  72. {
  73.     printf("\nCurrent board:\n");
  74.     for (int i = 0; i < n; i++)
  75.     {
  76.         printf("|");
  77.         for (int j = 0; j < n; j++)
  78.         {
  79.             printf("%c|", board[i][j]);
  80.         }
  81.         printf("\n");
  82.     }
  83.     printf("\n");
  84. }
  85.  
  86. //print a request for player with index "player_index" to move
  87. //1 lines
  88. void print_player_turn(int player_index)
  89. {
  90.     printf("\nPlayer ** %d **, enter next move:\n", player_index);
  91. }
  92.  
  93. //print error message
  94. //1 lines
  95. void print_error()
  96. {
  97.     printf("Illegal move!!!, please try again:\n");
  98. }
  99.  
  100. //print the winner
  101. //1 lines
  102. void print_winner(int player_index)
  103. {
  104.     printf("Player %d Wins! Hooray!\n", player_index);
  105. }
  106.  
  107. //print message to announce there is a tie (no one wins)
  108. //1 lines
  109. void print_tie()
  110. {
  111.     printf("It's a tie!\n");
  112. }
  113. void filltheemptyboard(char board[N][N],int boardsize)
  114. {
  115.     for(int i=0;i<boardsize;i++){
  116.         for(int j=0;j<boardsize;j++){
  117.             board[i][j]=space;
  118.         }
  119.     }
  120.    
  121.     print_board( board, boardsize);
  122.    
  123. }
  124. void fillforplayer1(char board[N][N], int boardsize){
  125.     int i,j;
  126.     scanf("%d",&i);
  127.     scanf("%d", &j);
  128.     if(board[i-1][j-1]==space){
  129.         board[i-1][j-1]= x;
  130.     print_board(board,boardsize);
  131.     }
  132.     else {
  133.         print_error();
  134.         fillforplayer1(board, boardsize);
  135.     }
  136. }
  137. void fillforplayer2(char board[N][N], int boardsize){
  138.     int i,j;
  139.     scanf("%d",&i);
  140.     scanf("%d", &j);
  141.     if(board[i-1][j-1]==space){
  142.         board[i-1][j-1]= o;
  143.     print_board(board,boardsize);
  144.     }
  145.     else{
  146.     print_error();
  147.     fillforplayer2(board, boardsize);
  148.     }
  149. }
  150. bool theboardisfull(char board[N][N], int boardsize)
  151. {
  152.     bool fullboard = false;
  153.      for(int i=0;i<boardsize;i++){
  154.          for(int j=0;j<boardsize;j++){
  155.              if(board[i][j]==space)
  156.                  return fullboard;
  157.          }
  158.      }
  159.     fullboard = true;
  160.     return fullboard;
  161. }
  162. bool straightlineOfx(char board[N][N],int boardsize)
  163. {
  164.     bool straightline = true;
  165.     for(int i=0;i<boardsize;i++){
  166.         for(int j=0;j<boardsize;j++)
  167.         {
  168.             if(board[i][j]!=x){
  169.                 straightline = false;
  170.             }
  171.         }
  172.             if(straightline==true)
  173.             {
  174.                 return straightline;
  175.                 }
  176.         }
  177.     return straightline;
  178. }
  179. bool straightlineOfo(char board[N][N],int boardsize)
  180. {
  181.     bool straightline = true;
  182.     for(int i=0;i<boardsize;i++){
  183.         for(int j=0;j<boardsize;j++)
  184.         {
  185.             if(board[i][j]!=o){
  186.                 straightline = false;
  187.             }
  188.         }
  189.             if(straightline==true)
  190.             {
  191.                 return straightline;
  192.                 }
  193.         }
  194.     return straightline;
  195.        
  196. }
  197. bool straightRowOfx(char board[N][N],int boardsize)
  198. {
  199.     bool straightline = true;
  200.     for(int i=0;i<boardsize;i++){
  201.         for(int j=0;j<boardsize;j++)
  202.         {
  203.             if(board[j][i]!=x){
  204.                 straightline = false;
  205.             }
  206.         }
  207.             if(straightline==true)
  208.             {
  209.                 return straightline;
  210.                 }
  211.         }
  212.     return straightline;
  213. }
  214. bool straightRowOfo(char board[N][N],int boardsize)
  215. {
  216.     bool straightline = true;
  217.     for(int i=0;i<boardsize;i++){
  218.         for(int j=0;j<boardsize;j++)
  219.         {
  220.             if(board[j][i]!=o){
  221.                 straightline = false;
  222.             }
  223.         }
  224.             if(straightline==true)
  225.             {
  226.                 return straightline;
  227.                 }
  228.         }
  229.     return straightline;
  230.        
  231. }
  232. bool DiameterX( char board[N][N],int boardsize)
  233. {
  234.     bool DiameterOfX = true;
  235.     int j = 0;
  236.      for(int i=0;i<boardsize;i++){
  237.          if(board[i][j]!=x){
  238.              DiameterOfX = false ;
  239.          return DiameterOfX;
  240.          }
  241.          j++;
  242. }
  243.     return DiameterOfX;
  244. }
  245. bool DiameterO( char board[N][N],int boardsize)
  246. {
  247.     bool DiameterOfo = true;
  248.     int j = 0;
  249.      for(int i=0;i<boardsize;i++){
  250.          if(board[i][j]!=o){
  251.              DiameterOfo = false ;
  252.          return DiameterOfo;
  253.          }
  254.          j++;
  255. }
  256.     return DiameterOfo;
  257. }
  258. bool OppDiameterX( char board[N][N], int boardsize)
  259. {
  260.     bool DiameterX = true;
  261.     int j =boardsize-1;
  262.     for(int i=0;i<boardsize;i++){
  263.      if(board[i][j]!=x){
  264.                  DiameterX = false ;
  265.              return DiameterX;
  266.              }
  267.              j--;
  268.     }
  269.         return DiameterX;
  270.     }
  271.  
  272. bool OppDiameterO(char board[N][N], int boardsize)
  273. {
  274.     bool DiameterO = true;
  275.        int j =boardsize-1;
  276.        for(int i=0;i<boardsize;i++){
  277.         if(board[i][j]!=x){
  278.                     DiameterO = false ;
  279.                 return DiameterO;
  280.                 }
  281.                 j--;
  282.        }
  283.            return DiameterO;
  284. }
  285. bool Player2Winner( char board [N][N],int boardsize){
  286.            bool Player2Winner = false;
  287.         if(straightlineOfx(board,boardsize)||straightRowOfx(board,boardsize)||OppDiameterX(board, boardsize)||DiameterX(board, boardsize)==1){
  288.             Player2Winner = true;
  289.             return Player2Winner;
  290.         }
  291.         return Player2Winner;
  292. }
  293. bool Player1Winner( char board [N][N],int boardsize)
  294. {
  295.     bool Player1Winner = false;
  296.     if(straightlineOfo(board,boardsize)||straightRowOfo(board,boardsize)||OppDiameterO(board, boardsize)||DiameterO(board, boardsize)==1){
  297.         Player1Winner = true;
  298.     return Player1Winner;
  299.     }
  300.     return Player1Winner;
  301. }
  302. void TheWinner(char board[N][N],int boardsize)
  303.     {
  304.         if(Player1Winner(board,boardsize)){
  305.            print_winner(player1);
  306.            
  307.         }
  308.         if(Player2Winner(board, boardsize)){
  309.             print_winner(player2);
  310.        
  311.         }
  312.     }
  313.  
  314. int TheGame(char board[N][N],int boardsize){
  315.     while(theboardisfull(board, boardsize)!=1){
  316.          print_player_turn(player1);
  317.          fillforplayer1(board, boardsize);
  318.          if(Player2Winner(board, boardsize)){
  319.              print_winner(player2);
  320.              return 0;
  321.              
  322.          }
  323.          print_player_turn(player2);
  324.          fillforplayer2(board, boardsize);
  325.          if(Player1Winner(board, boardsize)){
  326.              print_winner(player1);
  327.          return 0;
  328.          }
  329.     }
  330.     return 0;
  331. }
  332. int undo(char board[N][N], int boardsize, int Undonum){
  333.    
  334. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top