mines.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // mines.c - this file is a part of Minesweeper, a computer game
  3. //
  4. // Copyright (C) 2020 The Minesweeper contributors
  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,
  9. // or (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 GNU
  14. // 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. // @author Andrew Tokarskiy <tokarskiy.a@keemail.me>
  20. //
  21. #include "mines.h"
  22. #include <stdlib.h>
  23. #define ALLOCATE(type, amount) (type*)malloc(sizeof(type) * amount)
  24. inline const size_t sqr(const size_t a);
  25. void randomizeMines(Game* game, const size_t firstMoveI, const size_t firstMoveJ, const int minesAmount);
  26. Game* initGame(const size_t height, const size_t width, const int minesAmount) {
  27. int** map = ALLOCATE(int*, height);
  28. int** statuses = ALLOCATE(int*, height);
  29. for (size_t i = 0; i < height; i++) {
  30. map[i] = ALLOCATE(int, width);
  31. statuses[i] = ALLOCATE(int, width);
  32. for (size_t j = 0; j < width; j++) {
  33. map[i][j] = 0;
  34. statuses[i][j] = STATUS_NULL;
  35. }
  36. }
  37. Game* game = ALLOCATE(Game, 1);
  38. game->map = map;
  39. game->statuses = statuses;
  40. game->width = width;
  41. game->height = height;
  42. game->minesAmount = minesAmount;
  43. game->initialized = 0;
  44. return game;
  45. }
  46. inline const size_t sqr(const size_t a) {
  47. return a * a;
  48. }
  49. inline const size_t dif(const size_t a, const size_t b) {
  50. return a > b ? a - b : b - a;
  51. }
  52. void shuffle(size_t* array, const size_t count) {
  53. if (count > 1) {
  54. for (size_t i = 0; i < count - 1; i++) {
  55. const size_t j = i + rand() / (RAND_MAX / (count - i) + 1);
  56. const int t = array[j];
  57. array[j] = array[i];
  58. array[i] = t;
  59. }
  60. }
  61. }
  62. const int calculateMinesAround(Game* game, const size_t i, const size_t j) {
  63. int amount = 0;
  64. const size_t iFrom = i == 0 ? 0 : i - 1;
  65. const size_t iTo = i == game->height - 1 ? game->height - 1 : i + 1;
  66. const size_t jFrom = j == 0 ? 0 : j - 1;
  67. const size_t jTo = j == game->width - 1 ? game->width - 1 : j + 1;
  68. for (size_t ii = iFrom; ii <= iTo; ii++) {
  69. for (size_t jj = jFrom; jj <= jTo; jj++) {
  70. if (ii == i && jj == j) {
  71. continue;
  72. }
  73. if (game->map[ii][jj] == MINE_INDEX) {
  74. amount++;
  75. }
  76. }
  77. }
  78. return amount;
  79. }
  80. void randomizeMines(Game* game, const size_t firstMoveI, const size_t firstMoveJ, const int minesAmount) {
  81. int** map = game->map;
  82. const size_t size = game->width * game->height;
  83. size_t* possiblePlaces = ALLOCATE(size_t, size);
  84. size_t pos = 0;
  85. for (size_t i = 0; i < size; i++) {
  86. const size_t posI = i / game->width;
  87. const size_t posJ = i % game->width;
  88. int sqDistance = sqr(dif(firstMoveI, posI)) + sqr(dif(firstMoveJ, posJ));
  89. if (sqDistance >= 4) { // 2 - distance between points (0, 0) & (2, 0). sqrt(2) - distance between points (0, 0) & (1, 1)
  90. possiblePlaces[pos] = i;
  91. pos++;
  92. }
  93. }
  94. const size_t possiblePlacesSize = pos;
  95. shuffle(possiblePlaces, possiblePlacesSize);
  96. for (size_t i = 0; i < minesAmount; i++) {
  97. const size_t place = possiblePlaces[i];
  98. const size_t posI = place / game->width;
  99. const size_t posJ = place % game->width;
  100. map[posI][posJ] = MINE_INDEX;
  101. }
  102. free(possiblePlaces);
  103. for (size_t i = 0; i < game->height; i++) {
  104. for (size_t j = 0; j < game->width; j++) {
  105. if (map[i][j] != MINE_INDEX) {
  106. map[i][j] = calculateMinesAround(game, i, j);
  107. }
  108. }
  109. }
  110. }
  111. void freeGame(Game* game) {
  112. for (size_t i = 0; i < game->height; i++) {
  113. int* mapLine = game->map[i];
  114. free(mapLine);
  115. int* statLine = game->statuses[i];
  116. free(statLine);
  117. }
  118. free(game->map);
  119. free(game->statuses);
  120. free(game);
  121. }
  122. // call only when map[i][j] >= 0
  123. void fill(Game* game, const size_t i, const size_t j) {
  124. if (i < 0 || i >= game->height || j < 0 || j >= game->width) {
  125. return;
  126. }
  127. if (game->statuses[i][j] >= 0) {
  128. return;
  129. }
  130. const size_t iFrom = i == 0 ? 0 : i - 1;
  131. const size_t iTo = i == game->height - 1 ? game->height - 1 : i + 1;
  132. const size_t jFrom = j == 0 ? 0 : j - 1;
  133. const size_t jTo = j == game->width - 1 ? game->width - 1 : j + 1;
  134. game->statuses[i][j] = game->map[i][j];
  135. if (game->map[i][j] == 0) {
  136. for (size_t ii = iFrom; ii <= iTo; ii++) {
  137. for (size_t jj = jFrom; jj <= jTo; jj++) {
  138. if (ii == i && jj == j) {
  139. continue;
  140. }
  141. fill(game, ii, jj);
  142. }
  143. }
  144. }
  145. }
  146. const ActionResult checkWinStatus(Game* game) {
  147. int** statuses = game->statuses;
  148. int nullCount = 0;
  149. for (size_t i = 0; i < game->height; i++) {
  150. for (size_t j = 0; j < game->width; j++) {
  151. if (statuses[i][j] == STATUS_FLAG || statuses[i][j] == STATUS_NULL) {
  152. nullCount++;
  153. }
  154. }
  155. }
  156. return nullCount == game->minesAmount ? Win : Continue;
  157. }
  158. // return 0 -> continue, return 1 -> lose, 2 -> win
  159. const ActionResult action(Game* game, const size_t i, const size_t j, const ActionType actionType) {
  160. int** statuses = game->statuses;
  161. int** map = game->map;
  162. if (statuses[i][j] >= 0) { // if cell is opened
  163. return Continue;
  164. }
  165. if (actionType == MarkMine && statuses[i][j] == STATUS_NULL) {
  166. statuses[i][j] = STATUS_FLAG;
  167. return Continue;
  168. }
  169. if (actionType == MarkMine && statuses[i][j] == STATUS_FLAG) {
  170. statuses[i][j] = STATUS_NULL;
  171. return Continue;
  172. }
  173. if (game->initialized == 0) {
  174. randomizeMines(game, i, j, game->minesAmount);
  175. game->initialized = 1;
  176. }
  177. if (game->map[i][j] == MINE_INDEX) {
  178. for (size_t ii = 0; ii < game->height; ii++) {
  179. for (size_t jj = 0; jj < game->width; jj++) {
  180. if (map[ii][jj] == MINE_INDEX) {
  181. statuses[ii][jj] = STATUS_MINE;
  182. }
  183. }
  184. }
  185. return Lose;
  186. }
  187. fill(game, i, j);
  188. return checkWinStatus(game);
  189. }