forked from renenperlman/Software-Project-Ex.-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraughts.c
More file actions
115 lines (103 loc) · 2.13 KB
/
Draughts.c
File metadata and controls
115 lines (103 loc) · 2.13 KB
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
#include "Draughts.h"
#include <limits.h>
int main()
{
init_board(board);
print_board(board);
print_message(WRONG_MINIMAX_DEPTH);
perror_message("TEST");
return 0;
}
void print_line(){
int i;
printf(" |");
for (i = 1; i < BOARD_SIZE*4; i++){
printf("-");
}
printf("|\n");
}
void print_board(char board[BOARD_SIZE][BOARD_SIZE])
{
int i,j;
print_line();
for (j = BOARD_SIZE-1; j >= 0 ; j--)
{
printf((j < 9 ? " %d" : "%d"), j+1);
for (i = 0; i < BOARD_SIZE; i++){
printf("| %c ", board[i][j]);
}
printf("|\n");
print_line();
}
printf(" ");
for (j = 0; j < BOARD_SIZE; j++){
printf(" %c ", (char)('a' + j));
}
printf("\n");
}
void init_board(char board[BOARD_SIZE][BOARD_SIZE]){
int i,j;
for (i = 0; i < BOARD_SIZE; i++){
for (j = 0; j < BOARD_SIZE; j++){
if ((i + j) % 2 == 0){
if (j <= 3){
board[i][j] = WHITE_M;
}
else if (j >= 6){
board[i][j] = BLACK_M;
}
else{
board[i][j] = EMPTY;
}
}
else{
board[i][j] = EMPTY;
}
}
}
}
int max(int depth, char player, board_t board, steps** bestStep){
if (depth == 0){
return score(board);
}
char otherPlayer = ('b' + 'w' - player);
int currMin = INT_MAX;
int stepScore;
linkedList moves = setMoveList(otherPlayer, board);
listNode* node = moves.first;
while (node!=NULL)
{
stepScore = score(min(depth - 1, otherPlayer, moveDisc(*(steps*)node->data, player)));
if (stepScore < currMin){
currMin = stepScore;
*bestStep = (steps*)node->data;
}
}
return currMin;
}
int min(int depth, char player, board_t board, steps** bestStep){
if (depth == 0){
return score(board);
}
char otherPlayer = ('b' + 'w' - player);
int currMin = INT_MIN;
int stepScore;
linkedList moves = setMoveList(otherPlayer, board);
listNode* node = moves.first;
while (node != NULL)
{
stepScore = score(max(depth - 1, otherPlayer, moveDisc(*(steps*)node->data, player)));
if (stepScore > currMin){
currMin = stepScore;
*bestStep = (steps*)node->data;
}
}
return currMin;
}
steps* minmax(char player){
steps* bestStep = NULL;
max(minmaxDepth, player, board, &bestStep)
}
<<<<<<< HEAD
=======
>>>>>>> origin/master