2016-04-13 7 views
0

私は戦艦ゲームをPythonでセットアップしましたが、私が設定したグリッドは0〜5の範囲でした。戦艦の最初の行と列は(0,0)となります。 (1,1)または(1,2)を入力します。値0は、入力すると思われる値ではありません。私のプログラムは、1,1を開始列、2番目を行ではなく、どのように反映させることができますか?ユーザーは0〜4の間の値しか入力できないので、5は無効な値として表され、グリッド上にはないと表示されます。この戦艦ゲームを価値の面でより使いやすくするには?

だから、唯一の可能な組み合わせは、これらは以下のとおりです。

行:0、1、2、3、4、 カラム:0、1、2、3、4

私はそれになりたいです:

行:1、2、3、4、ここで5 カラム1、2、3、4、5

が私のコードである:

import random 

Battleship_Board = [] 

for x in range(0,5): 
    Battleship_Board.append(["O"] * 5) 


def print_Battleship_Board(Battleship_Board): 
    for row in Battleship_Board: 
    print (" ".join(row)) 

print ("Let's play a game of Battleships!") 
print_Battleship_Board(Battleship_Board) 


def Random_Battleship_Board_Row(Battleship_Board): 
return random.randint(0, len(Battleship_Board)-1) 

def Random_Battleship_Board_Column(Battleship_Board): 
return random.randint(0, len(Battleship_Board[0])-1) 

Battleship_Board_Row = Random_Battleship_Board_Row(Battleship_Board) 
Battleship_Board_Column = Random_Battleship_Board_Column(Battleship_Board) 


print (Battleship_Board_Row) 
print (Battleship_Board_Column) 

for turn in range(5): 

Guess_Battleship_Board_Row = int(input("Guess the X coordinate:")) 
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) 


if Guess_Battleship_Board_Row == Battleship_Board_Row and Guess_Battleship_Board_Column == Battleship_Board_Column: 
    print ("You sunk the battleship!") 
    print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]") 
    break 

else: 

     if turn + 1 == 5: 
      Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X" 
      print_Battleship_Board(Battleship_Board) 
      print ("Game Over") 
      print ("My ship was here: [" + str(Battleship_Board_Row) + "][" + str(Battleship_Board_Column) + "]") 

     if (Guess_Battleship_Board_Row < 0 or Guess_Battleship_Board_Row > 4) or (Guess_Battleship_Board_Column < 0 or Guess_Battleship_Board_Column > 4): 
      print ("The inserted value is not on the grid.") 
     elif(Battleship_Board[Guess_Battleship_Board_Row ][Guess_Battleship_Board_Column] == "X"): 
      print ("You already inserted this combination") 
     else: 
      print ("You missed my battleship") 
      Battleship_Board[Guess_Battleship_Board_Row][Guess_Battleship_Board_Column] = "X" 
     print ("Number of turns:", turn + 1,"out of 5") 
     print_Battleship_Board(Battleship_Board) 

答えて

4

あなたはユーザーの推測から1つを減算するだけでなく、数字がゼロベースでないと言うためにメモを追加することもできます。有効な入力を確認することを忘れないでください!

Guess_Battleship_Board_Row = int(input("Guess the X coordinate:")) - 1 
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) - 1 
+0

ありがとうございます:) –

1

5に入力する彼らの行と範囲1の列をユーザーを取得しますが、その後、あなたは自分の戦艦を配置する前に1を引くなど

たとえば、彼らは「5」を挿入した場合、1つのその後、場所を引きます船等

Guess_Battleship_Board_Row = INT(入力( "X座標推測:")) - 1 Guess_Battleship_Board_Column = INT(入力( "Y座標推測:")) - 1

0

は大きいように見えますあなたがここに行こうとしているプロジェクト!

この問題を解決できる方法はたくさんあるので、私はちょうどいくつかを撃墜すると思った。

すべての配列は "0"から始まりますが、(0、0)ちょっと醜いと思います。私は同意します。簡単な方法は、6,6配列を作成するだけで、0、0部分は使用しないでください。

もう1つの方法は、配列インデックスを表示する前に+1を追加するか、配列インデックスにアクセスする前に-1を追加することです。

したがって、ユーザーが4,5を入力すると、3,4(配列A [userinput - 1]、配列B [userinput - 1])が効果的に3に格納され、ユーザーはわかりません。

はまた、多分楽しいチャレンジではなく、行または列のための英数字の文字を使用しています。(例えば3、C)

switchステートメントまたは/ IF esle文を使用して。

例えば、

if userSelection == 'a' or userSelection == 'A' 
    arrayA[0] = true 

(申し訳ありませんが、私のPythonの構文は本当に錆びですが、概念はまだ適用されるべきである)

役に立てば幸い!

関連する問題