Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "common.h"
- #include "utils.h"
- #define MAX_NUMBER_OF_FISHES 3
- int randomNumberOfFishes() {
- return randomInRange(1, MAX_NUMBER_OF_FISHES);
- }
- Point randomPoint(Board board) {
- int x = randomInRange(0, board.size.width - 1);
- int y = randomInRange(0, board.size.height - 1);
- Point point = { x, y };
- return point;
- }
- Point randomEmptyFieldPoint(Board board) {
- Point point;
- do {
- point = randomPoint(board);
- } while(hasFishesAt(point, board));
- return point;
- }
- Point centerOf(Board board) {
- int x = (int) board.size.height / 2;
- int y = (int) board.size.width / 2;
- Point center = { x, y };
- return center;
- }
- int valueWithOffset(int value, int offset, Range boundaries) {
- int _withOffset = value + offset;
- if (_withOffset <= boundaries.from) return boundaries.from;
- if (_withOffset >= boundaries.to) return boundaries.to;
- return _withOffset;
- }
- Area squareWithCenterAt(Point point, Board board, int radius) {
- int numberOfPoints = (2 * radius + 1) * (2 * radius + 1);
- int x = point.x, y = point.y;
- Range xRange = { 0, board.size.width - 1 };
- Range yRange = { 0, board.size.height - 1 };
- Point *points = malloc(numberOfPoints * sizeof(Point));
- points[0] = point;
- int currentIndex = 0;
- Point pointToAdd;
- for (int xOffset = -radius; xOffset < radius; xOffset++) {
- currentIndex++;
- int _xWithOffset = x + xOffset;
- int xWithOffset = _xWithOffset > 0;
- pointToAdd.x = valueWithOffset(x, xOffset, xRange);
- pointToAdd.y = y;
- points[currentIndex] = pointToAdd;
- for (int yOffset = -radius; yOffset < radius; yOffset++) {
- currentIndex++;
- pointToAdd.x = valueWithOffset(x, xOffset, xRange);
- pointToAdd.y = valueWithOffset(y, yOffset, yRange);
- points[currentIndex] = pointToAdd;
- }
- }
- for (int yOffset = -radius; yOffset < radius; yOffset++) {
- currentIndex++;
- pointToAdd.x = x;
- pointToAdd.y = valueWithOffset(y, yOffset, yRange);
- points[currentIndex] = pointToAdd;
- }
- Area neighbourhood = {
- .numberOfPoints = 9,
- .points = points
- };
- return neighbourhood;
- }
- Board populateWithFishes(Board board) {
- Area middleSquare = squareWithCenterAt(centerOf(board), board, 1);
- for (int i = 0; i < middleSquare.numberOfPoints; i++) {
- Point point = middleSquare.points[i];
- updateNumberOfFishAt(point, board, randomNumberOfFishes());
- }
- int numberOfFields = board.size.height * board.size.width;
- // Todo: Move strange numbers to constants
- // int fieldsToPopulate = (numberOfFields * 1000) / 100; // 38% of ice/water(?)
- int fieldsToPopulate = 100000;
- while (fieldsToPopulate >= 0) {
- Point point = randomEmptyFieldPoint(board);
- Area neighbour = squareWithCenterAt(point, board, 1);
- bool shouldPopulate = false;
- for (int i = 1; i < neighbour.numberOfPoints; i++) {
- if (hasFishesAt(neighbour.points[i], board)) shouldPopulate = true;
- }
- if (shouldPopulate) {
- updateNumberOfFishAt(point, board, randomInRange(2, 3));
- fieldsToPopulate--;
- }
- }
- //
- // for (int x = 0; x < board.size.width; x++) {
- // for (int y = 0; y < board.size.height; y++) {
- // int z = 0;
- // if (b1.map[i + 1][j] != 0)
- // z++;
- // if (b1.map[i - 1][j] != 0)
- // z++;
- // if (b1.map[i][j + 1] != 0)
- // z++;
- // if (b1.map[i][j - 1] != 0)
- // z++;
- // if (z > 2)//||
- // b1.map[i][j] = (rand() % 3) + 1;
- // }
- // }
- return board;
- }
- int** runPlacementPhase(int** placementBoard, Dimensions boardDimensions, Player* players, int numberOfPlayers, int penguinsPerPlayer) {
- // One round is one move of every player
- printf("It's time to place your penguins!\n");
- int roundsLeft = penguinsPerPlayer;
- while (roundsLeft != 0) {
- for (int i = 0; i < numberOfPlayers; i++) {
- Player currentPlayer = players[i];
- // Point point = askPlayerForPoint(currentPlayer);
- // placePenguin(placementBoard, point, currentPlayer);
- }
- roundsLeft--;
- }
- // Todo: Use the legacy code to finish the function @bjacak
- //while (penguins_placed < penguin_number * player_count) {
- // printf("PLAYER%d PLACE YOUR PENGUIN!\n", current_player + 1);
- // char placement_x;
- // int placement_y;
- // getchar();
- // scanf("%c", &placement_x);
- // placement_x = placement_x - 65;
- // getchar();
- // scanf("%d", &placement_y);
- // //check if possible
- // if (map[placement_y - 1][2 * placement_x] == 0 || placement_y - 1 >= rowsCount || placement_x >= columnsCount) {
- // while (map[placement_y - 1][2 * placement_x] == 0 || placement_y - 1 >= rowsCount || placement_x >= columnsCount) {
- // printf("NOT HERE!\nPLACE YOUR PENGUIN SOMEWHERE ELSE!\n");
- // getchar();
- // scanf("%c", &placement_x);
- // placement_x = placement_x - 65;
- // getchar();
- // scanf("%d", &placement_y);
- // }
- // }
- //
- // printf("%d\n", map[placement_y - 1][2 * placement_x]);
- // //
- // player_score[current_player] = player_score[current_player] + map[placement_y - 1][2 * placement_x];
- // //
- // map[placement_y - 1][2 * placement_x] = 0;
- // map[placement_y - 1][2 * placement_x + 1] = current_player + 1;
- // //
- // printf(" ");
- // for (int i = 0; i < columnsCount; i++) {
- // printf("%c ", i + 65);
- // }
- // printf("\n");
- // for (int i = 0; i < rowsCount; i++) {
- // if (i < 9) {
- // printf("0%d ", i + 1);
- // } else {
- // printf("%d ", i + 1);
- // }
- //
- // for (int j = 0; j < columnsCount; j++) {
- // printf("%d%d ", map[i][2 * j], map[i][2 * j + 1]);
- // }
- // printf("\n");
- // }
- //
- // current_player++;
- // if (current_player >= player_count) {
- // current_player = 0;
- // }
- //
- // penguins_placed++;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement