python 我怎样才能使一个函数根据它的输出动态地调整它的一个参数呢?

gijlo24d  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(92)

我正在做一个国际象棋引擎,需要一个切片函数来改变,这取决于有多少数字是遵循分裂的开始(或任何其他方法,实现相同)。bRbNbBbQbKbBbNbR8bP32EE8wPwRwNwBwQwKwBwNwR
这是我目前为止的代码;
chessEngine.py

colours = ["White", "Black"]

class gameState():
    def __init__ (self):
        self.board = "bRbNbBbQbKbBbNbR8bP32EE8wPwRwNwBwQwKwBwNwR"
        self.moveColour = "White"
        self.gameLog = ""
        self.canCastle = True

chessMain.py

import pygame
import chessEngine

pygame.init()
WIDTH = HEIGHT = 512
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 5
IMAGES= {}

def loadImages():
    pieces = ["bR", "bN", "bB", "bQ", "bK", "bP", 
              "wP", "wR", "wN", "wB", "wQ", "wK",]
    for piece in pieces:
        IMAGES[piece] = pygame.transform.scale(pygame.image.load("Assets/" + piece + ".png"), (SQ_SIZE,SQ_SIZE))
        """ 
    We can now access an image by saying "IMAGES()"
    eg; to get the image for a white pawn write "IMAGES["wP"]"
    """
def main():
    screen = pygame.display.set_mode((WIDTH,HEIGHT))
    clock = pygame.time.Clock()
    screen.fill(pygame.Color("white"))
    gs = chessEngine.gameState()
    loadImages()
    running = True
    while running:
        for x in pygame.event.get():
            if x.type == pygame.QUIT:
                running = False
        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        pygame.display.flip()

def drawGameState(screen, gs):
    drawBoard(screen)
    drawPieces(screen, gs.board)

def drawBoard(screen):
    colors = [pygame.Color(253,235,253), pygame.Color(75,160,60)] # White and green
    for r in range(DIMENSION):
        for c in range (DIMENSION):
            color = colors[((r+c)%2)]
            pygame.draw.rect(screen, color, pygame.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))

def drawPieces(screen, board):
    squareCount = 0
    listCount = 0
    r = squareCount % DIMENSION
    c = squareCount // DIMENSION
    for z in range(DIMENSION^2):
        x = slice(listCount, listCount + 1)
        if board[x].isnumeric() == False:
            v = slice(listCount, listCount + 2)
            screen.blit(IMAGES[board[v]], pygame.Rect(r*SQ_SIZE, c*SQ_SIZE, SQ_SIZE, SQ_SIZE))
            squareCount += 1
            listCount += 2
        else:
            amount = int(board[x])
            for y in range(amount):
                v = slice(listCount, listCount + 2)
                screen.blit(IMAGES[board[v]], pygame.Rect(r*SQ_SIZE, c*SQ_SIZE, SQ_SIZE, SQ_SIZE))                
                squareCount += 1
            listCount += len(str(amount)) + 2      

    
main()
polhcujo

polhcujo1#

以下是如何对电路板进行游程编码:

import csv

board = [
 ['bR','bN','bB','bQ','bK','bB','bN','bR'],
 ['bP','bP','bP','bP','bP','bP','bP','bP'],
 ['EE','EE','EE','EE','EE','EE','EE','EE'],
 ['EE','EE','EE','EE','EE','EE','EE','EE'],
 ['EE','EE','EE','EE','EE','EE','EE','EE'],
 ['EE','EE','EE','EE','EE','EE','EE','EE'],
 ['wP','wP','wP','wP','wP','wP','wP','wP'],
 ['wR','wN','wB','wQ','wK','wB','wN','wR']
]

def rle(board):
    last = None
    count = 0
    s = ''
    for row in board:
        for cell in row:
            if cell == last:
                count += 1
            else:
                if last:
                    if count > 1:
                        s += str(count)+last
                    else:
                        s += last
                count = 1
                last = cell
    if count > 1:
        s += str(count)+last
    else:
        s += last
    return s

print(rle(board))

输出:

bRbNbBbQbKbBbNbR8bP32EE8wPwRwNwBwQwKwBwNwR

相关问题