aboutsummaryrefslogtreecommitdiff
path: root/semestr-5/so/lista1/so21_lista_1/het.c
blob: 579abe4834bba1fb596ed5d3e506bfacc4968dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "csapp.h"

#define CONFLICT    0
#define NO_CONFLICT 1

static int ndselect(int n) {
  for (int column = 0; column < n; column++) {
    int pid = fork();
    if (pid == 0) {
      return column;
    }
    waitpid(pid, NULL, 0);
  }
  /* TODO: A loop is missing here that spawns processes and waits for them! */
  exit(0);
}

static int conflict(int x1, int y1, int x2, int y2) {
  return x1 == x2
    || y1 == y2
    || x1 + y1 == x2 + y2
    || x1 - y1 == x2 - y2;
}

static bool check_conflict(int size, int board[size], int column) {
  for (int i = 0; i < column; i++) {
    if (conflict(board[i], i, board[column], column) == 1) return CONFLICT;
  }
  return NO_CONFLICT;
}

static void print_line_sep(int size) {
  for (int i = 0; i < size; ++i) 
    printf("+---");
  printf("+\n");
}

static void print_board(int size, int board[size]) {
  for (int i = 0; i < size; ++i) {
    print_line_sep(size);
    for (int j = 0; j < size; ++j)
      printf("|%s", board[i] == j ? " ♕ " : "   ");
    printf("|\n");
  }
  print_line_sep(size);
  printf("\n");
}

int main(int argc, char **argv) {
  if (argc != 2)
    app_error("Usage: %s [SIZE]", argv[0]);

  int size = atoi(argv[1]);

  if (size < 3 || size > 9)
    app_error("Give board size in range from 4 to 9!");

  int board[size];

  /* TODO: A loop is missing here that initializes recursive algorithm. */
  for (int row = 0; row < size; row++) {
    int column = ndselect(size);
    board[row] = column;
    if (check_conflict(size, board, row) == CONFLICT) 
      exit(0);
  }

  print_board(size, board);

  return 0;
}