-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
191 lines (158 loc) · 3.95 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "board.h"
#include "snake_and_ladder.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//Tiles Structure
struct tiles{
int player1;
int player2;
int snake_start;
int snake_end;
int ladder_start;
int ladder_end;
int coordinate;
};
typedef struct tiles Tiles;
//end of tiles
struct board{
Tiles game_board[10][10];
};
//creates a Board to play
Board *new_board(int snakes_array[2][SNAKE_NUM], int ladder_array[2][LADDER_NUM])
{
Board* new = malloc(sizeof(*new));
assert(new!=NULL);
//initializing all tiles with players as NULL snakes and NULL ladder NULL
//coordinates assigned
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
new->game_board[i][j].coordinate = i*10+j;
//tile_is_a_sanke ?
for(int k=0; k<SNAKE_NUM; k++)
{
if(new->game_board[i][j].coordinate == snakes_array[0][k])
{
new->game_board[i][j].snake_start = snakes_array[0][k];
new->game_board[i][j].snake_end = snakes_array[1][k];
break;
}
else
{
new->game_board[i][j].snake_start = 0;
new->game_board[i][j].snake_end = 0;
}
}
//tile is laddeR?
for(int k=0; k<LADDER_NUM; k++)
{
if(new->game_board[i][j].coordinate == ladder_array[0][k])
{
new->game_board[i][j].ladder_start = ladder_array[0][k];
new->game_board[i][j].ladder_end = ladder_array[1][k];
break;
}
else
{
new->game_board[i][j].ladder_start = 0;
new->game_board[i][j].ladder_end = 0;
}
}
//all tiles players are null
new->game_board[i][j].player1= 0;
new->game_board[i][j].player2= 0;
}
}
return new;
}
//access various members of board through abstraction
//checks whether there is a snake at the given cordinate
int is_snake(Board * board, int coord)
{
//code for i,j
int i=0,j=0;
j=coord%10;
i=(int)coord/10;
//code for i,j
if(coord == 0)
{
return 0;
}
if(board->game_board[i][j].snake_start == coord)
{
return 1;
}
return 0;
}
//checks whether there is a ladder at the given cordinate
int is_ladder(Board * board, int coord)
{
//code for i,j
int i=0,j=0;
j=coord%10;
i=(int)coord/10;
//code for i,j
if(coord == 0)
{
return 0;
}
if(board->game_board[i][j].ladder_start == coord)
{
return 1;
}
return 0;
}
//sets player value to correct coordinate after rolling dice
void set_player_coordinate(Board* board, int coord_prev,int coord_next, int player_num)
{
if(coord_next>99)
{
coord_next==coord_prev;
}
//code for i,j
int i=0,j=0;
j=coord_prev%10;
i=(int)coord_prev/10;
//code for i,j
if(player_num == 1)
{
board->game_board[i][j].player1=0;
}
else
{
board->game_board[i][j].player2=0;
}
//code for i,j
i=0,j=0;
j=coord_next%10;
i=(int)coord_next/10;
//code for i,j
if(player_num == 1)
{
board->game_board[i][j].player1=1;
}
else
{
board->game_board[i][j].player2=1;
}
}
//spits out end of the snkae on the given coordinate
int get_snake_tail(Board *board, int coordinate)
{
//code for i,j
int i=0,j=0;
j=coordinate%10;
i=(int)coordinate/10;
return board->game_board[i][j].snake_end;
}
//spits out end of the ladder at the given coordinate
int get_ladder_end(Board *board, int coordinate)
{
//code for i,j
int i=0,j=0;
j=coordinate%10;
i=(int)coordinate/10;
return board->game_board[i][j].ladder_end;
}