2016-12-07 2 views
0
私は可能であれば1に次の2つの句(互いの正反対)をリファクタリングしたい

をリファクタリングしますPythonはどのように反対の数句

if who == 0: 
    score = take_turn(strategy0(score0, score1), score1, select_dice(score0, score1)) 
    if score == 0: 
     score1 += strategy0(score0, score1) 
    else: 
     score0 += score 
elif who == 1: 
    score = take_turn(strategy1(score1, score0), score0, select_dice(score1, score0)) 
    if score == 0: 
     score0 += strategy1(score1, score0) 
    else: 
     score1 += score 

どのように私は、次の可能性を作るのでしょうか?

def other(who) 
    return 1 - who 

score = take_turn(strategy+who(score+who, score+other), score+other, select_dice(score+who, score+other)) 
if score == 0: 
    score+other += strategy+who(score+who, score+other) 
else: 
    score+who += score 

答えて

0

関連するデータが散在しているため、コードが複雑です。

あなたがしなければならないことは、データを収集して引数として渡すことだけです。

いくつかのシナリオがあり、私は次のコードのために辞書を使用します。

dataWho = {"id": 0, "score": score0, "strategy": strategy0} #gathering related data. function also can be the value of dictionary. 
dataOther = {"id": 1, "score": score1, "strategy": strategy1} 

def foo(dataWho, dataOther): 
    score = take_turn(dataWho["strategy"](dataWho["score"], dateOther["score"]), dataOther["score"], select_dice(dataWho["score"], dateOther["score"])) 
    if score == 0: 
     dateOther["score"] += dataWho["stratey"](dataWho["score"], dateOther["score"]) 
    else: 
     dataWho["score"] += score 

私はクラスの実装が優れていると思うので、試してみてください。