-
Notifications
You must be signed in to change notification settings - Fork 8
/
othello_question.c
332 lines (282 loc) · 7.5 KB
/
othello_question.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
// global variables
char board[SIZE][SIZE];
int white, black, free_cell = 64;
// functions
void gameloop();
void help();
void create_board();
void draw();
void insert(char player, char row, char column);
int occupied(char row, char column);
void reset();
int is_placeable(char player, char row, char column);
void show_placeable(char player);
void apply_changes(char player, char opponent, char row, char column);
void update_scores();
void display_scores();
void gameover();
void gameloop()
{
help();
create_board();
int turn = 0;
while(free_cell != 0)
{
// show placeable positions on board
show_placeable((turn % 2) == 0 ? 'B' : 'W');
draw();
// display scores
update_scores();
display_scores();
printf("player %d its your turn\n",(turn % 2) + 1);
char block[2];
printf("choose a block : ");
scanf("%s", &block);
char player, oponent;
player = (turn % 2) == 0 ? 'W' : 'B';
oponent = player == 'W' ? 'B' : 'W';
// if the cell is empty and placeable insert
if (occupied(block[0], block[1]) == 0)
{
insert(player, block[0], block[1]);
apply_changes(player, oponent, block[0], block[1]);
turn++;
}
else
{
printf("wrong move! try again.\n");
}
// remove the * from board
reset();
}
gameover();
}
/*
* prints a help message for new players
*/
void help()
{
char *text = "The object of the game Othello is to win!\n\nThe game gets its name from Shakespeare's Othello, who kept changing\nfrom bad to good and back again --from white to black, back and forth.\n. There are two\nplayers: white and black. White goes first, by placing a piece in some\nempty square (do this by clicking the mouse in the square in which you\n want to place a piece). However, you can't place a white piece in just\nany square, but only in one that satisfies the following condition:\n\n In at least one direction (vertical, horizontal, or diagonal) emanating\n from the square, there is a sequence of one or more black pieces\n followed by a white piece.\n\nWhen a white piece is placed in such a square, all the black pieces\nmentioned in this condition become white --in all directions. Try it!\n\nAfter a white piece is placed, the other player places a black piece in the\nsame manner (with the roles of white and black interchanged). Then it's\nwhite's turn again, an so it goes.\n\nA player who cannot place a piece loses their turn. If no player can play,\nthe game is over, and the player with the most pieces wins.";
printf("%s\npress any key to start",text);
getchar();
}
void create_board()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
board[i][j] = '_';
}
}
insert('W', '4', 'D');
insert('W', '5', 'E');
insert('B', '4', 'E');
insert('B', '5', 'D');
}
void draw()
{
// row names display
printf(" ");
for (int i = 0; i < SIZE; i++)
{
printf(" %c ", i+65);
}
printf("\n");
// board display
for (int i = 0; i < SIZE; i++)
{
// column names display
printf(" %i ",i+1);
for (int j = 0; j < SIZE; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
}
void insert(char player, char row, char column)
{
// change ascii code to places in matrix
// its 49 and not 48 becaus arrays start with 0 not 1
board[row - 49][column - 65] = player;
}
/*
* if the given block is empty return zero
* else return 1
*/
int occupied(char row, char column)
{
return board[row - 49][column - 65] == '*' ? 0 : 1;
}
/*
* return true if the given block is near other B/W blocks
* else return false
*/
int is_placeable(char player, char row, char column)
{
row = row + 48;
column = column + 65;
if (board[row - 48][column - 66] == player ||
board[row - 48][column - 64] == player ||
board[row - 49][column - 65] == player ||
board[row - 47][column - 65] == player)
{
return 1;
}
else
{
return 0;
}
}
/*
* draw an '*' in places where you can have your next move
*/
void show_placeable(char player)
{
//complete this function.
//use "is_placeable" function
}
void reset()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] == '*')
{
board[i][j] = '_';
}
}
}
}
/*
* after inserting a point for your next move
* this function apply changes in the game
* and flip color of peaces which have the condition
*/
void apply_changes(char player, char opponent, char row, char column)
{
int i = row - 49, j = column-65;
int x = i, y = j;
if (i - 1 >= 0 && j - i >= 0 && board[i - 1][j - 1] == opponent)
{
i--;
j--;
while(i > 0 && j > 0 && board[i][j] == opponent)
{
i--;
j--;
}
if(i >= 0 && j >= 0 && board[i][j] == player)
{
while(i != x - 1 && j != y - 1)
board[++i][++j]=player;
}
}
i = x;
j = y;
if(i - 1 >= 0 && board[i - 1][j] == opponent){
i--;
while(i > 0 && board[i][j] == opponent)
i--;
if(i >= 0 && board[i][j] == player)
{
while(i != x - 1)
board[++i][j]=player;
}
}
i = x;
if(i - 1 >= 0 && j + 1 <= 7 && board[i - 1][j + 1] == opponent){
i--;
j++;
while(i > 0 && j < 7 && board[i][j] == opponent)
{
i--;
j++;
}
if(i >= 0 && j <= 7 && board[i][j] == player)
{
while(i != x - 1 && j != y + 1)
board[++i][--j] = player;
}
}
i = x;
j = y;
if(j - 1 >= 0 && board[i][j - 1] == opponent){
j--;
while(j > 0 && board[i][j] == opponent)
j--;
if(j >= 0 && board[i][j] == player)
{
while(j != y - 1)
board[i][++j] = player;
}
}
j = y;
if(j + 1 <= 7 && board[i][j + 1] == opponent){
j++;
while(j < 7 && board[i][j] == opponent)
j++;
if(j <= 7 && board[i][j] == player)
{
while(j != y + 1)
board[i][--j] = player;
}
}
j = y;
if(i + 1 <= 7 && j - 1 >= 0 && board[i + 1][j - 1] == opponent){
i++;
j--;
while(i < 7 && j > 0 && board[i][j] == opponent)
{
i++;
j--;
}
if(i <= 7 && j >= 0 && board[i][j] == player)
{
while(i != x + 1 && j != y - 1)
board[--i][++j] = player;
}
}
i = x;
j = y;
//==================================
// complete two other conditions.
//==================================
// complete two other conditions.
}
void update_scores()
{
// reset scores
black = 0, white = 0, free_cell = 64;
// complete ................
}
void display_scores()
{
printf(" BLACK %d - WHITE %d\n", black, white);
}
void gameover()
{
if (black > white)
{
printf("BLACK TEAM WINS!\n");
}
else if (black < white)
{
printf("WHITE TEAM WINS!\n");
}
else
{
printf("DRAW!\n");
}
display_scores();
}
int main()
{
gameloop();
return 0;
}