home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1993 / USERMY93.MSA / OTHELLO.C next >
C/C++ Source or Header  |  1985-11-19  |  6KB  |  156 lines

  1. /* Don't worry about these # lines - we'll cover these in a future    */
  2. /* article, okay?                            */
  3. #ifndef IC
  4. #include <osbind.h>        /* Operating System BINDings        */
  5. #endif IC
  6.  
  7. /************************************************************************/
  8. /* The first thing we do is set up an array which we will use as our    */
  9. /* Othello board. Note that since we use char's for the individual    */
  10. /* squares, and terminate each line with '\0' (the standard C string    */
  11. /* terminator) then we can display each line using printf() very easily.*/
  12. /*                                    */
  13. /* If we had used int's or long's or some other data type then our    */
  14. /* show_line() function (see below) would have to look more complicated    */
  15. /************************************************************************/
  16.  
  17. char board[8][9] =    {
  18.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' },
  19.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' },
  20.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' },
  21.             { '.', '.', '.', 'X', 'O', '.', '.', '.', '\0' },
  22.             { '.', '.', '.', 'O', 'X', '.', '.', '.', '\0' },
  23.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' },
  24.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' },
  25.             { '.', '.', '.', '.', '.', '.', '.', '.', '\0' }
  26.             };
  27.  
  28. /* Note that, since we are using a null-terminated array of char's to    */
  29. /* represent each line of the board, we could have written the above    */
  30. /* in the form of strings. Have a go at this and I'll give you the    */
  31. /* answer next month.                            */
  32.  
  33.  
  34. /************************************************************************/
  35. /* main()    -    This is the first function to be executed in    */
  36. /*            each and every C program. A main() function    */
  37. /*            must always appear in your code.        */
  38. /************************************************************************/
  39.  
  40. main()
  41. {
  42. void show_board();        /* Declare function returning nothing    */
  43.  
  44.  
  45. /* Display the Othello board.                        */
  46. show_board();
  47.  
  48.  
  49. printf ( "\n\nPress a key" );
  50. Bconin ( 2 );                /* Wait for a key press        */
  51. };
  52.  
  53.  
  54. /************************************************************************/
  55. /* show_board()        -    Displays the Othello board.        */
  56. /*                                    */
  57. /*                This function takes no arguments, hence    */
  58. /*                the "()" after its name, and returns    */
  59. /*                no values, hence the "void" in front.    */
  60. /************************************************************************/
  61.  
  62. void show_board()
  63. {
  64. int line;                /* Declare int used for line No    */
  65. int show_first_line();            /* Declare function returns int    */
  66. void show_next_line();            /* Declare function, returns    */
  67.                     /* nothing, but see below.    */
  68.  
  69. printf ( " ________ \n" );
  70.  
  71. line = show_first_line();        /* Display the first line    */
  72.  
  73.  
  74. /* We'll deal with loops like the while() loop next month, but here is    */
  75. /* simple while() loop for you to look at. If I told you that this    */
  76. /* repeats the "show_next_line ( &line );" statement until it reachs    */
  77. /* the bottom of our board, could you work out what the syntax of a    */
  78. /* while() loop is? The answer will be given in next month's column.    */
  79.  
  80. while ( line < 7 )
  81.     show_next_line ( &line );    /* Note pointer usage        */
  82.  
  83. printf ( " ¯¯¯¯¯¯¯¯ \n" );
  84. };
  85.  
  86.  
  87. /************************************************************************/
  88. /* show_first_line()    -    Takes no arguments, just displays the    */
  89. /*                First line of the Othello board and    */
  90. /*                Returns the line number of that line.    */
  91. /************************************************************************/
  92.  
  93. int show_first_line()        /* Declare type of thing to be returned    */
  94. {
  95. int foo = 0;            /* Line number in the board        */
  96. void show_line();        /* Display a line of the Othello board    */
  97.  
  98. show_line ( foo );        /* Display this line of the board    */
  99.  
  100. return ( foo );            /* Return the line number.        */
  101. };
  102.  
  103.  
  104. /************************************************************************/
  105. /* show_next_line()    -    Takes a single argument which is a    */
  106. /*                pointer to an int which holds the    */
  107. /*                number  of the current line of the    */
  108. /*                Othello board. It then calculates and    */
  109. /*                displays the following line.        */
  110. /*                                    */
  111. /*                Note the usage of a pointer to the line    */
  112. /*                number, which removes the need to    */
  113. /*                explicitly return a value.        */
  114. /************************************************************************/
  115.  
  116. void show_next_line ( x )    /* Declare that function returns nothing*/
  117. int *x;                /* Declare type of argument to function    */
  118. {
  119. void show_line();        /* Display a line of the Othello board    */
  120.  
  121. *x = *x + 1;            /* Get the number of the next line    */
  122.  
  123. show_line ( *x );        /* Display this line of the board    */
  124. };
  125.  
  126.  
  127. /************************************************************************/
  128. /* show_line()        -    This function takes a single argument    */
  129. /*                (int x) and displays that line of the    */
  130. /*                Othello board.                */
  131. /*                                    */
  132. /*                No values are returned (hence the void)    */
  133. /************************************************************************/
  134.  
  135. void show_line ( x )        /* Declare nothing returned by function    */
  136. int x;
  137. {
  138. /* Display line (int x) of the Othello board.                */
  139. printf ( "|%s|\n", &board[x][0] );
  140.  
  141. /*----------------------------------------------------------------------*/
  142. /* Note that we have used a rather odd-looking format to describe the    */
  143. /* line of our board which we wish to display ("&board[x][0]").        */
  144. /*                                    */
  145. /* What this means is that we are taking the address of the first char    */
  146. /* on the x'th line of the array "board". Several tutorials may tell    */
  147. /* you that we could use simply "board[x]" here instead. That is wrong.    */
  148. /* Or rather, "board[x]" would work on many C compilers, but can cause    */
  149. /* odd problems on many others. I use this format both for this reason    */
  150. /* (it makes the code more portable) and also because this format makes    */
  151. /* it abundantly clear that what we are dealing with is a pointer into    */
  152. /* and array, rather than a simple array. As a result, it is far easier    */
  153. /* (to my mind) to see what the code is doing, and so easier to debug.    */
  154. /*----------------------------------------------------------------------*/
  155. };
  156.