You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Scorelist.c 2.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Scorelist.h"
  5. #include "UI.h"
  6. #define NAMELEN 20
  7. #define SCORES 10
  8. typedef struct Score {
  9. char name[NAMELEN + 1];
  10. int level;
  11. int score;
  12. } Score;
  13. static Score scores[SCORES];
  14. void
  15. Scorelist_read() {
  16. FILE *scorefile = fopen(SCOREFILE, "r");
  17. int i;
  18. if (scorefile != NULL) {
  19. for (i = 0; i < SCORES; i++)
  20. fscanf(scorefile, "%20s%d%d\n", scores[i].name,
  21. &scores[i].level, &scores[i].score);
  22. fclose(scorefile);
  23. }
  24. else {
  25. for (i = 0; i < SCORES; i++) {
  26. strcpy(scores[i].name, "Anonymous");
  27. scores[i].level = 0;
  28. scores[i].score = 0;
  29. }
  30. }
  31. }
  32. void
  33. Scorelist_write() {
  34. FILE *scorefile = fopen(SCOREFILE, "w");
  35. int i;
  36. if (scorefile == NULL)
  37. return;
  38. for (i = 0; i < SCORES; i++)
  39. fprintf(scorefile, "%-*s %d %d\n", NAMELEN,
  40. scores[i].name, scores[i].level, scores[i].score);
  41. fclose(scorefile);
  42. }
  43. /* Add new high score to list */
  44. void
  45. Scorelist_recalc(const char *str, int level, int score) {
  46. int i;
  47. char tname[NAMELEN + 1];
  48. char *nl;
  49. if (scores[SCORES - 1].score >= score)
  50. return;
  51. for (i = SCORES - 1; i > 0; i--) {
  52. if (scores[i - 1].score < score) {
  53. strcpy (scores[i].name, scores[i - 1].name);
  54. scores[i].level = scores[i - 1].level;
  55. scores[i].score = scores[i - 1].score;
  56. }
  57. else
  58. break;
  59. }
  60. memset(tname, 0, sizeof(tname));
  61. if (str == NULL || str[0] == 0)
  62. strcpy(tname, "Anonymous");
  63. strncpy(tname, str, sizeof(tname) - 1);
  64. nl = strchr(tname,'\n');
  65. if (nl != NULL)
  66. *nl = 0;
  67. strcpy(scores[i].name, tname);
  68. scores[i].level = level;
  69. scores[i].score = score;
  70. }
  71. void
  72. Scorelist_update() {
  73. char str[500];
  74. int i;
  75. sprintf(str, "%s\n\n", "High Scores:");
  76. sprintf(str, "%s%-*s %6s %7s\n", str, NAMELEN,
  77. "Name", "Level", "Score");
  78. for (i = 0; i < SCORES; i++) {
  79. sprintf(str, "%s%-*s %6d %7d\n", str, NAMELEN,
  80. scores[i].name, scores[i].level, scores[i].score);
  81. }
  82. UI_update_dialog(DIALOG_HIGHSCORE, str);
  83. }
  84. int
  85. Scorelist_ishighscore(int val) {
  86. return (val > scores[SCORES - 1].score);
  87. }