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.

228 lines
4.3 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int BinX;
  4. int BinY = 12;
  5. char field[23][29];
  6. int Ballx = -1;
  7. int Bally = 27;
  8. int fieldWidth = 25;
  9. int fieldHeigth = 30;
  10. struct Player
  11. {
  12. char Name[50];
  13. int Points;
  14. }playerlist[11];
  15. void buildBin(int width, int heigth){
  16. field[width][heigth] = 'V';
  17. field[width+1][heigth+1] = '\\';
  18. field[width+2][heigth+2] = '\\';
  19. field[width-1][heigth+1] = '/';
  20. field[width-2][heigth+2] = '/';
  21. field[width][heigth+1] = ' ';
  22. }
  23. void loadLevel() {
  24. int lvlnum;
  25. clearField();
  26. Ballx = -1;
  27. Bally = 30;
  28. int tmpX,tmpY;
  29. switch (lvlnum)
  30. {
  31. case(1):
  32. BinX = 12;
  33. BinY = 10;
  34. break;
  35. case(2):
  36. case(3):
  37. case(4):
  38. case(5):
  39. case(6):
  40. tmpX = rand() % 17;
  41. tmpY = rand() % 10;
  42. break;
  43. }
  44. }
  45. int main()
  46. {
  47. int ballX = 7;
  48. int BallY = 23;
  49. while (1)
  50. {
  51. system("clear");
  52. for (int i = 0; i < 23; i++)
  53. {
  54. for (int j = 0; j < 29; j++)
  55. {
  56. field[i][j] = ' ';
  57. }
  58. }
  59. field[ballX][BallY -= 1] = 'O';
  60. buildBin(10,10);
  61. for (int i = 0; i < 29; i++)
  62. {
  63. printf("|");
  64. for (int j = 0; j < 23; j++)
  65. {
  66. if (i == 0 || i == 29 - 1 || i == 2)
  67. printf("=");
  68. else
  69. printf("%c", field[j][i]);
  70. }
  71. printf("|");
  72. printf("\n");
  73. }
  74. getStartPosition();
  75. sleep(1);
  76. }
  77. return 0;
  78. }
  79. void getStartPosition()
  80. {
  81. while (Ballx == -1)
  82. {
  83. for (int i = 2, j = 0; i <= 20; i += 2, j++)
  84. {
  85. field[i][Bally] = j + '0';
  86. }
  87. Generatefield();
  88. //printf("W\x84\hle die position des Balls (0-9):\n");
  89. printf("W\x84\hle die position des Balls (0-9):\n");
  90. scanf("%d", &Ballx);
  91. if (Ballx < 0 || 9 < Ballx)
  92. {
  93. Ballx = -1;
  94. continue;
  95. }
  96. Ballx = (Ballx + 1);
  97. }
  98. }
  99. void clearField()
  100. {
  101. for (int i = 0; i <= fieldWidth; i++)
  102. {
  103. for (int j = 0; j <= fieldHeigth; j++)
  104. {
  105. field[i][j] = ' ';
  106. }
  107. }
  108. }
  109. void Generatefield() {
  110. }
  111. void addScoreboard()
  112. {
  113. int ScorePoints;
  114. //Die txt wird geöffnet
  115. FILE* fp = fopen("ScoreBoard.txt", "a+");
  116. char name[255];
  117. printf("Bitte gib deinen Namen ein:\n");
  118. scanf("%s", name);
  119. fprintf(fp, "%s %d\n", name, ScorePoints);
  120. fclose(fp);
  121. }
  122. int checkBallPosition()
  123. {
  124. if (BinY + 24 == Bally && (Ballx == BinX || Ballx == BinX + 2 || Ballx == BinX - 2))
  125. {
  126. return 1;
  127. }
  128. else if (BinY > Bally || field[Ballx][Bally - 1] == '_' || Ballx > fieldWidth || Ballx < 0)
  129. {
  130. return 2;
  131. }
  132. }
  133. void sortScoreboard()
  134. {
  135. //Öffnet die Textdatei oder erstellt sie wenn keine existiert
  136. FILE* fp;
  137. fp = fopen("ScoreBoard.txt", "a+");
  138. if (fp == NULL)
  139. {
  140. printf("Datei konnte nicht geoeffnet werden.\n");
  141. }
  142. else
  143. {
  144. system("clear");
  145. FILE *file = fopen("ScoreBoard.txt", "r");
  146. char line[1000];
  147. int numberOfLines = 0;
  148. //Die Textdatei wird Zeile für Zeile eingelesen und in die Strukturliste eingefügt
  149. for (int j = 0; fgets(line, sizeof line, file) != NULL && j <= 10; j++)
  150. {
  151. char* playertmp = strtok(line, " ");
  152. strcpy(playerlist[j].Name, playertmp);
  153. playertmp = strtok(NULL, " ");
  154. int playerPoints = atoi(playertmp);
  155. playerlist[j].Points = playerPoints;
  156. numberOfLines = j;
  157. sleep(100);
  158. }
  159. struct Player temp;
  160. for (int i = 0; i <= numberOfLines; i++)
  161. {
  162. for (int j = 0; j < (numberOfLines - i); j++)
  163. {
  164. if (playerlist[i].Points < playerlist[i + 1].Points)
  165. {
  166. temp = playerlist[i];
  167. playerlist[i] = playerlist[i + 1];
  168. playerlist[i + 1] = temp;
  169. }
  170. }
  171. }
  172. fclose(fp);
  173. printf(" Scoreboard:\n\n");
  174. for (int j = 0; j < numberOfLines; j++)
  175. {
  176. printf("%d. %s %s %d\n", j + 1, (j < 9) ? " " : " ", playerlist[j].Name, playerlist[j].Points);
  177. sleep(500);
  178. }
  179. FILE* fp = fopen("ScoreBoard.txt", "w");
  180. for (int i = 0; i < numberOfLines; i++)
  181. {
  182. fprintf(fp, "%s %d\n", playerlist[i].Name, playerlist[i].Points);
  183. }
  184. fclose(fp);
  185. }
  186. }