-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic.pinn
162 lines (148 loc) · 3.26 KB
/
tic.pinn
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
EMPTY := 0;
TIE := 3;
PLAYER_A := 1;
PLAYER_B := 2;
SIZE := 3;
var rHashMap [map]int;
func rHash(board [SIZE][SIZE]int, player int) int {
rt := 0;
var v int;
var x int;
var y int;
for x = range 0:SIZE {
for y = range 0:SIZE {
v = board[x][y];
rt *= SIZE;
rt += v;
}
}
rt *= 2;
rt += player - 1;
return rt;
}
func printBoard (board [SIZE][SIZE]int) {
var y int;
var x int;
for x = range 0:SIZE {
for y = range 0:SIZE {
print (board[x][y]);
}
println("");
}
println ("---");
}
func full (board [SIZE][SIZE]int) bool {
var x int;
var y int;
for x = range 0:SIZE {
for y = range 0:SIZE {
if board[x][y] == EMPTY {
return false;
}
}
}
return true;
}
func line (board [SIZE][SIZE]int, x int, y int, dx int, dy int) int {
comp := board[x][y];
if comp == EMPTY {
return TIE;
}
while x + dx < SIZE && y + dy < SIZE {
x += dx;
y += dy;
if board[x][y] != comp {
return TIE;
}
}
return comp;
}
func winner (board [SIZE][SIZE]int) int {
var i int;
var rt int;
for i = range 0:SIZE {
rt = line(board, i, 0, 0, 1);
if rt != TIE
return rt;
rt = line(board, 0, i, 1, 0);
if rt != TIE
return rt;
}
rt = line(board, 0, 0, 1, 1);
if rt != TIE
return rt;
rt = line(board, 0, SIZE - 1, 1, -1);
return rt;
}
func opposite (x int) int { return x == PLAYER_A ? PLAYER_B : PLAYER_A; }
func minimax (player int, board [SIZE][SIZE]int, depth int) int
{
marker++;
// if marker % 10 == 0 {
println(";", marker);
println("");
println(player);
println("d", depth);
println("");
printBoard(board);
// }
var result int;
best := opposite(player);
result = winner(board);
if result != TIE {
return result;
}
if full(board) {
return TIE;
}
var x int;
var y int;
var strRHash string;
for x = range 0:SIZE {
for y = range 0:SIZE {
if board[x][y] == EMPTY {
board[x][y] = player;
// strRHash = stringValue(rHash(board, player));
// if rHashMap[strRHash] > 0 {
// result = rHashMap[strRHash];
// } else {
result = minimax(opposite(player), board, depth + 1);
// rHashMap[strRHash] = result;
// }
if result == player {
return player;
}
if result == TIE {
best = TIE;
}
board[x][y] = EMPTY;
}
}
}
return best;
}
func main() {
var board [SIZE][SIZE]int;
board [0][0] = PLAYER_A;
board [0][1] = PLAYER_A;
board [0][2] = PLAYER_B;
board [1][0] = PLAYER_A;
println("Initial board");
printBoard(board);
result := minimax (PLAYER_A, board, 0);
resultString := "";
match result {
when TIE:
resultString = "Tie.";
when PLAYER_A:
resultString = "Player A.";
when PLAYER_B:
resultString = "Player B.";
}
println (resultString);
}
marker := 0;
time := 0;
rHashMap = {};
main();
print(marker);