为什么不仅有效的典当动作也起作用?

如何解决为什么不仅有效的典当动作也起作用?

我正在使用Pycharm Community 2020作为我的IDE。 Pygame 1.9.6。我正在使用Python 3.7.4。我正在编写一个工作的国际象棋棋盘。现在,我正在编程中,有效的pawn会变成“ w”或“ white”。如果有一个黑色的棋子在一个白色的棋子前面说,它可以将其移到它的前面甚至捕获它。当前面的相对的棋子可以互相捕获或移动时,它们只能在侧面移动。第一次棋子只能将白色棋子上移1或2个空格,但我可以将其移动到任何地方。我不知道为什么会这么做。

代码:

import pygame as p
from Chess import ChessEngine

WIDTH = HEIGHT = 512  # 400 is another option
DIMENSION = 8  # dimensions of a chess board are 8x8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15  # for animations later on
IMAGES = {}

'''
Initialize a global dictionary of images. This will be called exactly once in 
the main  
'''


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 saving "IMAGES"['wp']


'''
The main driver for our code. This will handle user input and updating the graphics.
'''


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 # flag variable for when a move is made

    loadImages()  # only do this once,before the while loop
    running = True
    sqSelected = ()  # no square is selected,keep track of the last click of the user (tuple: (row,col))
    playerClicks = []  # keep track of player clicks (two tuples [6,4),(4,4)]

while running:
    for e in p.event.get():
        if e.type == p.QUIT:
            running = False
        # mouse handler
        elif e.type == p.MOUSEBUTTONDOWN:
            location = p.mouse.get_pos()  # (x,y) location of mouse
            col = location[0]//SQ_SIZE
            row = location[1]//SQ_SIZE
            if sqSelected == (row,col):  # the user clicked the same square twice
                sqSelected = ()  # deselect
                playerClicks = []  # clear player clicks
            else:
                sqSelected = (row,col)
                playerClicks.append(sqSelected)  # append for both 1st and 2nd clicks
            if len(playerClicks) == 2:  # after 2nd click
                move = ChessEngine.Move(playerClicks[0],playerClicks[1],gs.board)
                print(move.getChessNotation())
                if move in validMoves:
                    gs.makeMove(move)
                    makeMove = True
                gs.makeMove(move)
                sqSelected = ()  # reset user clicks
                playerClicks = []
        # key handler
        elif e.type == p.KEYDOWN:
            if e.key == p.K_u: # undo when 'u' is pressed
                gs.undoMove()
                moveMade = True

    if moveMade:
        validMoves = gs.getValidMoves()
        moveMade = False

    drawGameState(screen,gs)
    clock.tick(MAX_FPS)
    p.display.flip()


'''
Responsible for all the graphics within a current game state.
'''


def drawGameState(screen,gs):
    drawBoard(screen)  # draw squares on the board
    # add in piece highlighting or move suggestions (later)
    drawPieces(screen,gs.board)  # draw pieces on top of those squares


'''
Draw the squares on the board. The top left square is always light.
'''


def drawBoard(screen):
    colors = [p.Color("white"),p.Color("grey")]
    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))


'''
Draw the pieces on the board using the current GameState.board
'''


def drawPieces(screen,board):
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            piece = board[r][c]
            if piece != "--":  # not empty square
                screen.blit(IMAGES[piece],SQ_SIZE))


if __name__ == "__main__":
main()


class GameState:
    def __init__(self):
        # board is an 8x8 2d list,each element of lest has 2 character.
        # The first character represents the color of the piece,"b" or "w"
        # The second character represents the type of the piece,"K","Q","R","B","N",or "p"
        # "--" - represents an empty space with no piece.
        self.board = [
            ["bR","bN","bB","bQ","bK","bR"],["bp","bp","bp"],["--","--","--"],["wp","wp","wp"],["wR","wN","wB","wQ","wK","wR"]]
        self.whiteToMove = True
        self.moveLog = []
'''
Takes a Move as a parameter and executes it (this will not work for castling,pawn promotion,and en passant 
'''
    def makeMove(self,move):
        self.board[move.startRow][move.startCol] = "--"
        self.board[move.endRow][move.endCol] = move.pieceMoved
        self.moveLog.append(move)  # log the move so we can undo it later
        self.whiteToMove = not self.whiteToMove  # swap players
'''
Undo the last move  
'''
    def undoMove(self):
        if len(self.moveLog) != 0:  # make sure there is a move to undo
            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  # switch turns back

'''
All moves considering checks
'''
    def getValidMoves(self):
        return self.getAllPossibleMoves()  # for now will not worry about checks

'''
All moves without considering checks
'''
    def getAllPossibleMoves(self):
        moves = []
        for r in range(len(self.board)):  # number of rows
            for c in range(len(self.board[r])):  # number of cols in given row
                turn = self.board[r][c][0]
                if (turn == 'w' and self.whiteToMove) or (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,moves)
    return moves
'''
Get all the pawn moves for the pawn located at row,col and add these moves to the list
'''
    def getPawnMoves(self,r,moves):
        if self.whiteToMove:  # white pawn moves
            if self.board[r-1][c] == "--":  # 1 square pawn advance
                moves.append(Move((r,c),(r-1,self.board))
                if r == 6 and self.board[r-2][c] == "--":  # 2 square pawn move
                moves.append(Move((r,(r-2,self.board))
        if c-1 >= 0:  # capture to the left
            if self.board[r-1][c-1] == 'b':  # enemy piece to capture
                moves.append(Move((r-1,c-1),self.board))
        if c+1 <= 7:  # capture to the right
            if self.board[r+1][c+1] == 'b':  # enemy piece to capture
                moves.append(Move((r-1,c+1),self.board))

'''
    Get all the rook moves for the rook located at row,col and add these moves to the list
'''
    def getRookMoves(self,moves):
        pass


class Move():
   # maps key to values
   # key : value
   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


'''
Overriding the equals method
'''
    def __eq__(self,other):
        if isinstance(other,Move):
            return self.moveID == other.moveID
        return False

    def getChessNotation(self):
        # you can add to make this like real chess notation
        return self.getRankFile(self.startRow,self.startCol) + self.getRankFile(self.endRow,self.endCol)

    def getRankFile(self,c):
        return self.colsToFiles[c] + self.rowsToRanks[r]

所以我迷失了为什么典当的白色动作不会像棋子的第一个动作那样上升一两个(它可以,但是如果单击它,它不应像五个空格一样上升)。

编辑:所以我将其更改为所有代码都在一个文件下。我也相信我已经为代码做出了正确的缩进。如果还有其他问题,请通知我。

解决方法

问题出在这里:

if move in validMoves:
  gs.makeMove(move)
  makeMove = True
gs.makeMove(move)
sqSelected = ()  # reset user clicks
playerClicks = []

我们有一个条件来检查移动是否在validMoves中。如果是,则使用gs.makeMove(move)进行移动,并设置一个似乎无用的变量(makeMove = True)。

现在出现了一个有趣的部分:鉴于先前的条件,我们再次调用gs.makeMove(move)

请尝试以下代码:

if move in validMoves:
  gs.makeMove(move)
sqSelected = ()  # reset user clicks
playerClicks = []

换一个音符:

  1. 您可能希望在此处正确设置复制粘贴的代码的格式。缩进是Python语法的重要组成部分,此处的粘贴似乎使它变得混乱。

  2. 您可能需要反转查找,而不是定义validMoves:特定的请求移动合法吗?这样,您无需计算所有可能的移动。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-