Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class piece:
- def __init__(self, x, y, type=0):
- self.x = x
- self.y = y
- self.type = type
- if self.type == 1:
- self.show = '●'
- elif self.type == 2:
- self.show = '○'
- else:
- self.show = '+'
- class player:
- def __init__(self, name):
- self.name = name
- class board:
- def __init__(self, pieces=[[piece(x, y, 0) for x in range(15)] for y in range(15)]):
- self.Pieces = pieces
- def initpieces(self):
- self.pieces = [[piece(x, y, 0) for x in range(15)] for y in range(15)]
- def showboard(self):
- for i in range(15):
- for j in range(15):
- print(self.Pieces[i][j].show, end='')
- print()
- class game:
- def __init__(self, B, turn, players=None):
- self.players = players
- self.B = B
- self.turn = turn
- self.check = True
- def initplayer(self, p1, p2):
- self.players = [p1, p2]
- def begin(self):
- self.showRule()
- print('The game is started')
- print("please input player1's name: ", end=' ')
- n1 = input()
- print("please input player2's name: ", end=' ')
- n2 = input()
- p1 = player(n1)
- p2 = player(n2)
- self.initplayer(p1, p2)
- def begingame(self):
- while self.check:
- self.showB()
- if self.turn:
- print("Now it's %s's turn" %(self.players[0].name))
- t = 1
- else:
- print("Now it's %s's turn" %(self.players[1].name))
- t = 2
- print("Please input position: ", end=' ')
- print()
- x, y = map(int, input().split())
- self.B.Pieces[x - 1][y - 1] = piece(x - 1, y - 1, t)
- self.judge(x - 1, y - 1)
- self.turn = not self.turn
- print('Game over!')
- def showRule(self):
- print('Rule:')
- def showB(self):
- self.B.showboard()
- def judge(self, x, y):
- cnt = 1
- for i in range(1, 15):
- if self.B.Pieces[i][y].type != 0 and self.B.Pieces[i][y].type == self.B.Pieces[i - 1][y].type:
- cnt += 1
- if cnt == 5:
- self.showB()
- if self.turn:
- print(self.players[0].name + ' won!')
- else:
- print(self.players[1].name + ' won!')
- self.check = False
- return
- else:
- cnt = 1
- cnt = 1
- for j in range(1, 15):
- if self.B.Pieces[x][j].type != 0 and self.B.Pieces[x][j].type == self.B.Pieces[x][j - 1].type:
- cnt += 1
- if cnt == 5:
- self.showB()
- if self.turn:
- print(self.players[0].name + ' won!')
- else:
- print(self.players[1].name + ' won!')
- self.check = False
- return
- else:
- cnt = 1
- x0 = x
- y0 = y
- while x0 > 0 and y0 > 0:
- x0 -= 1
- y0 -= 1
- x0 += 1
- y0 += 1
- cnt = 1
- while x0 < 14 and y0 < 14:
- if self.B.Pieces[x0][y0].type != 0 and self.B.Pieces[x0][y0].type == self.B.Pieces[x0 - 1][y0 - 1].type:
- cnt += 1
- if cnt == 5:
- self.showB()
- if self.turn:
- print(self.players[0].name + ' won!')
- else:
- print(self.players[1].name + ' won!')
- self.check = False
- return
- else:
- cnt = 1
- x0 += 1
- y0 += 1
- x0 = x
- y0 = y
- while x0 > 0 and y0 < 14:
- x0 -= 1
- y0 += 1
- x0 += 1
- y0 -= 1
- cnt = 1
- while x0 < 14 and y0 > 0:
- if self.B.Pieces[x0][y0].type != 0 and self.B.Pieces[x0][y0].type == self.B.Pieces[x0 - 1][y0 + 1].type:
- cnt += 1
- if cnt == 5:
- self.showB()
- if self.turn:
- print(self.players[0].name + ' won!')
- else:
- print(self.players[1].name + ' won!')
- self.check = False
- return
- else:
- cnt = 1
- x0 += 1
- y0 -= 1
- b1 = board()
- b1.initpieces()
- G = game(b1, True)
- G.begin()
- G.begingame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement