-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhomework2_week2.py
More file actions
160 lines (128 loc) · 3.74 KB
/
homework2_week2.py
File metadata and controls
160 lines (128 loc) · 3.74 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
157
158
159
160
#------------------------------------------------
# Homework 2 of course "Python for Research"
# HarvardX - PH526xEdX, EdX
#------------------------------------------------
#Exercise 1
import numpy as np
def create_board():
"""creates 3 x3 numpy array"""
board=np.zeros((3,3))
return(board)
#Exercise 2
def place(board,player,position):
""" Allows placement of player 1 or 2 on empty (corresponding to (0,0) position on board """
if board[position]==0 and (player==1 or player==2):
board[position]=player
return(board)
#Exercise 3
def possibilities(board):
"""" Find all positions (as tuples), where numpy array board is 0, returns tuples as list """
positions=np.array(np.where(board==0)).T #only arrays can be transposed, np.where returns tuple
list_of_tuples=list(map(tuple,positions))
return(list_of_tuples)
#Exercise 4
def random_place(board,player):
""" places a marker for the current player (1 or 2) at random among all positions with value 0 in board """
import random
selection=possibilities(board)
position=random.choice(selection)
board=place(board,player,position)
return(board)
#Exercise 5
board = create_board()
for j in range(3):
for player in [1,2]:
board=random_place(board,player)
print(board)
#Exercise 6
def row_win(board, player):
for i in range(board.shape[0]):
if (set(board[i,:]) == {player})==True:
return(True)
return(False)
row_win(board,1)
#Exercise 7
def col_win(board, player):
for i in range(board.shape[0]):
if (set(board[:,i]) == {player})==True:
return(True)
return(False)
#Exercise 8
def diag_win(board, player):
for i in range(board.shape[1]):
if (set(np.diagonal(board)) == {player})==True:
return(True)
return(False)
diag_win(board, 1)
#Exercise 9
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)==True or col_win(board, player)==True or
diag_win(board, player)==True):
winner=player
return(winner)
if np.all(board != 0) and winner == 0:
winner = -1
return winner
evaluate(board)
board
player=[1,2]
results=player+[-1]
board=create_board()
results
player
for j in range(5): #better: while winner==0
for i in player:
board=random_place(board,i)
if (evaluate(board) in results)==True:
print([board,evaluate(board)])
break
#Exercise 10
def play_game():
player=[1,2]
results=player+[-1]
board=create_board()
#for j in range(5):
while(evaluate(board)==0):
for i in player:
board=random_place(board,i)
if (evaluate(board) in results)==True:
return(evaluate(board))
play_game()
#Exercise 11
import time as time
import matplotlib.pyplot as plt
start=time.time()
winners=[]
for i in range(1000):
winners.append(play_game())
stop=time.time()
print(stop-start)
plt.hist(winners); #player one wins more often
plt.show(winners);
#Exercise 12
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:
return(winner)
break
play_strategic_game()
#Exercise 13
start=time.time()
winners=[]
for i in range(1000):
winners.append(play_strategic_game())
stop=time.time()
print(stop-start)
plt.hist(winners); #player one wins more often
plt.show();