-
Notifications
You must be signed in to change notification settings - Fork 0
/
Devil.py
120 lines (100 loc) · 4.4 KB
/
Devil.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
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
from NeuralNetwork import NeuralNetwork
import numpy as np
import random
class Devil:
def __init__(self, sides, trained):
self.blocks = []
self.sides = sides
self.identity = np.arange(self.sides ** 2).reshape(self.sides, self.sides)
input_nodes = int(sides ** 2)
hidden_nodes = 160
output_nodes = int(sides ** 2)
learning_rate = 0.2
if not trained:
weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
else:
devil_who = open("Files/devil_who.csv", 'r')
devil_who_read = devil_who.readlines()
devil_who.close()
devil_wih = open("Files/devil_wih.csv", 'r')
devil_wih_read = devil_wih.readlines()
devil_wih.close()
weight_wih = np.asfarray([line.split(',') for line in devil_wih_read])
weight_who = np.asfarray([line.split(',') for line in devil_who_read])
self.consciousness = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)
def get_blocks(self):
return self.blocks
def reset(self):
self.blocks = []
def god_place(self, place):
self.blocks.append(place)
def place_block(self, board):
turn = np.argmax(self.consciousness.query(board))
self.blocks.append(turn)
return
def train(self, has_won):
if has_won is "devil":
self.consciousness.train(True)
if has_won is "angel":
self.consciousness.train(False)
self.consciousness.reset()
def get_wih(self):
return self.consciousness.wih
def get_who(self):
return self.consciousness.who
def random_place_block(self, angel_pos):
random_dir = random.randrange(0, 8)
random_place = -1
# check_layer = self.check_on_out_layer(angel_pos)
# new_devil_pos = (angel_pos + check_layer)
# if not check_layer == 0 and 0 <= new_devil_pos < self.sides ** 2 \
# and new_devil_pos not in self.blocks:
# self.blocks.append(new_devil_pos)
# return
# for pos in self.determine_quad(angel_pos):
# if (angel_pos + pos) >= 0 and (angel_pos + pos) < self.sides ** 2 and (angel_pos + pos) \
# not in self.blocks:
# self.blocks.append(angel_pos + pos)
# print(str(angel_pos) + " " + str(self.blocks))
# return
while random_place < 0 or random_place >= self.sides ** 2:
if random_dir == 0:
random_place = angel_pos - self.sides
if random_dir == 1:
random_place = angel_pos + 1
if random_dir == 2:
random_place = angel_pos + self.sides
if random_dir == 3:
random_place = angel_pos - 1
if random_dir == 4:
random_place = angel_pos - self.sides - 1
if random_dir == 5:
random_place = angel_pos + self.sides - 1
if random_dir == 6:
random_place = angel_pos - self.sides + 1
if random_dir == 7:
random_place = angel_pos + self.sides + 1
random_dir = random.randrange(0, 4)
self.blocks.append(random_place)
return
def check_on_out_layer(self, angel_pos):
if (angel_pos - 1) % self.sides == 0:
return -1
elif (angel_pos + 1) % self.sides == self.sides - 2:
return 1
elif self.sides + 1 <= (angel_pos - self.sides) <= 2 * self.sides - 2:
return -self.sides
elif self.sides ** 2 - 2 * self.sides + 1 <= (angel_pos + self.sides) <= self.sides ** 2 - self.sides - 2:
return self.sides
return 0
# def determine_quad(self, angel_pos):
# if angel_pos in self.identity[:int(self.sides / 2), :int(self.sides / 2)]:
# return [-1, -self.sides]
# elif angel_pos in self.identity[int(self.sides / 2):self.sides, :int(self.sides / 2)]:
# return [self.sides, -1]
# elif angel_pos in self.identity[:int(self.sides / 2), int(self.sides / 2):self.sides]:
# return [-self.sides, 1]
# else:
# return [1, self.sides]