Advertisement
H0_0H

Untitled

Apr 23rd, 2022 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. class piece:
  2.     def __init__(self, x, y, type=0):
  3.         self.x = x
  4.         self.y = y
  5.         self.type = type
  6.         if self.type == 1:
  7.             self.show = '●'
  8.         elif self.type == 2:
  9.             self.show = '○'  
  10.         else:
  11.             self.show = '+'
  12.  
  13. class player:
  14.     def __init__(self, name):
  15.         self.name = name
  16.  
  17.  
  18. class board:
  19.     def __init__(self, pieces=[[piece(x, y, 0) for x in range(15)] for y in range(15)]):
  20.         self.Pieces = pieces
  21.    
  22.     def initpieces(self):
  23.         self.pieces = [[piece(x, y, 0) for x in range(15)] for y in range(15)]
  24.  
  25.     def showboard(self):
  26.         for i in range(15):
  27.             for j in range(15):
  28.                 print(self.Pieces[i][j].show, end='')                  
  29.             print()
  30.  
  31. class game:
  32.     def __init__(self, B, turn, players=None):
  33.         self.players = players
  34.         self.B = B
  35.         self.turn = turn
  36.         self.check = True
  37.  
  38.     def initplayer(self, p1, p2):
  39.         self.players = [p1, p2]
  40.  
  41.     def begin(self):
  42.         self.showRule()
  43.         print('The game is started')
  44.         print("please input player1's name: ", end=' ')
  45.         n1 = input()
  46.         print("please input player2's name: ", end=' ')
  47.         n2 = input()
  48.         p1 = player(n1)
  49.         p2 = player(n2)
  50.         self.initplayer(p1, p2)
  51.    
  52.     def begingame(self):
  53.         while self.check:
  54.             self.showB()
  55.             if self.turn:
  56.                 print("Now it's %s's turn" %(self.players[0].name))
  57.                 t = 1
  58.             else:
  59.                 print("Now it's %s's turn" %(self.players[1].name))
  60.                 t = 2
  61.  
  62.            
  63.             print("Please input position: ", end=' ')
  64.             print()
  65.             x, y = map(int, input().split())
  66.             self.B.Pieces[x - 1][y - 1] = piece(x - 1, y - 1, t)
  67.             self.judge(x - 1, y - 1)
  68.             self.turn = not self.turn
  69.            
  70.         print('Game over!')
  71.            
  72.        
  73.  
  74.    
  75.     def showRule(self):
  76.         print('Rule:')
  77.    
  78.     def showB(self):
  79.         self.B.showboard()
  80.    
  81.     def judge(self, x, y):
  82.         cnt = 1
  83.         for i in range(1, 15):
  84.             if self.B.Pieces[i][y].type != 0 and self.B.Pieces[i][y].type == self.B.Pieces[i - 1][y].type:
  85.                 cnt += 1
  86.                 if cnt == 5:
  87.                     self.showB()
  88.                     if self.turn:
  89.                         print(self.players[0].name + ' won!')
  90.                     else:
  91.                         print(self.players[1].name + ' won!')  
  92.  
  93.                     self.check = False
  94.                     return
  95.             else:
  96.                 cnt = 1
  97.        
  98.         cnt = 1
  99.         for j in range(1, 15):
  100.             if self.B.Pieces[x][j].type != 0 and self.B.Pieces[x][j].type == self.B.Pieces[x][j - 1].type:
  101.                 cnt += 1
  102.                 if cnt == 5:
  103.                     self.showB()
  104.                     if self.turn:
  105.                         print(self.players[0].name + ' won!')
  106.                     else:
  107.                         print(self.players[1].name + ' won!')  
  108.  
  109.                     self.check = False
  110.                     return
  111.             else:
  112.                 cnt = 1
  113.            
  114.         x0 = x
  115.         y0 = y
  116.         while x0 > 0 and y0 > 0:
  117.             x0 -= 1
  118.             y0 -= 1
  119.        
  120.         x0 += 1
  121.         y0 += 1
  122.         cnt = 1
  123.         while x0 < 14 and y0 < 14:
  124.             if self.B.Pieces[x0][y0].type != 0 and self.B.Pieces[x0][y0].type == self.B.Pieces[x0 - 1][y0 - 1].type:
  125.                 cnt += 1
  126.                 if cnt == 5:
  127.                     self.showB()
  128.                     if self.turn:
  129.                         print(self.players[0].name + ' won!')
  130.                     else:
  131.                         print(self.players[1].name + ' won!')
  132.  
  133.                     self.check = False
  134.                     return
  135.             else:
  136.                 cnt = 1
  137.             x0 += 1
  138.             y0 += 1
  139.  
  140.         x0 = x
  141.         y0 = y
  142.         while x0 > 0 and y0 < 14:
  143.             x0 -= 1
  144.             y0 += 1
  145.  
  146.         x0 += 1
  147.         y0 -= 1
  148.         cnt = 1
  149.         while x0 < 14 and y0 > 0:
  150.             if self.B.Pieces[x0][y0].type != 0 and self.B.Pieces[x0][y0].type == self.B.Pieces[x0 - 1][y0 + 1].type:
  151.                 cnt += 1
  152.                 if cnt == 5:
  153.                     self.showB()
  154.                     if self.turn:
  155.                         print(self.players[0].name + ' won!')
  156.                     else:
  157.                         print(self.players[1].name + ' won!')  
  158.  
  159.                     self.check = False
  160.                     return
  161.             else:
  162.                 cnt = 1
  163.             x0 += 1
  164.             y0 -= 1
  165.  
  166.  
  167. b1 = board()
  168. b1.initpieces()
  169. G = game(b1, True)
  170. G.begin()
  171. G.begingame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement