-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
88 lines (71 loc) · 1.94 KB
/
board.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include "board.h"
#define BOARD_BYTES sizeof(pid_t) * BOARD_ROWS *BOARD_COLUMNS
static int board_shm_id;
static pid_t *board_ptr;
//Inizializzazione della board
int initialize_board()
{
//Inizializzazione shared memory segment della boards
board_shm_id = shmget(IPC_PRIVATE, BOARD_BYTES, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
if (board_shm_id == -1)
return -1;
//Collegamento al shared memory segment della board
board_ptr = shmat(board_shm_id, NULL, 0);
if (board_ptr == (pid_t *)-1)
return -1;
//Setta a 0 ogni byte della memoria allocata alla board
memset(board_ptr, 0, BOARD_BYTES);
return 0;
}
//Chiusura della boards
int teardown_board()
{
if (board_ptr != NULL && board_ptr != (pid_t *)-1)
{
//Scollegamento al shared memory segment della board
if (shmdt(board_ptr) == -1)
return -1;
//Cancellando lo shared memory segment
if (shmctl(board_shm_id, IPC_RMID, NULL) == -1)
return -1;
}
return 0;
}
//Getter posizione della cella
pid_t board_get(pos_t position)
{
return board_ptr[position.y * BOARD_COLUMNS + position.x];
}
//Setter posizione della cella
void board_set(pos_t position, pid_t value)
{
board_ptr[position.y * BOARD_COLUMNS + position.x] = value;
}
//Visualizza la board
void display_board()
{
printf("┌");
for (int i = 0; i < BOARD_ROWS; i++)
printf("-");
printf("┐\n");
for (int i = 0; i < BOARD_ROWS; i++)
{
printf("|");
for (int j = 0; j < BOARD_COLUMNS; j++)
{
pos_t position = {i, j};
pid_t pid = board_get(position);
char *string = pid == 0 ? " " : "#";
printf("%s", string);
}
printf("|\n");
}
printf("└");
for (int i = 0; i < BOARD_ROWS; i++)
printf("-");
printf("┘\n");
}