2017-03-08 5 views
0

私は戦艦のようなゲームのためにJavaアプレットを作り始めました。Java Applet Game

「船」が配置されている10個の座標を無作為に選んで10x10のボードを作成します。ユーザーはボードをクリックし、白いペグを配置して「ミス」を、赤いペグを「ヒット」として船の位置を推測します。 ユーザが最後の「船」に当選すると、プログラムは、獲得した推測の数を含む勝利のメッセージを書き出します。

私は10x10のボードを作成し、regピンのランダムな場所をArrayListに格納しました。今、それは私のために隠された赤い点の座標を出力することができ、それらがヒットしたときにそれらを置くことができます。ヒットしなければ、白い点を入れることができます。どのくらいヒットしたかを数えさせ、最後にすべてが満たされた場合や10個の船がすべて見つかった場合、ヒット数を超えてゲームを出力するようにしますか?何か助けていただければ幸いです。ここに私のコードのセクションです:

Boolean isHit = false; 

while(unWon && totalClicks <= 100) { 
isHit = false; // reset isHit 
Coordinate currentClick = board.getClick(); // Get the current click 

//Check the ship coordinates to see whether it is hit 
for(Coordinate c: ships) { 
    if(c.getRow() == currentClick.getRow() && c.getCol() == currentClick.getCol()) { 
    board.putPeg("red", currentClick.getRow(), currentClick.getCol()); 
    isHit = true; 
    break;   
    } 
} 

// If it didn't hit, mark it with a white peg 
if (!isHit) { 
    board.putPeg("white", currentClick.getRow(), currentClick.getCol()); 
    } 
    } 
} 
} 
+1

vandalizeないでくださいよurコンテンツ。サイトに投稿されると、サイトにライセンスされ、「コミュニティに所属します」。 – TylerH

答えて

0

あなたには、いくつかの理由のために、このコードでshipsにわたって2つの別々のループを持っている:

for(Coordinate c: ships){ 

for (int i = 0; i < ships.size(); i++) { 

は取り除きます外側のもの。有用なものは何もしていません。currentClickshipsの位置にない場合、そのループに関連するステートメントは、ループ内のすべてのコードを「内側」にスキップします。if

+0

それで、編集した後のコードセクションはどうなるでしょうか? if文で変更する必要があるのは何ですか? –

0

あなたがちょうどを通してソートする物事を簡単にするために、クリーンアップの少しを必要とするように思える:

// This will be used to track whether any of the ship coordinates is a match for currentClick 
Boolean isHit = false; 

// It looks like you can combine these two conditions, but if that changes, just put `totalClicks <= 100` in its own `if` statement 
while(unWon && totalClicks <= 100) { 
    // reset isHit 
    isHit = false; 
    // Get the current click 
    Coordinate currentClick = board.getClick(); 
    // Check the ship coordinates to see whether we hit 
    for(Coordinate c: ships) { 
    if(c.getRow() == currentClick.getRow() && c.getCol() == currentClick.getCol()) { 
     board.putPeg("red", currentClick.getRow(), currentClick.getCol()); 
     isHit = true; 
     break;   
    } 
    } 
    // If we didn't hit, mark it with white 
    if (!isHit) { 
    board.putPeg("white", currentClick.getRow(), currentClick.getCol()); 
    } 
} 

明瞭度を向上させるために、あなたは、独自の機能の中に赤いチェックを入れることができます:

Boolean isHit(Coordinate currentClick, ArrayList<Coordinate> ships) { 
    for(Coordinate c: ships) { 
    if(c.getRow() == currentClick.getRow() && c.getCol() == currentClick.getCol()) { 
    return = true; 
    } 
    return false; 
} 
は、その後、あなたはisHitブールを取り除くとあなたの中を書き換える得ることができます

while(unWon && totalClicks <= 100) { 
    Coordinate currentClick = board.getClick(); 
    if (isHit(currentClick, ships)) { 
    board.putPeg("red", currentClick.getRow(), currentClick.getCol()); 
    } else { 
    board.putPeg("white", currentClick.getRow(), currentClick.getCol()); 
    } 
} 
+0

最初の部分は機能しましたが、明快さを改善することによって何を意味しますか?私はあなたが2番目と3番目のセクションで何をしたのかよく分かりません。助けていただきありがとうございます。現在、私は白い点をクリックすることができます。 –

+0

2番目のセクションでは、現在のクリックがヒットしているかどうかのテストを分割して、3番目のセクションとしてwhileループを使用することができます。 – adamdc78

+0

それを手に入れました。ご協力いただきありがとうございます! @ adamdc78 –