-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepth_limited_search.py
84 lines (65 loc) · 1.85 KB
/
depth_limited_search.py
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
import curses, time, copy, UI_elements as UI
from checks_counts import *
from math import inf
from algo import *
def get_move(turn, n, arr, depth_limit):
alpha = -inf
beta = inf
p = NextWithPruning(turn, n, arr, alpha, beta, 0, depth_limit)
return p
n = int(input("Enter n for size of board : "))
player = int(input("Enter 1 if you want to play as player 1 otherwise enter 2 : "))
[sc, win, h, w ] = UI.initialise_screen(n)
flag = -1
played = 0
if(player == 2):
played = 1
arr = [[-1]*n for i in range(n)]
# for i in range(n*n):
# start_time = time.time()
# p = get_move(i%2, n, arr, 3)
# print("--- %s seconds ---" % (time.time() - start_time))
# print(i%2, p)
# arr[p[1]][p[2]] = i%2
start_time = time.time()
p = NextWithPruning(0, n, arr, -inf, inf, 0, 6)
print("--- %s seconds ---" % (time.time() - start_time))
print(p)
while True:
win.border(0)
win.timeout(100)
curses.noecho()
next_key = win.getch()
if(next_key == curses.KEY_MOUSE and played == 0):
_, mx, my, _, _ = curses.getmouse()
arr_x = int((n)*my/h)
arr_y = int((n)*mx/w)
arr[arr_x][arr_y] = 0
win.addstr(int(((2*arr_x + 1)*h)/(2*n)),int(((2*arr_y + 1)*w)/(2*n)),'X')
played = 1
if(next_key == 55):
break
c = check(arr,n)
if(c == 0):
break
if(played == 1):
p = NextWithPruning(1, n, arr, -inf, inf,0, 2*n)
arr[p[1]][p[2]] = 1
win.addstr(int(((2*p[1] + 1)*h)/(2*n)),int(((2*p[2] + 1)*w)/(2*n)),'0')
played = 0
c = check(arr,n)
if(c == 1):
flag = 0
break
## Checking conditions for winning or losing or draw
if(flag==0):
s = "You Lost!"
elif(flag==1):
s = "You Won!"
elif(flag==-1):
s = "Draw!"
## Outputting Result
sc.addstr(int(h/2),int(w/2),s)
sc.refresh()
time.sleep(2)
curses.endwin()