home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XGAMES / XHEXTRIS.TAR / stdsys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  4.9 KB  |  177 lines

  1. /*
  2.  * hextris Copyright 1990 David Markley, dm3e@+andrew.cmu.edu, dam@cs.cmu.edu
  3.  *
  4.  * Permission to use, copy, modify, and distribute, this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided that
  6.  * the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of the copyright holders be used in
  9.  * advertising or publicity pertaining to distribution of the software with
  10.  * specific, written prior permission, and that no fee is charged for further
  11.  * distribution of this software, or any modifications thereof.  The copyright
  12.  * holder make no representations about the suitability of this software for
  13.  * any purpose.  It is provided "as is" without express or implied warranty.
  14.  *
  15.  * THE COPYRIGHT HOLDER DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19.  * DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
  20.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21.  * PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. /* This file contains routines used by hextris that are generally standard
  25.  * on most system and display types. It contains no I/O, other than to files.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include "header.h"
  30.  
  31. /* The function reverse is not copyrighted.
  32.  *
  33.  * This is a standard string reverse routine.
  34.  */
  35. reverse(s)
  36. char *s;
  37. {
  38.     int c,i,j;
  39.  
  40.     for (i = 0, j = strlen(s)-1; i < j; i++,j--) {
  41.     c = s[i];
  42.     s[i] = s[j];
  43.     s[j] = c;
  44.     }
  45. }
  46.  
  47. /* The function itoa is not copyrighted.
  48.  *
  49.  * This is a standard integer to string converter.
  50.  */
  51. itoa(n,s)
  52. int n;
  53. char *s;
  54. {
  55.     int i, sign;
  56.  
  57.     if ((sign = n) < 0)
  58.       n = -n;
  59.     i = 0;
  60.     do {
  61.     s[i++] = n % 10 + '0';
  62.     } while ((n /= 10) > 0);
  63.     if (sign < 0)
  64.       s[i++] = '-';
  65.     s[i] = '\0';
  66.     reverse(s);
  67. }
  68.  
  69. /* This randomly selects the next piece. It also selects the current
  70.  * piece, if it has not yet been set (rotation = -1).
  71.  */
  72. new_piece(npiece,piece)
  73. piece_t *npiece,*piece;
  74. {
  75.     if (npiece->rotation == -1) {
  76.     piece->type = (int)(random() % NUMBEROFPIECES);
  77.     piece->rotation = 0;
  78.     piece->row = 1;
  79.     piece->column = MAXCOLUMN / 2;
  80.     } else {
  81.     piece->type = npiece->type;
  82.     piece->rotation = npiece->rotation;
  83.     piece->row = npiece->row;
  84.     piece->column = npiece->column;
  85.     }
  86.     npiece->type = (int)(random() % NUMBEROFPIECES);
  87.     npiece->rotation = 0;
  88.     npiece->row = 1;
  89.     npiece->column = MAXCOLUMN / 2;
  90. }
  91.  
  92. /* This reads in the high score file.
  93.  */
  94. read_high_scores(high_scores)
  95. high_score_t high_scores[MAXHIGHSCORES];
  96. {
  97.     int i, j;
  98.     FILE *high_score_file;
  99.     char high_score_file_name[512];
  100.     char buffer[40];
  101.  
  102.     strcpy(high_score_file_name,HIGHSCOREDIR);
  103.     strcat(high_score_file_name,HIGHSCOREEXT);
  104.     
  105.     if ((high_score_file = fopen(high_score_file_name , "r")) == NULL) {
  106.     fprintf(stderr,"xhextris: Can't open high score file.\n");
  107.     return 0;
  108.     }
  109.  
  110.     for (i = 0; i < MAXHIGHSCORES; i++) {
  111.     fread(high_scores[i].name,sizeof(char),MAXNAMELENGTH,
  112.           high_score_file);
  113.     fread(high_scores[i].userid,sizeof(char),MAXUSERIDLENGTH,
  114.           high_score_file);
  115.     fread(buffer,sizeof(char),40,high_score_file);
  116.     high_scores[i].score = atoi(buffer);
  117.     fread(buffer,sizeof(char),40,high_score_file);
  118.     high_scores[i].rows = atoi(buffer);
  119.     if (feof(high_score_file))
  120.       break;
  121.     }
  122.     for (j = i; j < MAXHIGHSCORES; j++) {
  123.     strcpy(high_scores[j].name,"David Markley");
  124.     strcpy(high_scores[j].userid,"CMU");
  125.     high_scores[j].score = 0;
  126.     high_scores[j].rows = 0;
  127.     }
  128.     fclose(high_score_file);
  129.     return 1;
  130. }
  131.  
  132. /* This writes the high score file.
  133.  */
  134. write_high_scores(high_scores,uniqueid)
  135. high_score_t high_scores[MAXHIGHSCORES];
  136. char *uniqueid;
  137. {
  138.     int i;
  139.     FILE *high_score_file;
  140.     char tmp_high_score_file_name[512];
  141.     char high_score_file_name[512];
  142.     char buffer[40];
  143.     
  144.     strcpy(tmp_high_score_file_name,HIGHSCOREDIR);
  145.     strcat(tmp_high_score_file_name,uniqueid);
  146.     strcpy(high_score_file_name,HIGHSCOREDIR);
  147.     strcat(high_score_file_name,HIGHSCOREEXT);
  148.     
  149. #ifdef AFS
  150.     beGames();
  151. #endif
  152.     if ((high_score_file = fopen(tmp_high_score_file_name, "w")) == NULL) {
  153.     fprintf(stderr,"xhextris: Can't open high score file.\n");
  154.     return 0;
  155.     }
  156.     for (i = 0; i < MAXHIGHSCORES; i++) {
  157.     fwrite(high_scores[i].name,sizeof(char),MAXNAMELENGTH,
  158.            high_score_file);
  159.     fwrite(high_scores[i].userid,sizeof(char),MAXUSERIDLENGTH,
  160.            high_score_file);
  161.     itoa(high_scores[i].score,buffer);
  162.     fwrite(buffer,sizeof(char),40,high_score_file);
  163.     itoa(high_scores[i].rows,buffer);
  164.     fwrite(buffer,sizeof(char),40,high_score_file);
  165.     }
  166.     fflush(high_score_file);
  167.     fclose(high_score_file);
  168.     rename(tmp_high_score_file_name,high_score_file_name);
  169. #ifdef AFS
  170.     bePlayer();
  171. #endif
  172.     return 1;
  173. }
  174.  
  175.  
  176.  
  177.