Advertisement
Guest User

interactive.c

a guest
Dec 11th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "common.h"
  5. #include "utils.h"
  6.  
  7. #define MAX_NUMBER_OF_FISHES 3
  8.  
  9. int randomNumberOfFishes() {
  10.     return randomInRange(1, MAX_NUMBER_OF_FISHES);
  11. }
  12.  
  13. Point randomPoint(Board board) {
  14.     int x = randomInRange(0, board.size.width - 1);
  15.     int y = randomInRange(0, board.size.height - 1);
  16.  
  17.     Point point = { x, y };
  18.  
  19.     return point;
  20. }
  21.  
  22. Point randomEmptyFieldPoint(Board board) {
  23.     Point point;
  24.  
  25.     do {
  26.         point = randomPoint(board);
  27.     } while(hasFishesAt(point, board));
  28.  
  29.     return point;
  30. }
  31.  
  32. Point centerOf(Board board) {
  33.     int x = (int) board.size.height / 2;
  34.     int y = (int) board.size.width / 2;
  35.  
  36.     Point center = { x, y };
  37.     return center;
  38. }
  39.  
  40. int valueWithOffset(int value, int offset, Range boundaries) {
  41.     int _withOffset = value + offset;
  42.  
  43.     if (_withOffset <= boundaries.from) return boundaries.from;
  44.     if (_withOffset >= boundaries.to) return boundaries.to;
  45.  
  46.     return _withOffset;
  47. }
  48.  
  49. Area squareWithCenterAt(Point point, Board board, int radius) {
  50.     int numberOfPoints = (2 * radius + 1) * (2 * radius + 1);
  51.     int x = point.x, y = point.y;
  52.  
  53.     Range xRange = { 0, board.size.width - 1 };
  54.     Range yRange = { 0, board.size.height - 1 };
  55.  
  56.     Point *points = malloc(numberOfPoints * sizeof(Point));
  57.     points[0] = point;
  58.     int currentIndex = 0;
  59.  
  60.     Point pointToAdd;
  61.  
  62.     for (int xOffset = -radius; xOffset < radius; xOffset++) {
  63.         currentIndex++;
  64.  
  65.         int _xWithOffset = x + xOffset;
  66.         int xWithOffset = _xWithOffset > 0;
  67.  
  68.         pointToAdd.x = valueWithOffset(x, xOffset, xRange);
  69.         pointToAdd.y = y;
  70.         points[currentIndex] = pointToAdd;
  71.  
  72.         for (int yOffset = -radius; yOffset < radius; yOffset++) {
  73.             currentIndex++;
  74.             pointToAdd.x = valueWithOffset(x, xOffset, xRange);
  75.             pointToAdd.y = valueWithOffset(y, yOffset, yRange);
  76.             points[currentIndex] = pointToAdd;
  77.         }
  78.     }
  79.  
  80.     for (int yOffset = -radius; yOffset < radius; yOffset++) {
  81.         currentIndex++;
  82.         pointToAdd.x = x;
  83.         pointToAdd.y = valueWithOffset(y, yOffset, yRange);
  84.         points[currentIndex] = pointToAdd;
  85.     }
  86.  
  87.     Area neighbourhood = {
  88.             .numberOfPoints = 9,
  89.             .points = points
  90.     };
  91.  
  92.     return neighbourhood;
  93. }
  94.  
  95. Board populateWithFishes(Board board) {
  96.     Area middleSquare = squareWithCenterAt(centerOf(board), board, 1);
  97.  
  98.     for (int i = 0; i < middleSquare.numberOfPoints; i++) {
  99.         Point point = middleSquare.points[i];
  100.         updateNumberOfFishAt(point, board, randomNumberOfFishes());
  101.     }
  102.  
  103.     int numberOfFields = board.size.height * board.size.width;
  104.  
  105.     // Todo: Move strange numbers to constants
  106. //    int fieldsToPopulate = (numberOfFields * 1000) / 100; // 38% of ice/water(?)
  107.  
  108.     int fieldsToPopulate = 100000;
  109.  
  110.     while (fieldsToPopulate >= 0) {
  111.         Point point = randomEmptyFieldPoint(board);
  112.         Area neighbour = squareWithCenterAt(point, board, 1);
  113.         bool shouldPopulate = false;
  114.  
  115.         for (int i = 1; i < neighbour.numberOfPoints; i++) {
  116.             if (hasFishesAt(neighbour.points[i], board)) shouldPopulate = true;
  117.         }
  118.  
  119.         if (shouldPopulate) {
  120.             updateNumberOfFishAt(point, board, randomInRange(2, 3));
  121.             fieldsToPopulate--;
  122.         }
  123.     }
  124. //
  125. //    for (int x = 0; x < board.size.width; x++) {
  126. //        for (int y = 0; y < board.size.height; y++) {
  127. //            int z = 0;
  128. //            if (b1.map[i + 1][j] != 0)
  129. //                z++;
  130. //            if (b1.map[i - 1][j] != 0)
  131. //                z++;
  132. //            if (b1.map[i][j + 1] != 0)
  133. //                z++;
  134. //            if (b1.map[i][j - 1] != 0)
  135. //                z++;
  136. //            if (z > 2)//||
  137. //                b1.map[i][j] = (rand() % 3) + 1;
  138. //        }
  139. //    }
  140.  
  141.     return board;
  142. }
  143.  
  144.  
  145. int** runPlacementPhase(int** placementBoard, Dimensions boardDimensions, Player* players, int numberOfPlayers, int penguinsPerPlayer) {
  146.     // One round is one move of every player
  147.     printf("It's time to place your penguins!\n");
  148.  
  149.     int roundsLeft = penguinsPerPlayer;
  150.     while (roundsLeft != 0) {
  151.         for (int i = 0; i < numberOfPlayers; i++) {
  152.             Player currentPlayer = players[i];
  153. //            Point point = askPlayerForPoint(currentPlayer);
  154. //            placePenguin(placementBoard, point, currentPlayer);
  155.         }
  156.  
  157.         roundsLeft--;
  158.     }
  159.  
  160. // Todo: Use the legacy code to finish the function @bjacak
  161.  
  162. //while (penguins_placed < penguin_number * player_count) {
  163. //    printf("PLAYER%d PLACE YOUR PENGUIN!\n", current_player + 1);
  164. //    char placement_x;
  165. //    int placement_y;
  166. //    getchar();
  167. //    scanf("%c", &placement_x);
  168. //    placement_x = placement_x - 65;
  169. //    getchar();
  170. //    scanf("%d", &placement_y);
  171. //    //check if possible
  172. //    if (map[placement_y - 1][2 * placement_x] == 0 || placement_y - 1 >= rowsCount || placement_x >= columnsCount) {
  173. //        while (map[placement_y - 1][2 * placement_x] == 0 || placement_y - 1 >= rowsCount || placement_x >= columnsCount) {
  174. //            printf("NOT HERE!\nPLACE YOUR PENGUIN SOMEWHERE ELSE!\n");
  175. //            getchar();
  176. //            scanf("%c", &placement_x);
  177. //            placement_x = placement_x - 65;
  178. //            getchar();
  179. //            scanf("%d", &placement_y);
  180. //        }
  181. //    }
  182. //
  183. //    printf("%d\n", map[placement_y - 1][2 * placement_x]);
  184. //    //
  185. //    player_score[current_player] = player_score[current_player] + map[placement_y - 1][2 * placement_x];
  186. //    //
  187. //    map[placement_y - 1][2 * placement_x] = 0;
  188. //    map[placement_y - 1][2 * placement_x + 1] = current_player + 1;
  189. //    //
  190. //    printf("   ");
  191. //    for (int i = 0; i < columnsCount; i++) {
  192. //        printf("%c  ", i + 65);
  193. //    }
  194. //    printf("\n");
  195. //    for (int i = 0; i < rowsCount; i++) {
  196. //        if (i < 9) {
  197. //            printf("0%d ", i + 1);
  198. //        } else {
  199. //            printf("%d ", i + 1);
  200. //        }
  201. //
  202. //        for (int j = 0; j < columnsCount; j++) {
  203. //            printf("%d%d ", map[i][2 * j], map[i][2 * j + 1]);
  204. //        }
  205. //            printf("\n");
  206. //        }
  207. //
  208. //        current_player++;
  209. //        if (current_player >= player_count) {
  210. //            current_player = 0;
  211. //        }
  212. //
  213. //        penguins_placed++;
  214. //    }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement