2016-10-26 3 views
0

私は船が5x5ボード上の2スペースを占める基本的な戦艦スタイルのゲームを設計しようとしています。より具体的な範囲のランダムints

私はランダムに座標の1つを生成していて、2番目の座標をランダムに生成して最後の番号の1つのスペース内にしようとしています。ここに私のコードです

coordinate1x = random.randint(0,4) 
coordinate1y = random.randint(0,4) 
coordinate1 = [coordinate1x, coordinate1y] 

coordinate2x = random.randint(coordinate1x, coordinate1x + 1) 
coordinate2y = random.randint(coordinate1y, coordinate1y + 1) 
coordinate2 = [coordinate2x, coordinate2y] 

battleship_location = [coordinate1, coordinate2] 

私は正しい数字を得るために論理を見つけるのに苦労しています。

すべてのヘルプは大幅に取るには、2つの決定事項があります

+0

:ここ

Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N N N N N 

が示唆コードです。そして、第2世代の世代の値を0から4の範囲に制限します(したがって、4,4の後ろに5,5を置くことはできません)。 – ShadowRanger

+0

したがって、初期値は0,3になり、5x5の範囲外になることはありませんか? – Nizac

+0

いいえ、初期値をどこにでも設定します。第2ラウンドのみを縛った。 – ShadowRanger

答えて

0

をapprecaitedさ:

  1. をグリッド上の2つのセルの最初のを配置する場所を水平または垂直に
  2. 船を配置するかどうかは。

船の向きとすることができる:

[ ][ ] 

または:

[ ] 
[ ] 

左/上の2つの大部分が第二の選択肢で選択されている細胞です。垂直船について

Y Y Y Y N 
Y Y Y Y N 
Y Y Y Y N 
Y Y Y Y N 
Y Y Y Y N 

:あなたが境界線をオーバーランしていないことを確認するには、最初のセルを選択することができ範囲は、水平方向の船のために、この(Yを選択することができ、Nではない)のようなものです:あなたは、おそらくそれはあまりにも最初の座標より1少ない値を生成できるようにする必要があり

# choose whether to place the ship vertically or not 
is_vertical = random.randint(0,1) 
# choose the first cell from a range that depends on the chosen direction 
cell1 = [random.randint(0, 3+is_vertical), random.randint(0,4-is_vertical)] 
# copy the coordinates 
cell2 = cell1[:] 
# add 1 to the x or y coordinate, depending on the chosen direction 
cell2[is_vertical] += 1 

battleship_location = [cell1, cell2] 

print(battleship_location) 
+0

これは完全に機能し、垂直かどうかを判断することは考えませんでした。 ! – Nizac

関連する問題