我怎样才能使函数根据其输出动态调整其参数之一?

问题描述 投票:0回答:1

我正在制作一个国际象棋引擎,需要一个切片函数根据拆分开始后的数字数量(或任何其他实现相同功能的方法)进行更改。这样程序就知道重复显示一块的次数。例如,这是标准的国际象棋设置; 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()
python chess
1个回答
0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.