-
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathcode.py
More file actions
156 lines (121 loc) · 3.23 KB
/
code.py
File metadata and controls
156 lines (121 loc) · 3.23 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
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
import numpy as np
import random
import time
import matplotlib.pyplot as plt
def create_board():
x = np.zeros((3, 3))
return x
board = create_board()
def place(board, player, position):
if board[position] == 0:
board[position] = player
return board
def possibilities(board):
# it gives the indices wherever there are zeros in the form of 2 seperate
# arrays in a tuple.
# (array([0, 0, 1, 1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2, 0, 1, 2]))
y = np.where(board == 0)
y = np.array(y)
z = list(map(tuple, np.transpose(y)))
# transpose it to get [0,1],[0,2],[1,0]
# ... and so on. Then using map function convert each of them to tuple.
# And keep it totally as a list.
return z
def random_place(board, player):
position = random.choice(possibilities(board))
# print (position)
return place(board, player, position)
board = random_place(board, 2)
def row_win(board, player):
for i in range(3):
if(player == board[i][0]):
if((player == board[i][1]) and (player == board[i][2])):
print("True")
else:
print("False")
else:
print("False")
row_win(board, 1)
def col_win(board, player):
for i in range(3):
if(player == board[0][i]):
if((player == board[1][i]) and (player == board[2][i])):
print("True")
else:
print("False")
else:
print("False")
col_win(board, 1)
def diag_win(board, player):
z = 0
for i in range(3):
if(player == board[i][i]):
z += 1
if(z == len(board)):
print("True")
else:
print("False")
diag_win(board, 1)
# Evaluate
def evaluate(board):
winner = 0
for player in [1, 2]:
# Check if `row_win`, `col_win`, or `diag_win` apply.
# if so, store `player` as `winner`.
if (row_win(board, player) is True or col_win(board, player) is True):
winner = player
if(diag_win(board, player) is True):
winner = player
if np.all(board != 0) and winner == 0:
winner = -1
return winner
evaluate(board)
# play_game()
def play_game():
board = create_board()
if(0 in board):
player = 1
random_place(board, player)
z = evaluate(board)
player = 2
return z
# plot
R = 1000
ty = []
r = []
s = time.time()
for i in range(R):
z = play_game()
r.append(z)
e = time.time()
c = e-s
print(c)
x = [r[j] for j in range(R)]
plt.hist(x)
plt.show()
# play strategic
def play_strategic_game():
board, winner = create_board(), 0
board[1, 1] = 1
while winner == 0:
for player in [2, 1]:
# use `random_place` to play a game, and store as `board`.
board = random_place(board, player)
# use `evaluate(board)`, and store as `winner`.
winner = evaluate(board)
if winner != 0:
break
return winner
play_strategic_game()
# plotting play-strategic-game
# write your code here!
R = 1000
s = time.time()
z = []
for i in range(R):
k = play_strategic_game()
z.append(k)
e = time.time()
x = [z[j] for j in range(R)]
plt.hist(x)
plt.show()