我正在创建一个国际象棋游戏,但我的代码有问题。我有两个文件,国际象棋引擎文件(ChessEngine.py):
class GameState():
def __init__(self):
# 8x8 2d board, each element has 2 characters.
self.board = [
["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]]
self.whitetoMove = True
self.moveLog = []
def makeMove(self, move):
self.board[move.startRow][move.startCol] = "--"
self.board[move.endRow][move.endCol] = move.pieceMoved
self.moveLog.append(move)
self.whitetoMove = not self.whitetoMove
def undoMove(self):
if len(self.moveLog) != 0:
move = self.moveLog.pop()
self.board[move.startRow][move.startCol] = move.pieceMoved
self.board[move.endRow][move.endCol] = move.pieceCaptured
self.whitetoMove = not self.whitetoMove
def getValidMoves(self):
return self.getAllPossibleMoves()
def getAllPossibleMoves(self):
moves = [Move((6, 4), (4, 4), self.board)]
for r in range(len(self.board)):
for c in range(len(self.board[r])):
turn = self.board[r][c][0]
if (turn == 'w' and self.whitetoMove) and (turn == 'b' and not self.whitetoMove):
piece = self.board[r][c][1]
if piece == 'p':
self.getPawnMoves(r, c, moves)
elif piece == 'R':
self.getRookMoves(r, c, moves)
return moves
def getPawnMoves(self, r, c, moves):
pass
def getRookMoves(self, r, c, moves):
pass
class Move():
ranksToRows = {"1": 7, "2": 6, "3": 5,
"4": 4, "5": 3, "6": 2, "7": 1, "8": 0}
rowsToRanks = {v: k for k, v in ranksToRows.items()}
filesToCols = {"a": 0, "b": 1, "c": 2,
"d": 3, "e": 4, "f": 5, "g": 6, "h": 7}
colsToFiles = {v: k for k, v in filesToCols.items()}
def __init__(self, startSq, endSq, board):
self.startRow = startSq[0]
self.startCol = startSq[1]
self.endRow = endSq[0]
self.endCol = endSq[1]
self.pieceMoved = board[self.startRow][self.startCol]
self.pieceCaptured = board[self.endRow][self.endCol]
self.moveID = self.startRow * 1000 + \
self.startCol*100 + self.endRow*10 + self.endCol
print(self.moveID)
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def getChessNotation(self):
return self.getRankFile(self.startRow, self.startCol) + self.getRankFile(self.endRow, self.endCol)
def getRankFile(self, r, c):
return self.colsToFiles[c] + self.rowsToRanks[r]
和我的实际游戏文件(ChessMate.py):
import pygame as p
import ChessEngine
WIDTH = HEIGHT = 512
DIMENSION = 8
SQ_SIZE = HEIGHT//DIMENSION
MAX_FPS = 15
IMAGES = {}
def loadImages():
pieces = ['wp', 'wR', 'wN', 'wB', 'wK',
'wQ', 'bp', 'bR', 'bN', 'bB', 'bK', 'bQ']
for piece in pieces:
IMAGES[piece] = p.transform.scale(
p.image.load('Chess/Images/' + piece + '.png'), (SQ_SIZE, SQ_SIZE))
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = ChessEngine.GameState()
validMoves = gs.getValidMoves()
moveMade = False
loadImages()
running = True
sqSelected = ()
playerClicks = []
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
elif e.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos()
col = location[0]//SQ_SIZE
row = location[1]//SQ_SIZE
if sqSelected == (row, col):
sqSelected = ()
playerClicks = []
else:
sqSelected = (row, col)
playerClicks.append(sqSelected)
if len(playerClicks) == 2:
move = ChessEngine.Move(
playerClicks[0], playerClicks[1], gs.board)
print(move.getChessNotation())
if move in validMoves:
gs.makeMove(move)
moveMade = True
sqSelected = ()
playerClicks = []
elif e.type == p.KEYDOWN:
if e.key == p.K_z:
gs.undoMove()
moveMade = True
if moveMade:
validMoves = gs.getValidMoves
moveMade = False
drawGameState(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()
def drawGameState(screen, gs):
drawBoard(screen)
drawPieces(screen, gs.board)
def drawBoard(screen):
colors = [p.Color("light yellow"), p.Color("dark green")]
for r in range(DIMENSION):
for c in range(DIMENSION):
color = colors[((r+c) % 2)]
p.draw.rect(screen, color, p.Rect(
c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawPieces(screen, board):
for r in range(DIMENSION):
for c in range(DIMENSION):
piece = board[r][c]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(
c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
if __name__ == '__main__':
main()
我得到的确切错误是:
Traceback (most recent call last):
File "/Users/Name/Documents/Python/AI/Chess/Chess/ChessMate.py", line 94, in <module>
main()
File "/Users/Name/Documents/Python/AI/Chess/Chess/ChessMate.py", line 51, in main
if move in validMoves:
TypeError: argument of type 'method' is not iterable
我查过其他帖子,上面说问这个问题的人的功能缺少括号,但是我已经扫描了我的代码很多次了,我仍然找不到任何东西
请帮帮我,我真的卡住了
这部分代码中有一个问题:
您需要将其更改为:
相关问题 更多 >
编程相关推荐