2017-07-07 13 views
0

私はゲームをパイソンでシミュレートしています。それには2人のプレーヤーが関わっており、それぞれに一連のターンがあります。ヒットまたはミスに基づいて、そのプレイヤーが別のターンを得るかどうかを決定します。ターンベースのパズルを解く正しいアプローチ

Iプログラムで、ここにこのロジックを私がこれまで持っているものされて作ることができない:個々のターンを持っているために、

だが

for coordinate in list_of_player1_moves: 
    result = fire(coordinate[0], coordinate[1]) 
    #result is either a 'Hit' or a 'Miss' 

for coordinate in list_of_player2_moves: 
    result = fire(coordinate[0], coordinate[1]) 
    #result is either a 'Hit' or a 'Miss' 

今すぐ簡単にするために個別に各プレイヤーをシミュレートしてみましょう各プレーヤーのために、私はした:

turns = len(list_of_player2_moves) if len(list_of_player2_moves) > len(list_of_player1_moves) else len(list_of_player1_moves) 

for turn in range(0, turns): 
    r = move_player1(turn) #move player inturn calls fire() 
    if(r == 'Hit'): 
     break #start over, giving them another turn 
    r = move_player2(turn) 
    if r == 'Hit': 
     #how do give player 2 another turn? 

私はこれ以上のアプローチ方法のアイデアです。提案してください。代替/より良いアプローチに関するアドバイスもお願いします。

ありがとうございます!

編集:より良い理解のため

サンプル出力、

Player1 fires got miss 
Player2 fires which got hit 
Player2 fires which got miss 
Player1 fires which got hit 
Player1 fires which got hit 
Player1 fires which got miss 
Player2 fires which got miss 
Player1 has no more missiles left to launch 
Player2 fires which got hit 
Player2 fires which got miss 
Player1 has no more missiles left to launch 
Player2 fires which got miss 
Player1 has no more missiles left to launch 
Player2 fires which got hit 
Player2 fires which got miss 
Player1 no more missiles left to launch 
Player2 fires which got hit 
Player2 won the battle 

答えて

1

あなたの問題の説明が少し曖昧であるので、私は

  1. たびにプレイヤーが他のプレイヤーに当たると仮定しています、彼はさらに1回移動する
  2. 両方のプレイヤーが移動しなくなるまでゲームが終了しない

は、このように私は機能

counter = 0 
no_of_player1_moves = len(list_of_player1_moves) 
no_of_player2_moves = len(list_of_player2_moves) 

while counter < no_of_player1_moves or counter < no_of_player2_moves: 
    # won't terminate until both players run out of turns 
    r = move_player1(turn) 
    if(r == 'Hit'): 
     no_of_player1_moves += 1 #if player1 hits, player1 gets 1 more turn 
    r = move_player2(turn) 
    if(r == 'Hit'): 
     no_of_player2_moves += 1 #if player2 hits, player2 gets 1 more turn 
    counter += 1 

P.S書き直し:あなたは簡単に使用することができます

turns = len(list_of_player2_moves) if len(list_of_player2_moves) > len(list_of_player1_moves) else len(list_of_player1_moves) 

の代わりにあなたの長い文を

turns = max(len(list_of_player2_moves),len(list_of_player1_moves)) 

編集:

no_of_player1_moves = len(list_of_player1_moves) 
no_of_player2_moves = len(list_of_player2_moves) 

while no_of_player1_moves>0 or no_of_player2_moves>0: 
    # won't terminate until both players run out of turns 
    while move_player1(turn)=="Hit" and no_of_player1_moves!=0: 
     no_of_player1_moves -= 1 
    while move_player2(turn)=="Hit" and no_of_player2_moves!=0: 
     no_of_player2_moves -= 1 

これはおそらくあなたの問題を解決するでしょうが、設計がうまくスケールされません(10人のプレイヤーがいる場合、10回書くことは望ましくありません)。

これについては、Playerオブジェクトを作成してリストに配置し、すべてのプレイヤーが動かなくなるまでリスト内を循環させておくことをお勧めします。そこにはもっと良い解決策があるかもしれませんが(これと比べても)、それは少なくともスケーラビリティがあります。

+0

私は連続したターンが必要です。おそらくサンプル出力が役に立ちます。https://bpaste.net/show/661190d04e57 – user1502

+0

あなたの出力からどのように問題が解決するかを編集します。 –

+0

今すぐターンを増やすにはどうしたらいいですか? – user1502

関連する問題