Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. # coding=UTF-8
  2.  
  3. import json, copy, pickle
  4. from pybrain.tools.shortcuts import buildNetwork
  5. from pybrain.structure import TanhLayer
  6. from pybrain.datasets import SupervisedDataSet
  7. from pybrain.supervised.trainers import RPropMinusTrainer
  8.  
  9. #        1  2  3  4  5  6  7  8
  10. pole = [[1, 0, 1, 0, 1, 0, 1, 0],  # 1
  11.         [0, 1, 0, 1, 0, 1, 0, 1],  # 2
  12.         [1, 0, 1, 0, 1, 0, 1, 0],  # 3
  13.         [0, 0, 0, 0, 0, 0, 0, 0],  # 4
  14.         [0, 0, 0, 0, 0, 0, 0, 0],  # 5
  15.         [0, 2, 0, 2, 0, 2, 0, 2],  # 6
  16.         [2, 0, 2, 0, 2, 0, 2, 0],  # 7
  17.         [0, 2, 0, 2, 0, 2, 0, 2]]  # 8
  18.  
  19. filename = "learning.txt"
  20.  
  21. fileNet = "Net.1"
  22.  
  23.  
  24. def addData(input, result):
  25.     learning = readLearning()
  26.     learning.append(input)
  27.     learning.append(result)
  28.     writeLearning(learning)
  29.  
  30.  
  31. def writeLearning(learning):
  32.     file = open(filename, 'w')
  33.     jlearning = json.dumps(learning)
  34.     file.write(jlearning)
  35.     file.close()
  36.  
  37.  
  38. def readLearning():
  39.     file = open(filename)
  40.     jlearning = file.read()
  41.     learning = json.loads(jlearning)
  42.     file.close()
  43.     return learning
  44.  
  45.  
  46. def saveNet(net):
  47.     file = open(fileNet, 'w')
  48.     pickle.dump(net, file)
  49.     file.close()
  50.  
  51.  
  52. def loadNet():
  53.     file = open(fileNet, 'w')
  54.     net = pickle.load(file)
  55.     file.close()
  56.  
  57.     return net
  58.  
  59.  
  60. def train():
  61.     learning = readLearning()
  62.     net = buildNetwork(65, 10, 4)
  63.     ds = SupervisedDataSet(65, 4)
  64.     for i in range(0, len(learning), 2):
  65.         ds.addSample(learning[i],learning[i+1])
  66.     trainer = RPropMinusTrainer(net)
  67.     trainer.setData(ds)
  68.     trainer.trainUntilConvergence()
  69.     saveNet(net)
  70.  
  71.  
  72. train()
  73.  
  74. # def ivertPole():
  75. #     poleinvert = copy.deepcopy(pole)
  76. #     for i in poleinvert:
  77. #         for j in i:
  78. #             if j == 2:
  79.  
  80.  
  81.  
  82.  
  83. def win():
  84.     for k in pole[0]:
  85.         if k == 2:
  86.             print('Победили черные')
  87.             exit()
  88.  
  89.     for k in pole[7]:
  90.         if k == 1:
  91.             print('Победили белые')
  92.             exit()
  93.  
  94.  
  95. def printPole():
  96.     c = 0
  97.     print(' 1   2   3   4   5   6   7   8  ')
  98.     for e in pole:
  99.         c += 1
  100.         polePrint = ''
  101.         for j in e:
  102.             if j == 1:
  103.                 polePrint += ' O |'
  104.             if j == 0:
  105.                 polePrint += '   |'
  106.             if j == 2:
  107.                 polePrint += ' * |'
  108.         print polePrint, c
  109.  
  110.  
  111. def savepole(pole):
  112.     savepole = []
  113.     for i in pole:
  114.         savepole += i
  115.  
  116.     return savepole
  117.  
  118.  
  119. def step(gamer):
  120.     polecopy = copy.deepcopy(pole)
  121.     printPole()
  122.     print('Выберите какой шашкой ходить')
  123.  
  124.     try:
  125.         x1 = int(input('x1 = '))
  126.         y1 = int(input('y1 = '))
  127.     except ValueError:
  128.         print('Введите корректные значения')
  129.         step(gamer)
  130.  
  131.     while x1 > 8 or x1 < 1 or y1 > 8 or y1 < 1:
  132.         print('Введите корректные значения')
  133.         try:
  134.             x1 = int(input('x1 = '))
  135.             y1 = int(input('y1 = '))
  136.         except ValueError:
  137.             print('Введите корректные значения')
  138.             step(gamer)
  139.  
  140.     print('Выберите куда пойти')
  141.  
  142.     try:
  143.         x2 = int(input('x2 = '))
  144.         y2 = int(input('y2 = '))
  145.     except ValueError:
  146.         print('Введите корректные значения')
  147.         step(gamer)
  148.  
  149.     while x2 > 8 or x2 < 1 or y2 > 8 or y2 < 1:
  150.         print('Введите корректные значения')
  151.         try:
  152.             x2 = int(input('x2 = '))
  153.             y2 = int(input('y2 = '))
  154.         except ValueError:
  155.             print('Введите корректные значения')
  156.             step(gamer)
  157.  
  158.     flag = False
  159.  
  160.     # четность
  161.     if (x2 + y2) % 2 == 0:
  162.         flag = True
  163.  
  164.     # что ноль в клетке куда идем
  165.     if pole[x2 - 1][y2 - 1] != 0 and flag:
  166.         flag = False
  167.  
  168.     # что мы не нулем
  169.     if pole[x1 - 1][y1 - 1] == 0:
  170.         flag = False
  171.  
  172.     # расчет центрального элемента
  173.     center_x = int((x1 + x2) / 2)
  174.     center_y = int((y1 + y2) / 2)
  175.  
  176.     # бить или не бить
  177.     if (x1 + x2) % 2 == 0:
  178.         if flag and pole[x1 - 1][y1 - 1] != pole[center_x - 1][center_y - 1] and pole[center_x - 1][center_y - 1] != 0:
  179.             flag = True
  180.             pole[center_x - 1][center_y - 1] = 0
  181.         else:
  182.             flag = False
  183.  
  184.     if flag:
  185.         if gamer == 1:
  186.             addData(savepole(polecopy) + [1], [x1, y1, x2, y2])
  187.  
  188.         pole[x2 - 1][y2 - 1] = pole[x1 - 1][y1 - 1]
  189.         pole[x1 - 1][y1 - 1] = 0
  190.     else:
  191.         print('Некорректный ввод')
  192.         step(gamer)
  193.  
  194.  
  195. while True:
  196.     print('Ходят белые')
  197.     step(1)
  198.     win()
  199.  
  200.     print('Ходят черные')
  201.     step(2)
  202.     win()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement