main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /***********************************************************************
  2. *
  3. * This file is a part of Minesweeper - a computer game
  4. * Copyright (C) 2020, 2021 Andrew Tokarskiy <tokarskiy.a@keemail.me>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. *************************************************************************/
  20. #include "mines.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24. #include <string.h>
  25. #define COMMAND_BUFFER_SIZE 32
  26. #define DEFAULT_WIDTH 10
  27. #define DEFAULT_HEIGHT 10
  28. #define DEFAULT_MINES 20
  29. void outputGameConsole(Game* const game) {
  30. int** statuses = game->statuses;
  31. {
  32. printf(" ");
  33. for (int j = 0; j < game->width; j++)
  34. {
  35. const int jLastDigit = (j + 1) % 10;
  36. printf("%d ", jLastDigit);
  37. }
  38. printf("\n");
  39. }
  40. for (int i = 0; i < game->height; i++) {
  41. const int iLastDigit = (i + 1) % 10;
  42. printf("%d ", iLastDigit);
  43. for (int j = 0; j < game->width; j++) {
  44. const int status = statuses[i][j];
  45. switch (status) {
  46. case STATUS_NULL:
  47. printf("_ ");
  48. break;
  49. case STATUS_FLAG:
  50. printf("F ");
  51. break;
  52. case STATUS_MINE:
  53. printf("M ");
  54. break;
  55. case 0:
  56. printf(" ");
  57. break;
  58. default:
  59. printf("%d ", status);
  60. break;
  61. }
  62. }
  63. printf("\n");
  64. }
  65. }
  66. typedef struct {
  67. int i;
  68. int j;
  69. ActionType command;
  70. int parseStatus;
  71. }
  72. Turn;
  73. Turn parseTurn(Game* const game, char* command, const int n) {
  74. char* token = strtok(command, " ");
  75. if (token == NULL) {
  76. Turn turn;
  77. turn.parseStatus = 0;
  78. return turn;
  79. }
  80. char* commandStr = token;
  81. token = strtok(NULL, " ");
  82. if (token == NULL) {
  83. Turn turn;
  84. turn.parseStatus = 0;
  85. return turn;
  86. }
  87. char* xStr = token;
  88. token = strtok(NULL, " ");
  89. if (token == NULL) {
  90. Turn turn;
  91. turn.parseStatus = 0;
  92. return turn;
  93. }
  94. char* yStr = token;
  95. const int x = atoi(xStr);
  96. const int y = atoi(yStr);
  97. if (x <= 0 || x > game->width || y <= 0 || y > game->height) {
  98. Turn turn;
  99. turn.parseStatus = 0;
  100. return turn;
  101. }
  102. ActionType type;
  103. if (strcmp(commandStr, "flag") == 0) {
  104. type = MarkMine;
  105. }
  106. else if (strcmp(commandStr, "click") == 0) {
  107. type = Click;
  108. }
  109. else {
  110. Turn turn;
  111. turn.parseStatus = 0;
  112. return turn;
  113. }
  114. Turn turn;
  115. turn.i = y - 1;
  116. turn.j = x - 1;
  117. turn.command = type;
  118. turn.parseStatus = 1;
  119. return turn;
  120. }
  121. typedef struct {
  122. int width;
  123. int height;
  124. int mines;
  125. } InitData;
  126. InitData parseArgs(char* args[], int n) {
  127. InitData data;
  128. data.width = DEFAULT_WIDTH;
  129. data.height = DEFAULT_HEIGHT;
  130. data.mines = DEFAULT_MINES;
  131. int i = 1;
  132. while (i < n - 1) {
  133. if (strcmp("--width", args[i]) == 0) {
  134. const int w = atoi(args[i + 1]);
  135. if (w > 0) {
  136. data.width = w;
  137. }
  138. i++;
  139. }
  140. if (strcmp("--height", args[i]) == 0) {
  141. const int h = atoi(args[i + 1]);
  142. if (h > 0) {
  143. data.height = h;
  144. }
  145. i++;
  146. }
  147. if (strcmp("--mines", args[i]) == 0) {
  148. const int m = atoi(args[i + 1]);
  149. if (m > 0) {
  150. data.mines = m;
  151. }
  152. i++;
  153. }
  154. i++;
  155. }
  156. return data;
  157. }
  158. void printGreetings() {
  159. printf("--- MINESWEEPER ---\n");
  160. printf("\n");
  161. printf("How to play: \n");
  162. printf(" click <x> <y>: click on cell with coords x and y.\n");
  163. printf(" flag <x> <y>: flag mine on cell with coords x and y.\n");
  164. printf("Numeration of coords starts from 1\n");
  165. printf("\n");
  166. printf("Good luck!\n");
  167. }
  168. int main(int argc, char* argv[]) {
  169. char command[COMMAND_BUFFER_SIZE];
  170. printGreetings();
  171. srand(time(NULL));
  172. const InitData parse = parseArgs(argv, argc);
  173. Game* game = initGame(parse.height, parse.width, parse.mines);
  174. outputGameConsole(game);
  175. while (1) {
  176. printf("> ");
  177. fgets(command, COMMAND_BUFFER_SIZE, stdin);
  178. const Turn turnData = parseTurn(game, command, COMMAND_BUFFER_SIZE);
  179. if (turnData.parseStatus == 0) {
  180. printf("Error!\n");
  181. continue;
  182. }
  183. ActionResult actionStatus = action(game, turnData.i, turnData.j, turnData.command);
  184. outputGameConsole(game);
  185. if (actionStatus == Lose) {
  186. printf("Lose...\n");
  187. break;
  188. }
  189. if (actionStatus == Win) {
  190. printf("WIN!!!\n");
  191. break;
  192. }
  193. }
  194. freeGame(game);
  195. }