所以在过去的几天里,我一直在尝试让我的象棋引擎在numpy数组中工作。我已经在stringbase中创建了整个东西,但是它不会工作,因为numpy数组的整体性能。我在pygame中构建了这个。所以我使用的是最新版本的numpy和pygame。 守则:
import sys
import numpy as np
import pygame as p
BOARD_WIDTH = 512
BOARD_HEIGHT = 512
DIMENSION = 8 # dimensions of a chess board are 8x8
SQ_SIZE = 512 // DIMENSION
MAX_FPS = 60 # for animations later on
screen = p.display.set_mode((BOARD_WIDTH, BOARD_HEIGHT))
IMAGES = {}
class GameState():
def __init__(self):
# board is 8x8 2D List, each element of the list has 2 characters
# initial character == colour (b,w)
# second character == piece
# R == rook, N == knight, B == bishop, Q == Queen, K == king, P == pawn
# -- == empty space
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.lookup = {
"--": 0,
"wK": 1, "wQ": 2, "wR": 3, "wB": 4, "wN": 5, "wp": 6,
"bK": 7, "bQ": 8, "bR": 9, "bB": 10, "bN": 11, "bp": 12
}
self.compute_board = np.array([[self.lookup[p] for p in row] for row in self.board])
print(self.compute_board)
self.whiteToMove = True
self.moveLog = []
def makeMove(self, move):
self.compute_board[move.start_row][move.start_col] == 0
self.compute_board[move.end_row][move.end_col] = move.piece_moved
self.moveLog.append(move)
print(self.moveLog)
self.whiteToMove = not self.whiteToMove
class Move:
# in chess, fields on the board are described by two symbols, one of them being number between 1-8 (which is corresponding to rows)
# and the second one being a letter between a-f (corresponding to columns), in order to use this notation we need to map our [row][col] coordinates
# to match the ones used in the original chess game
ranks_to_rows = {"1": 7, "2": 6, "3": 5, "4": 4,
"5": 3, "6": 2, "7": 1, "8": 0}
rows_to_ranks = {v: k for k, v in ranks_to_rows.items()}
files_to_cols = {"a": 0, "b": 1, "c": 2, "d": 3,
"e": 4, "f": 5, "g": 6, "h": 7}
cols_to_files = {v: k for k, v in files_to_cols.items()}
def __init__(self, start_square, end_square, compute_board):
self.start_row = start_square[0]
self.start_col = start_square[1]
self.end_row = end_square[0]
self.end_col = end_square[1]
self.piece_moved = compute_board[self.start_row][self.start_col]
self.piece_captured = compute_board[self.end_row][self.end_col]
def getChessNotation(self):
return self.getRankFile(self.start_row, self.start_col + self.getRankFile(self.end_row, self.end_col))
def getRankFile(self, r, c):
return self.cols_to_files[c] + self.rows_to_ranks[r]
def main():
p.init()
square_selected = () # no square is selected initially, this will keep track of the last click of the user (tuple(row,col))
player_clicks = [] # this will keep track of player clicks (two tuples)
gs = GameState()
clock = p.time.Clock()
loadImages() # only do this once, before the while loop
running = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
p.quit()
sys.exit()
# mouse handler
elif e.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos() # (x, y) location of the mouse
col = location[0] // SQ_SIZE
row = location[1] // SQ_SIZE
if square_selected == (row, col) or col >= 8: # user clicked the same square twice
square_selected = () # deselect
player_clicks = [] # clear clicks
else:
square_selected = (row, col)
player_clicks.append(square_selected) # append for both 1st and 2nd click
if len(player_clicks) == 2: # after 2nd click
move = Move(player_clicks[0], player_clicks[1], gs.compute_board)
# print(move.getChessNotation())
gs.makeMove(move)
square_selected = ()
player_clicks = []
drawGameState(screen, gs.board)
clock.tick(MAX_FPS)
p.display.update()
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("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
# Note we can access an image by saying "IMAGES['wp']'
def drawGameState(screen, board):
drawBoard(screen) # draw squares on the board
drawPieces(screen, board)
def drawBoard(screen):
global colors
colors = [p.Color("white"), p.Color("gray")]
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 row in range(DIMENSION):
for column in range(DIMENSION):
piece = board[row][column]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(column * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE))
if __name__ == '__main__':
main()
要加载图像,您可以使用wikipedia:https://en.wikipedia.org/wiki/Chess_piece右侧“棋子”下的第一列中的图像
问题出在以下行:self.compute_board[move.start_row][move.start_col] == 0
其中0表示numpy数组中的空白,这些块可以移动到其中,但我还没有找到问题所在。我试图找到type
的值move.start_row
和move.start_col
,它们都是int
。我试过了self.compute_board[move.start_row][move.start_col] == list(self.lookup.keys())[list(self.lookup.values()).index(0)]
,但它给你的'--'
不起作用
但这对字符串verison有效。在类Move
中,将所有compute_board
和self.compute_board
更改为board
和self.board
。以及{self.compute_board[move.start_row][move.start_col] = 0
中的0更改为'--'
。在main函数中,将move = Move(player_clicks[0], player_clicks[1], gs.compute_board)
中的gs.compute_board
更改为gs.board
当所有这些改变都完成后,所有的部件移动都没有问题。但是说到numpy阵列,我不知道
self.compute_board
是我想要将所有移动编码到numpy数组中的方式,这样当我运行我的国际象棋ai时,它就不会被它必须运行的字符串所困扰
我解决了逻辑工作的问题,只是当我加载图像时,我没有将它们与compute_board
一起加载,而只是另一块板。我还必须改变我在黑板上快速播放图像的方式。如果你想知道如何让我知道
如果你能帮助我,我非常感谢,如果你需要更多的信息,请告诉我
这取决于我如何加载我的图像
老办法:
新方式:
然后我不得不改变我在黑板上画的方式
刚刚用
if piece != 0
摆脱了if piece != '
,它就工作了。我还将函数调用中的变量drawGameState
改为gs.board
改为gs.compute_board
相关问题 更多 >
编程相关推荐