Sha256: b66f035051b8ddf40f25d916682f0e4f00644140e0adcd58f23a7058d7f74e31

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

#include "minesweeper.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static void set_single_field(char **minefield, const size_t row,
                             const size_t col)
{
   if (minefield[row][col] == '*')
      return;

   const char c = minefield[row][col];
   minefield[row][col] = (c == ' ') ? '1' : c + 1;
}

static void increment_neighbors(char **minefield, const size_t row_count,
                                const size_t col_count, const size_t row,
                                const size_t col)
{
   const size_t from_row = row ? row - 1 : row;
   const size_t to_row = (row + 1 < row_count) ? row + 1 : row;
   const size_t from_col = col ? col - 1 : col;
   const size_t to_col = (col + 1 < col_count) ? col + 1 : col;

   for (size_t i = from_row; i <= to_row; i++) {
      for (size_t k = from_col; k <= to_col; k++)
         set_single_field(minefield, i, k);
   }
}

char **annotate(const char **minefield, const size_t rows)
{
   if (minefield == NULL || *minefield == NULL || rows == 0)
      return NULL;

   const size_t cols = strlen(minefield[0]);

   char **annotated = malloc(sizeof(char *) * rows);
   if (annotated == NULL) {
      fprintf(stderr, "Memory error!\n");
      return NULL;
   }

   char *all_rows = malloc(rows * (cols + 1));
   if (all_rows == NULL) {
      fprintf(stderr, "Memory error!\n");
      free(annotated);
      return NULL;
   }

   for (size_t i = 0; i < rows; i++) {
      annotated[i] = &all_rows[i * (cols + 1)];
      strcpy(annotated[i], minefield[i]);
   }

   for (size_t i = 0; i < rows; i++) {
      for (size_t k = 0; k < cols; k++) {
         if (minefield[i][k] == '*')
            increment_neighbors(annotated, rows, cols, i, k);
      }
   }

   return annotated;
}

void free_annotation(char **annotation)
{
   free(annotation[0]);
   free(annotation);
}

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.179 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.178 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.177 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.176 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.175 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.174 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.173 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.172 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.171 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.170 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.169 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.167 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.166 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.165 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.164 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.163 tracks/c/exercises/minesweeper/src/example.c
trackler-2.2.1.162 tracks/c/exercises/minesweeper/src/example.c