Skip to content

Commit

Permalink
Turned board into class
Browse files Browse the repository at this point in the history
  • Loading branch information
Panda7777 committed Jun 30, 2017
1 parent 4869e1e commit 17fcd72
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 66 deletions.
58 changes: 58 additions & 0 deletions Angel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from NeuralNetwork import NeuralNetwork
import numpy as np


class Angel:
def __init__(self, sides):
self.sides = sides
self.position = int(sides / 2) * sides + int(sides / 2)
# initially in the center
self.moves = [self.position]

self.escaped = False

input_nodes = sides ** 2
hidden_nodes = 200
output_nodes = 4
learning_rate = 0.1
weight_wih = np.random.normal(0.0, pow(hidden_nodes, -0.5), (hidden_nodes, input_nodes))
weight_who = np.random.normal(0.0, pow(output_nodes, -0.5), (output_nodes, hidden_nodes))
self.consciousness = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)

def has_escaped(self):
return self.escaped

def get_position(self):
if self.escaped:
return None
return self.position

def get_moves(self):
return self.moves

def is_trapped(self, all_devils):
if self.position - self.sides in all_devils.get_positions() and self.position + self.sides in \
all_devils.get_positions() and self.position - 1 in all_devils.get_positions() and self.position + 1 \
in all_devils.get_positions():
return True
return False

def angel_move(self, all_devils, move):
if (self.position + move) in all_devils.get_positions():
return False
if self.position + move < 0 or self.position + move > self.sides ** 2 - 1:
self.escaped = True
if (self.position % self.sides == self.sides - 1 and (self.position + move) % self.sides == 0) or (
self.position % self.sides == 0 and (self.position + move) % self.sides == self.sides - 1):
self.escaped = True
self.position += move
self.moves.append(self.position)
return True

def train(self, boards, has_won):
win_error = 1 if has_won else 0
for i in range(len(boards)):
reinforced_error = np.array([None, None, None, None]).reshape(4, 1)
reinforced_error[self.moves[i]] = win_error
self.consciousness.train(boards[i], reinforced_error)
118 changes: 52 additions & 66 deletions Board.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
import pygame
from pygame.locals import *


class Angel:
def __init__(self, sides):
self.sides = sides
self.position = int(sides / 2) * sides + int(sides / 2)
self.escaped = False

def has_escaped(self):
return self.escaped

def get_position(self):
return self.position

def angel_move(self, all_devils, move):
for barrier in all_devils.get_positions():
if barrier == (self.position + move):
return
if self.position + move < 0 or self.position + move > self.sides ** 2 - 1:
self.escaped = True
return
if (self.position % self.sides == self.sides - 1 and (self.position + move) % self.sides == 0) or (
self.position % self.sides == 0 and (self.position + move) % self.sides == self.sides - 1):
self.escaped = True
return
self.position += move


class Devils:
def __init__(self):
self.positions = []

def get_positions(self):
return self.positions

def add_devil(self, angel_position, position):
if angel_position == position:
return
self.positions.append(position)
from Angel import Angel
from Devil import Blocks


class Board:
Expand All @@ -50,7 +13,6 @@ def __init__(self, margin, sides):
pygame.init()
self.window = pygame.display.set_mode((self.size, self.size))
self.canvas = self.window.copy()
self.running = True

self.black = (0, 0, 0, 255)
self.white = (255, 255, 255)
Expand All @@ -59,55 +21,79 @@ def __init__(self, margin, sides):
self.red = (255, 0, 0)

self.angel = Angel(self.sides)
self.devils = Devils()

def rect_equ(self, position):
return (
(position % self.sides) * self.margin, int(position / self.sides) * self.margin, self.margin, self.margin)

def is_running(self):
return self.running
self.blocks = Blocks()
self.winner = None

def run(self):
def machinery(self):
self.window.fill(self.white)
if self.angel.has_escaped():
self.winner = "angel"
if self.angel.is_trapped(self.blocks):
self.winner = "devil"
# grid
for side in range(0, self.size, self.margin):
pygame.draw.lines(self.window, self.gray, False, ((side, 0), (side, self.size)))
pygame.draw.lines(self.window, self.gray, False, ((0, side), (self.size, side)))

# angel
if not self.angel.has_escaped():
# noinspection PyTypeChecker
pygame.draw.rect(self.window, self.silver, self.rect_equ(self.angel.get_position()))
pygame.draw.rect(self.window, self.silver, self.rect_equ(self.angel.get_position()))

# devil barriers
for devil in self.devils.get_positions():
for devil in self.blocks.get_positions():
pygame.draw.rect(self.window, self.red, (self.rect_equ(devil)))

def rect_equ(self, position):
return (
(position % self.sides) * self.margin, int(position / self.sides) * self.margin, self.margin, self.margin)

def representation(self):
board_rep = []
for i in range(self.sides ** 2):
if i == self.angel.get_position():
board_rep.append(1)
elif i in self.blocks.get_positions():
board_rep.append(0.5)
return board_rep

def get_angel(self):
return self.angel

def get_winner(self):
return self.winner

def train_angel(self):
self.machinery()

angel.query()
return True

def run(self):
self.machinery()

for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
x_box = int(pos[0] / self.margin)
y_box = int(pos[1] / self.margin)
box_index = x_box + self.sides * y_box
self.devils.add_devil(self.angel.get_position(), box_index)
self.blocks.add_block(self.angel.get_position(), box_index)

if e.type == pygame.KEYDOWN:
if e.key == K_RIGHT:
self.angel.angel_move(self.devils, 1)
move = self.angel.angel_move(self.blocks, 1)
if not move:
self.winner = "devil"
if e.key == K_LEFT:
self.angel.angel_move(self.devils, -1)
move = self.angel.angel_move(self.blocks, -1)
if not move:
self.winner = "devil"
if e.key == K_UP:
self.angel.angel_move(self.devils, -self.sides)
move = self.angel.angel_move(self.blocks, -self.sides)
if not move:
self.winner = "devil"
if e.key == K_DOWN:
self.angel.angel_move(self.devils, self.sides)
if e.type == pygame.QUIT:
self.running = False
move = self.angel.angel_move(self.blocks, self.sides)
if not move:
self.winner = "devil"

pygame.display.update()


if __name__ == "__main__":
board = Board(60, 11)
while board.is_running():
board.run()
23 changes: 23 additions & 0 deletions Devil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import random


class Blocks:
def __init__(self):
self.positions = []

def get_positions(self):
return self.positions

def add_block(self, angel_position, position):
if angel_position == position:
return
self.positions.append(position)


class Devil:
def __init__(self, sides):
self.blocks = Blocks()
self.sides = sides

def place_block(self, angel):
random_place = random.randrange(0, self.sides ** 2)
10 changes: 10 additions & 0 deletions Main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from Board import Board

board = Board(60, 9)
while True:
board.run()
winner = board.get_winner()
if winner is not None:
print(winner)
break
# for i in range(100):
54 changes: 54 additions & 0 deletions NeuralNetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy
import scipy.special


class NeuralNetwork:
def __init__(self, input_node, hidden_node, output_node, initial_wih, initial_who, learn_rate):
self.i_nodes = input_node
self.h_nodes = hidden_node
self.o_nodes = output_node

self.wih = initial_wih
self.who = initial_who

self.lr = learn_rate
self.activation_function = lambda x: scipy.special.expit(x)
pass

def train(self, inputs_list, targets_list):
input_array = numpy.array(inputs_list, ndmin=2).T
target_array = numpy.array(targets_list, ndmin=2).T

hidden_inputs = numpy.dot(self.wih, input_array)
hidden_outputs = self.activation_function(hidden_inputs)

final_inputs = numpy.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)

for i in range(self.o_nodes):
if target_array[i] is None:
target_array[i] = final_outputs[i]
output_errors = target_array - final_outputs
hidden_errors = numpy.dot(self.who.T, output_errors)

self.who += self.lr * (output_errors * final_outputs * (1.0 - final_outputs)) * numpy.transpose(hidden_outputs)
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),
numpy.transpose(input_array))
pass

def query(self, input_list):
input_list = numpy.array(input_list, ndmin=2).T

hidden_inputs = numpy.dot(self.wih, input_list)
hidden_outputs = self.activation_function(hidden_inputs)

final_inputs = numpy.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)

return final_outputs

def return_wih(self):
return self.wih

def return_who(self):
return self.who

0 comments on commit 17fcd72

Please sign in to comment.