2017-07-11 13 views
1

Java AWTを使用して以下のシャッフルパズルを作成しました。プログラムの始めに、あなたが見ることができるように、すべての数字が順番に並んでいます。繰り返さずに乱数を生成したい。これを行う方法? Randomクラスを使用する必要がありますか?乱数を1から8まで反復せずに生成する方法はありますか?

import java.awt.*; 
import java.awt.event.*; 
class Puzzle extends Frame implements ActionListener 
{ 
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9; 
    Puzzle() 
    { 
     setTitle("Shuffle"); 
     setSize(500,500); 
     setLayout(new GridLayout(3,3)); 
     setVisible(true); 
     Font f=new Font("Arial",Font.BOLD,100); 

     b1=new Button("1"); 
          b1.setFont(f); 
          b1.addActionListener(this); 
     b2=new Button("2"); 
          b2.setFont(f); 
          b2.addActionListener(this); 
     b3=new Button("3"); 
          b3.setFont(f); 
          b3.addActionListener(this); 
     b4=new Button("4"); 
          b4.setFont(f); 
          b4.addActionListener(this); 
     b5=new Button("5"); 
          b5.setFont(f); 
          b5.addActionListener(this); 
     b6=new Button("6"); 
          b6.setFont(f); 
          b6.addActionListener(this); 
     b7=new Button("7"); 
          b7.setFont(f); 
          b7.addActionListener(this); 
     b8=new Button("8"); 
          b8.setFont(f); 
          b8.addActionListener(this); 
     b9=new Button(" "); 
          b9.setFont(f); 
          b9.addActionListener(this); 

     add(b1); add(b2); add(b3); 
     add(b4); add(b5); add(b6); 
     add(b7); add(b8); add(b9); 




    } 
    public void actionPerformed(ActionEvent ae) 
    { 
     if(ae.getSource() == b1) 
     { 
      if(b2.getLabel() == " ") 
      { 
       b2.setLabel(b1.getLabel()); 
       b1.setLabel(" "); 
      } 
     else if(b4.getLabel() == " ") 
      { 

       b4.setLabel(b1.getLabel()); 
       b1.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b2) 
     { 
      if(b1.getLabel() == " ") 
      { 
       b1.setLabel(b2.getLabel()); 
       b2.setLabel(" "); 
      } 
     else if(b3.getLabel() == " ") 
      { 

       b3.setLabel(b2.getLabel()); 
       b2.setLabel(" "); 
      } 
      else if(b5.getLabel() == " ") 
      { 

       b5.setLabel(b2.getLabel()); 
       b2.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b3) 
     { 
      if(b2.getLabel() == " ") 
      { 
       b2.setLabel(b3.getLabel()); 
       b3.setLabel(" "); 
      } 
     else if(b6.getLabel() == " ") 
      { 

       b6.setLabel(b3.getLabel()); 
       b3.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b4) 
     { 
      if(b1.getLabel() == " ") 
      { 
       b1.setLabel(b4.getLabel()); 
       b4.setLabel(" "); 
      } 
     else if(b5.getLabel() == " ") 
      { 

       b5.setLabel(b4.getLabel()); 
       b4.setLabel(" "); 
      } 
      else if(b7.getLabel() == " ") 
      { 

       b7.setLabel(b4.getLabel()); 
       b4.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b5) 
     { 
      if(b2.getLabel() == " ") 
      { 
       b2.setLabel(b5.getLabel()); 
       b5.setLabel(" "); 
      } 
     else if(b4.getLabel() == " ") 
      { 

       b4.setLabel(b5.getLabel()); 
       b5.setLabel(" "); 
      } 
      else if(b6.getLabel() == " ") 
      { 

       b6.setLabel(b5.getLabel()); 
       b5.setLabel(" "); 
      } 
      else if(b8.getLabel() == " ") 
      { 

       b8.setLabel(b5.getLabel()); 
       b5.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b6) 
     { 
      if(b3.getLabel() == " ") 
      { 
       b3.setLabel(b6.getLabel()); 
       b6.setLabel(" "); 
      } 
     else if(b5.getLabel() == " ") 
      { 

       b5.setLabel(b6.getLabel()); 
       b6.setLabel(" "); 
      } 
     else if(b9.getLabel() == " ") 
      { 

       b9.setLabel(b6.getLabel()); 
       b6.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b7) 
     { 
      if(b4.getLabel() == " ") 
      { 
       b4.setLabel(b7.getLabel()); 
       b7.setLabel(" "); 
      } 
     else if(b8.getLabel() == " ") 
      { 

       b8.setLabel(b7.getLabel()); 
       b7.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b8) 
     { 
      if(b5.getLabel() == " ") 
      { 
       b5.setLabel(b8.getLabel()); 
       b8.setLabel(" "); 
      } 
     else if(b7.getLabel() == " ") 
      { 

       b7.setLabel(b8.getLabel()); 
       b8.setLabel(" "); 
      } 
      else if(b9.getLabel() == " ") 
      { 

       b9.setLabel(b8.getLabel()); 
       b8.setLabel(" "); 
      } 

     } 
     if(ae.getSource() == b9) 
     { 
      if(b6.getLabel() == " ") 
      { 
       b6.setLabel(b9.getLabel()); 
       b9.setLabel(" "); 
      } 
     else if(b8.getLabel() == " ") 
      { 

       b8.setLabel(b9.getLabel()); 
       b9.setLabel(" "); 
      } 

     } 

    } 
    public static void main(String args[]) 
    { 

     new Puzzle(); 
    } 
} 
+2

ていることにラップすることができます - 8、その後、代わりにそれをシャッフル? java.util.Collections.shuffle(List ) – Daedric

+0

シャッフルを試みましたか?そのコードを表示してください。そして、はい、[Math.random()](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random())は良い出発点です。 – Amber

+0

b1 = new Button( "1")の代わりに、私は次のようにしました: [code] ランダムrand = new Random(); int a = rand.nextInt(); b1 =新規ボタン(+ a); ですが、番号が繰り返されます。 –

答えて

1

あなたはこのような何かを行うことができます。

// global scope 
List<Integer> container = new ArrayList<>(); 

//populate it 
for(int i =1; i<=8; i++){ 
    container.add(i); 
} 
.......... 
.......... 
.......... 
public static int getRandom(){ 
    //to refill it if ya need to call it more than 8 times 
    //if(container.size()==0){for(int i=1; i<=8; i++){container.add(i);} 
    Random generator = new Random(); 

    Integer randomNumber = container.get(generator.nextInt(container.size())); 

    container.remove(randomNumber); 

    return randomNumber; 
} 

をさらに、ラベル/、単にあなたが例のために行う1ランダム8までの固有の番号を持つすべてのボタンに名前を付けたい場合:

b1=new Button(String.valueOf(getRandom())); 

など。

+0

ありがとうございます。 –

+0

@VishnuVardhanそれを返すと、それは決して繰り返されません。メソッドを呼び出すたびに一意の数値が返されます。しかし、メソッドを8回以上呼び出す場合は、それを補充する場合には...少し変更を加えることができます。 – Yahya

+0

私はあなたの言ったことを実装しました。しかし、ランダムに生成された数値をボタンにどのように追加すればよいですか? –

0

一般的な答え:

編集:私は謝罪は、ここでは基本的に他の男が掲載同じことです。彼はそれに最初に着いた。あなたのために

ArrayList<Integer> draw = new ArrayList<Integer>(); 
for(int i = 1; i<=8; i++){ 
    draw.add(i); //Add every number once 
} 

//Every time you call it, you pick a random item from the list. 
//Then you remove it from it to prevent being recalled. Therefore it won't be re-accesed. 
Random r = new Random(); 
int placeholder = r.nextInt(draw.size()); 
int num = draw.get(placeholder); 
draw.remove(placeholder); 

、これはあなたが何をすべきかです:

... 
ArrayList<Button> buttonlist = new ArrayList<Button>(); /// ADD THIS. Put this outside the method/function. 
... 
Puzzle(){ 
    setTitle("Shuffle"); 
    setSize(500,500); 
    setLayout(new GridLayout(3,3)); 
    setVisible(true); 
    Font f=new Font("Arial",Font.BOLD,100); 

    b1=new Button("1"); 
         b1.setFont(f); 
         b1.addActionListener(this); 
         buttonlist.add(b1); /// ADD THIS 
    b2=new Button("2"); 
         b2.setFont(f); 
         b2.addActionListener(this); 
         buttonlist.add(b2); /// ADD THIS 
    b3=new Button("3"); 
         b3.setFont(f); 
         b3.addActionListener(this); 
         buttonlist.add(b3); /// ADD THIS 
    b4=new Button("4"); 
         b4.setFont(f); 
         b4.addActionListener(this); 
         buttonlist.add(b4); /// ADD THIS 
    b5=new Button("5"); 
         b5.setFont(f); 
         b5.addActionListener(this); 
         buttonlist.add(b5); /// ADD THIS 
    b6=new Button("6"); 
         b6.setFont(f); 
         b6.addActionListener(this); 
         buttonlist.add(b6); /// ADD THIS 
    b7=new Button("7"); 
         b7.setFont(f); 
         b7.addActionListener(this); 
         buttonlist.add(b7); /// ADD THIS 
    b8=new Button("8"); 
         b8.setFont(f); 
         b8.addActionListener(this); 
         buttonlist.add(b8); /// ADD THIS 
    b9=new Button(" "); 
         b9.setFont(f); 
         b9.addActionListener(this); 
         buttonlist.add(b9); /// ADD THIS 
    Random r = new Random(); /// ADD THIS 
    while(buttonlist.size() != 0){ /// ADD THIS 
     int placeholder = r.nextInt(buttonlist.size()); /// ADD THIS 
     Button button = buttonlist.get(placeholder); /// ADD THIS 
     add(button); /// ADD THIS AFTER REPLACING all the original add(b1)... 
     buttonlist.remove(placeholder); /// ADD THIS 
    } /// ADD THIS 
} 

これはそれを個々の名前をgivingg推奨者の他の回答からは少し異なっている、私はこの方法が少しうまくいくと信じてb1はラベル3を持っていません。このようにして、順番をシャッフルする答えは、ランダムな順序ではなく、b1、b2、b3 ...となります。これはボーダー上のボタンを実際にシャッフルしながら順序を保持します。そうしないと、オブジェクトを識別するのが難しくなります。

+0

ありがとうございます。ボタンにどのように配置すればよいですか?そして、上記の方法は時には同じ数字を繰り返すのですか? –

+0

それはありません。 draw.remove(プレースホルダ)を使ってリストから削除します。 – El8tedN8te

+0

ありがとうございました。とった。 –

0

コレクションから数字をランダムに選択しているので、Random番号ジェネレーターを使用して番号を選択しないでください。Random番号ジェネレーターを使用して残りの数字からピックします。

List<Integer> choices = getNewChoices(); 
int choice = choices.remove(random.nextInt(choices.size()); 

選択肢が最終的にますます小さくなりますが、選択されていない要素がListにまだあります。

あなたはたぶん1から配列を作成する保護法にselectOne

public class Whatever { 

    public void run() { 
     ... 
     List<Integer> choices = getNewChoices(); 
     ... 
     int choice = selectOne(choices); 
    } 

    public List<Integer> getNewChoices() { 
     return Arrays.toList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8}); 
    } 

    public int selectOne(List<Integer> choices) { 
     if (choices.length() > 0) { 
      return choices.remove(random.nextInt(choices.size()); 
     } 
     throw new RuntimeException("out of choices"); 
    } 
} 
関連する問題