我有一个学校作业,其目的是模拟骑士在棋盘上的运动。骑士随机移动的第一部分都很好但是用户选择移动的第二部分让我很头疼。由于我们休假,我无法在学校获得帮助。
from random import choice
#From the module random imports the random choice option
class Point: #Class for a point on the board
def __init__(self, xCoord, yCoord):#Coordinates of a position on the board
self.xCoord = xCoord #x-coordinate, i.e. row
self.yCoord = yCoord #y-coordinate, i.e. column
def getCoordinates(self):
return (self.xCoord, self.yCoord) #Returns coordinates for point on board
def getOffsetCoordinates(self, i = 65):
return (chr(self.xCoord + i), self.yCoord + 1)
def __str__(self):
return str(self.getCoordinates()) #Returns the coordinates in a string
return str(self.getLegalPoints())
class Board: #Class for the board
def __init__(self): #Creates the board using matrices
self.rows = [] #A list which will be used as a base for the rows
for x in range(8): #Adds 8 columns in which every
column = []
for y in range(8):
column.append(0)
self.rows.append(column)
def getBoard(self):
return self.rows #Returns rows, aka the board
def setPointTaken(self, point, stepnumber):
x,y = point.getCoordinates()
self.rows[x][y] = stepnumber
def __str__(self):
boardString = ""
boardString += " 1 2 3 4 5 6 7 8\n"
for i in range(len(self.rows)):
currentRow = self.rows[i]
boardString += " |" + chr(65 + i) + "| "
for j in range(len(currentRow)):
elementString = str(currentRow[j])
elementStringAdjusted = elementString.ljust(2)
boardString += elementStringAdjusted + " "
boardString += "\n"
return boardString
def getLegalPoints(currentPosition, board):
possiblePoints = []
legalPoints = []
stringlegalPoints = str(legalPoints)
originX, originY = currentPosition.getCoordinates()
possiblePoints.append(Point(originX + 1, originY + 2))
possiblePoints.append(Point(originX + 1, originY - 2))
possiblePoints.append(Point(originX - 1, originY - 2))
possiblePoints.append(Point(originX - 1, originY + 2))
possiblePoints.append(Point(originX + 2, originY + 1))
possiblePoints.append(Point(originX + 2, originY - 1))
possiblePoints.append(Point(originX - 2, originY - 1))
possiblePoints.append(Point(originX - 2, originY + 1))
for point in possiblePoints:
x, y = point.getCoordinates()
if 0 <= x <= 7 and 0 <= y <= 7 and not board.rows[x][y]:
legalPoints.append(point)
return legalPoints
return strlegalPoints
def main():
playType = None
while playType != 1 or playType != 2:
playType = int(input("\n""This program shows the path of a knight on a chessboard." "\n" + "[1] - Random path" "\n" + "[2] - Make your own path" + "\n" + "Choose option 1 for a random path or option 2 to create your own!" "\n"))
if playType == 1:
board = Board()
startLetter = input("Choose a row (A-H): ").upper()
startNumber = input("Choose a number (1-8): ")
xCoord = ord(startLetter)-65
yCoord = (int(startNumber) - 1)
startPoint = Point(xCoord, yCoord)
stepnumber = 1
board.setPointTaken(startPoint, stepnumber)
legalPoints = getLegalPoints(startPoint, board)
while legalPoints != None:
stepnumber += 1
nextPosition = choice(legalPoints)
board.setPointTaken(nextPosition, stepnumber)
legalPoints = getLegalPoints(nextPosition, board)
if len(legalPoints) <= 0:
break
print(board)
print("Last visited square: ",nextPosition.getOffsetCoordinates(),"\n" "Number of steps: ",stepnumber,"\n")
break
elif playType == 2:
board = Board()
startLetter = input("Choose a row (A-H): ").upper()
startNumber = input("Choose a number (1-8): ")
xCoord = ord(startLetter)-65
yCoord = (int(startNumber) - 1)
startPoint = Point(xCoord, yCoord)
stepnumber = 1
board.setPointTaken(startPoint, stepnumber)
legalPoints = getLegalPoints(startPoint, board)
while legalPoints != None:
print(str(legalPoints))
stepnumber += 1
nextPosition = input("Choose your next square among those presented: ")
board.setPointTaken(nextPosition, stepnumber)
legalPoints = getLegalPoints(nextPosition, board)
print(board)
print(nextPosition.getOffsetcoordinates(), "\n" "Number of steps: ",stepnumber,"\n")
if len(legalPoints) <= 0:
break
break
main()
这是我到目前为止所拥有的。我尝试重新使用随机路径的移动来创建个性化路径,但我无法弄清楚如何打印出允许的移动而不会出现如此:
[<__main__.Point object at 0x03915030>, <__main__.Point object at 0x03915090>, <__main__.Point object at 0x039150B0>, <__main__.Point object at 0x039150D0>, <__main__.Point object at 0x039150F0>, <__main__.Point object at 0x03915110>, <__main__.Point object at 0x03915130>, <__main__.Point object at 0x03915150>]
Choose your next square among those presented:
根据我的理解,我应该在类中使用legalpoints函数将其表示为字符串。有关如何解决此问题的任何想法?我是一个完整的新手,如果我违反任何规则,请删除此帖。
答案 0 :(得分:0)
打印Point对象列表的一种方法是将Point.__str__
方法更改为__repr__
。那样会打印[(1,4),(1,0),(2,3),(2,1)]而不是[<__main__.Point object at 0x03915030>, ...]
之类的东西。小心!该表示看起来像两个整数的元组。我会亲自将其改为别的东西。 (如果您不想将__str__更改为__repr__,则可以使用类似for i in legalPoints: print(i)
的for循环
board.setPointTaken
方法需要一个Point对象作为它的第一个参数,但是你要传递一个字符串,因为input
返回一个字符串。
假设用户将以(1, 4)
等格式输入新的广场,您可以通过将以下两行替换为nextPosition = input("Choose your next square among those presented: ")
行来解决此问题:
nextPosition_str = input("Choose your next square among those presented: ")
nextPosition = Point(*(int(k) for k in nextPosition_str[1:-1].split(',')))
如果您希望用户以B6
左右的格式输入广场,则必须对其进行编辑。
您在print(nextPosition.getOffsetcoordinates(), "\n" "Number of steps: ",stepnumber,"\n")
行中也有拼写错误。该方法的名称具有大写C,因此它应为getOffsetCoordinates()
。