交换一个类的两个实例

时间:2018-11-03 14:09:09

标签: python python-3.x class instance swap

#!/usr/bin/python
import chess.uci
import chess
import chess.pgn
import chess.svg

def set_engine(engine_path):
    engine = chess.uci.popen_engine(engine_path)
    engine.uci()
    engine.ucinewgame()
    return engine

def chess_match(engine1,engine2):
    while not board.is_game_over():
        engine1.position(board)
        engine1_move,ponder= engine1.go(movetime=100)
        board.push(engine1_move)
        engine2.position(board)
        engine2_move,ponder= engine2.go(movetime=100)
        board.push(engine2_move)

    print(engine1.name ," - ", engine2.name, board.result())


board = chess.Board()
start = board.set_fen(chess.STARTING_FEN)

engine1 = set_engine(".../Chess Engines/andscacs.exe")
engine2 = set_engine(".../Chess Engines/stockfish_18102108_x64_modern.exe")

for _ in range(6):
    chess_match(engine1,engine2)
    engine1, engine2 = engine2, engine1

在上面的代码中,我正在编写使用python-chess模块在​​国际象棋引擎之间进行匹配的程序。在这行代码中

for _ in range(6):
    chess_match(engine1,engine2)
    engine1, engine2 = engine2, engine1

我想在引擎之间进行6场比赛,每场比赛他们都会切换。 engine1engine2chess.uci.Engine类的实例。在尝试了代码之后,每次的结果都是相同的,这意味着引擎没有切换侧面。经过一番浏览后,我意识到您不能通过元组拆包来交换一个类的两个实例。我已经读过this thread关于相同的问题,但仍然无法弄清楚如何编写swap方法。我在Windows 10中使用Python 3.6。

1 个答案:

答案 0 :(得分:0)

并不是您要的解决方案,但是添加另一个bool变量以确定应该启动哪个引擎呢?

following_ids = current_user.following_users.pluck(:id)
following_ids << current_user.id
@posts = Post.where(user_id: params[:following_ids]).where('id < ?', params[:last_post_id]).order('created_at DESC').limit(20)