-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
128 lines (114 loc) · 4.91 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/******************************************************************************
* board.c: *
* Creates and displays the board of the game. *
* *
* Inserts snake's head and food. *
* *
* Functions: *
* board_size(), alloc_cont(), init_board(), disp_board(), *
* init_snake(), insert_food(), dealloc_cont() *
******************************************************************************/
#include "retro_snake_lib.h"
/******************************************************************************
* board_size: Returns the size of the table as a run time parameter. *
* If the size is less than 3 or not set, program terminates. *
******************************************************************************/
int board_size(const int argc, const int s)
{
if (argc != 2 || s <= 3) {
fprintf(stderr, "Size too small; program terminated.\n");
exit(EXIT_FAILURE);
}
return s;
}
/******************************************************************************
* alloc_cont: Allocates a continuous block of memory for the board. *
******************************************************************************/
void alloc_cont(char *** a, const int size)
{
int i;
*a = (char **) malloc(ROWS * sizeof(char *) + ROWS * COLS * sizeof(char));
if (*a == NULL) {
fprintf(stderr, "Error: malloc failed in alloc_cont\n");
exit(EXIT_FAILURE);
}
/* a[i][0] pointing to the first element of each row */
for (i = 0; i < ROWS; i++)
(*a)[i] = (char *) &(*a)[ROWS] + i * COLS;
}
/******************************************************************************
* init_board: Fills the board with borders and spaces. *
******************************************************************************/
void init_board(char ** a, const int size)
{
int i, j;
for (i = 0; i < ROWS; i++){
for (j = 0; j < COLS; j++){
if (i == 0 || i == ROWS - 1)
*(*(a + i) + j) = '-';
else if (j == 0 || j == COLS - 1)
*(*(a + i) + j) = '|';
else
*(*(a + i) + j) = ' ';
}
}
a[0][0] = ' ';
a[0][COLS - 1] = ' ';
a[ROWS - 1][0] = ' ';
a[ROWS - 1][COLS - 1] = ' ';
}
/******************************************************************************
* disp_board: Prints the board. *
******************************************************************************/
void disp_board(char ** a, const int size)
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++)
printf("%c", *(*(a + i) + j));
printf("\n");
}
printf("\n");
}
/******************************************************************************
* init_snake: Randomly places the head of the snake *
* and returns its position. *
******************************************************************************/
char * init_snake(char **a, const int size)
{
int x = 1 + rand() % (ROWS - 2);
int y = 1 + rand() % (COLS - 2);
*(*(a + x) + y) = '@';
return &a[x][y];
}
/******************************************************************************
* insert_food: Inserts 'X' (food) to a random location. *
* Returns the position of 'X'. *
* If max score is reached, win variable is set to 1 (true) *
* initiating the end of game. *
******************************************************************************/
char * insert_food(char ** a, const int size, int * score, int * win)
{
int x = 1;
int y = 0;
if ((*score + SCORE_OFFSET) != MAX_SCORE) {
while(a[x][y] != ' ') {
x = 1 + rand() % (ROWS - 2);
y = 1 + rand() % (COLS - 2);
}
*score = *score + SCORE_OFFSET;
a[x][y] = 'X';
return &a[x][y];
}
else {
*win = 1; /* If game is over, flag win turns true (1) */
}
return NULL;
}
/******************************************************************************
* dealloc_cont: Releases the memory allocated for the board *
******************************************************************************/
void dealloc_cont(char ** a)
{
free(a);
}