-4

Pythonで私がやろうとしているのは、移動が合法であることを確認するdef players_moveなど、len 5のraw_input文字列を取る関数を用意し、次に文字列を取り、2dで更新される座標を出力b1,i5する新しい関数を呼び出すことですcords()ボード。

以下は、文字列を座標に変換するために持っているものです。返すように頼んだ場合は機能b[2],i[5]しますが、文字列を取得してエラーなしで x 座標と y 座標を出力するか、座標ではなく文字列を返すだけです。

def cords ():
    move=raw_input('enter starting pos and end pos')        

    a=[[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0]]
    b=[[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[8,1]]
    c=[[0,2],[1,2],[2,2],[3,2],[4,2],[5,2],[6,2],[7,2],[8,2]]
    d=[[0,3],[1,3],[2,3],[3,3],[4,3],[5,3],[6,3],[7,3],[8,3]]
    e=[[0,4],[1,3],[2,4],[3,4],[4,4],[5,4],[6,4],[7,4],[8,4]]
    f=[[0,5],[1,3],[2,5],[3,5],[4,5],[5,5],[6,5],[7,5],[8,5]]
    g=[[0,6],[1,3],[2,6],[3,6],[4,6],[5,6],[6,6],[7,6],[8,6]]
    h=[[0,7],[1,3],[2,7],[3,7],[4,7],[5,7],[6,7],[7,7],[8,7]]
    i=[[0,8],[1,3],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8]]
    xx = move[0], int(move[1])
    #yy = move[3], int(move[4])
    xxx = xx[0],[xx[1]]
    #yyy= yy[3],[yy[4]]
    return xxx#,yyy    

関数内に raw_input を含めて、座標を出力するための他の部分がなくても機能するようにしましたが、[1,1] の代わりに ('b'[1]),'i'[5] を返しています。 [[3,8]またはさらに良いのは、[1][1]、[3][8]を出力することです。b1、i5が指定された場合、xとyの両方を試して返すとエラーが発生します。そうではありませんx だけで正しく動作しますが、エラーはありません。

次に、返された値をインポートし、update_board 関数内でボードを更新する必要があります。

4

1 に答える 1

0
import re
def get_move(move)  : 
    d = {}
    d['a']=[[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0]]
    d['b']=[[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[8,1]]
    d['c']=[[0,2],[1,2],[2,2],[3,2],[4,2],[5,2],[6,2],[7,2],[8,2]]
    #made example smaller

    def get_parts(part):
         parts = re.split("([a-zA-Z]+)",part)
     _,ltr,idx =parts
         try:
              return d[ltr.lower()][int(idx)]
         except KeyError:
              raise Exception("Letter {0} Does Not Exist".format(ltr))
         except IndexErrorError:
              raise Exception("Letter {0} Does Not Have Index {1}".format(ltr,idx))
    start,end = move.split(",")

    return get_parts(start),get_parts(end)
print  get_move(raw_input('enter starting pos and end pos'))

結果

enter starting pos and end posa3,c4
([3, 0], [4, 2])

caseInsensitive を許可するように編集

于 2012-09-15T06:48:52.117 に答える