首页 > > 详细

辅导Board python数据结构、Board class python 编程辅导、讲解留学生数据结构语言

import fileinput
import copy
WHITE = 0
BLACK = 1
class Board:
"""
A 'Board' is a playground of this game. It contains
a static variable called 'turn' which indicates the
current turn of each play. It also contains a list
of lists of object which is of class 'Piece'.

A Piece has four types -- "-", "X", "O", "@".

"""

turn = WHITE

def __init__(self, board):
self.board = board
self.boundaryLength = len(self.board)
return
def __str__(self):
utput = " " + " ".join([str(i)
for i in range(self.boundaryLength)]) + "\n"
for i in range(len(self.board)):
output += str(i) + " "
row = self.board[i]
for j in range(len(row)):
spot = row[j]
if j == len(row)-1:
output += str(spot) + "\n"
else:
output += str(spot) + " "
return output

def __eq__(self, other):
return self.board == other.board

def validateBoard(self):
for i in range(len(self.board)):
row = self.board[i]
for j in range(len(row)):
piece = row[j]
if piece.loc != (i, j):
print("validation failed")
return False
return True

def getPieceOfLoc(self, loc):
"""
get the piece on location 'loc', return its reference.
If the location is out of boundary, return None
"""
if (loc[0] >= self.boundaryLength or loc[1] >= self.boundaryLength
or loc[0] ", (dest1, dest0))
return
def debug(board, path):
"""
Code used to debug board with a graphical UI
"""
board = copy.deepcopy(board)
print(board)
if path is None:
print("No solution")
return
for (oneMoveSrc, oneMoveDest) in path:
if board.moveTo(oneMoveSrc, oneMoveDest):
board.eliminate()
Board.turn = BLACK
board.eliminate()
Board.turn = WHITE
print(board)
else:
print("invalid move")
return
# main function
def main():

board, ption = readBoardFromFile()
board = Board(board)
if ption == "Moves":
for i in range(len(PIECE_CLASS_LIST)):
Board.turn = i
print(len(getTotalPotentialMoves(board)))
elif ption == "Massacre":
path = bfs(board)
printPath(path);
# debug(board, path)
return
if __name__ == "__main__":
main()
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!