Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=UTF-8
- import json, copy, pickle
- from pybrain.tools.shortcuts import buildNetwork
- from pybrain.structure import TanhLayer
- from pybrain.datasets import SupervisedDataSet
- from pybrain.supervised.trainers import RPropMinusTrainer
- # 1 2 3 4 5 6 7 8
- pole = [[1, 0, 1, 0, 1, 0, 1, 0], # 1
- [0, 1, 0, 1, 0, 1, 0, 1], # 2
- [1, 0, 1, 0, 1, 0, 1, 0], # 3
- [0, 0, 0, 0, 0, 0, 0, 0], # 4
- [0, 0, 0, 0, 0, 0, 0, 0], # 5
- [0, 2, 0, 2, 0, 2, 0, 2], # 6
- [2, 0, 2, 0, 2, 0, 2, 0], # 7
- [0, 2, 0, 2, 0, 2, 0, 2]] # 8
- filename = "learning.txt"
- fileNet = "Net.1"
- def addData(input, result):
- learning = readLearning()
- learning.append(input)
- learning.append(result)
- writeLearning(learning)
- def writeLearning(learning):
- file = open(filename, 'w')
- jlearning = json.dumps(learning)
- file.write(jlearning)
- file.close()
- def readLearning():
- file = open(filename)
- jlearning = file.read()
- learning = json.loads(jlearning)
- file.close()
- return learning
- def saveNet(net):
- file = open(fileNet, 'w')
- pickle.dump(net, file)
- file.close()
- def loadNet():
- file = open(fileNet, 'w')
- net = pickle.load(file)
- file.close()
- return net
- def train():
- learning = readLearning()
- net = buildNetwork(65, 10, 4)
- ds = SupervisedDataSet(65, 4)
- for i in range(0, len(learning), 2):
- ds.addSample(learning[i],learning[i+1])
- trainer = RPropMinusTrainer(net)
- trainer.setData(ds)
- trainer.trainUntilConvergence()
- saveNet(net)
- train()
- # def ivertPole():
- # poleinvert = copy.deepcopy(pole)
- # for i in poleinvert:
- # for j in i:
- # if j == 2:
- def win():
- for k in pole[0]:
- if k == 2:
- print('Победили черные')
- exit()
- for k in pole[7]:
- if k == 1:
- print('Победили белые')
- exit()
- def printPole():
- c = 0
- print(' 1 2 3 4 5 6 7 8 ')
- for e in pole:
- c += 1
- polePrint = ''
- for j in e:
- if j == 1:
- polePrint += ' O |'
- if j == 0:
- polePrint += ' |'
- if j == 2:
- polePrint += ' * |'
- print polePrint, c
- def savepole(pole):
- savepole = []
- for i in pole:
- savepole += i
- return savepole
- def step(gamer):
- polecopy = copy.deepcopy(pole)
- printPole()
- print('Выберите какой шашкой ходить')
- try:
- x1 = int(input('x1 = '))
- y1 = int(input('y1 = '))
- except ValueError:
- print('Введите корректные значения')
- step(gamer)
- while x1 > 8 or x1 < 1 or y1 > 8 or y1 < 1:
- print('Введите корректные значения')
- try:
- x1 = int(input('x1 = '))
- y1 = int(input('y1 = '))
- except ValueError:
- print('Введите корректные значения')
- step(gamer)
- print('Выберите куда пойти')
- try:
- x2 = int(input('x2 = '))
- y2 = int(input('y2 = '))
- except ValueError:
- print('Введите корректные значения')
- step(gamer)
- while x2 > 8 or x2 < 1 or y2 > 8 or y2 < 1:
- print('Введите корректные значения')
- try:
- x2 = int(input('x2 = '))
- y2 = int(input('y2 = '))
- except ValueError:
- print('Введите корректные значения')
- step(gamer)
- flag = False
- # четность
- if (x2 + y2) % 2 == 0:
- flag = True
- # что ноль в клетке куда идем
- if pole[x2 - 1][y2 - 1] != 0 and flag:
- flag = False
- # что мы не нулем
- if pole[x1 - 1][y1 - 1] == 0:
- flag = False
- # расчет центрального элемента
- center_x = int((x1 + x2) / 2)
- center_y = int((y1 + y2) / 2)
- # бить или не бить
- if (x1 + x2) % 2 == 0:
- if flag and pole[x1 - 1][y1 - 1] != pole[center_x - 1][center_y - 1] and pole[center_x - 1][center_y - 1] != 0:
- flag = True
- pole[center_x - 1][center_y - 1] = 0
- else:
- flag = False
- if flag:
- if gamer == 1:
- addData(savepole(polecopy) + [1], [x1, y1, x2, y2])
- pole[x2 - 1][y2 - 1] = pole[x1 - 1][y1 - 1]
- pole[x1 - 1][y1 - 1] = 0
- else:
- print('Некорректный ввод')
- step(gamer)
- while True:
- print('Ходят белые')
- step(1)
- win()
- print('Ходят черные')
- step(2)
- win()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement