6

私は Java を学んでいるので、Java で Tic Tac Toe Program を作成しています。単純なプロジェクトから始めるのが最適だと思いました。これまでの私のコードは次のとおりです。

public class Start {
    public static void main(String[] args) {
    GameTicTacToe gameTicTacToe = new GameTicTacToe();
    gameTicTacToe.windowBirth();

    }
}

と、

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GameTicTacToe implements ActionListener {
    private int gridSize = 3;
    private JButton[] gridButton = new JButton[(gridSize * gridSize)];
    private JPanel grid = new JPanel(new GridLayout(gridSize, gridSize, 0, 0));
    private JFrame windowTicTacToe = new JFrame("Tisk, Task, Toes");
    private int[] gridButtonOwner = new int[(gridSize * gridSize)];
    private int turn = 1;
    private int HolerHor, HolerVer, HolerDia1, HolerDia2;

    Thread winnerBlue = new Thread() {
        public void run() {
            for (int a = 0; a < 4; a++) {
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(Color.BLUE);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(Color.WHITE);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
            }
            for (int i = 0; i < gridButton.length; i++) {
                gridButton[i].setEnabled(true);
                gridButtonOwner[i] = 0;
            }
        }
    };
    Thread winnerRed = new Thread() {
        public void run() {
            for (int a = 0; a < 4; a++) {
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(Color.RED);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(Color.WHITE);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
            }
            for (int i = 0; i < gridButton.length; i++) {
                gridButton[i].setEnabled(true);
                gridButtonOwner[i] = 0;
            }
        }
    };
    public void windowBirth() {
        for (int i = 0; i < gridButton.length; i++) {
            gridButtonOwner[i] = 0;
            gridButton[i] = new JButton("");
            gridButton[i].addActionListener(this);
            gridButton[i].setBackground(Color.WHITE);
            grid.add(gridButton[i]);
        }
        windowTicTacToe.setDefaultCloseOperation(3);
        windowTicTacToe.setLocation(400, 200);
        windowTicTacToe.setPreferredSize(new Dimension(400, 400));
        windowTicTacToe.add(grid);
        windowTicTacToe.pack();
        windowTicTacToe.setVisible(true);
    }
    public void actionPerformed(ActionEvent gridButtonClicked) {
        for (int i = 0; i < gridButton.length; i++) {
            if (gridButtonClicked.getSource() == gridButton[i]) {
                if (turn == 1) {
                    turn = 2;
                    gridButtonOwner[i] = 1;
                    gridButton[i].setBackground(Color.blue);
                    gridButton[i].setEnabled(false);
                } else {
                    turn = 1;
                    gridButtonOwner[i] = 2;
                    gridButton[i].setBackground(Color.red);
                    gridButton[i].setEnabled(false);
                }
            }
        }
        checkWinner();
    }
    public void checkWinner() {
        for (int a = 1; a < 3; a++) {
            HolerDia1 = a;
            HolerDia2 = a;
            for (int b = 0; b < gridSize; b++) {
                HolerHor = a;
                HolerVer = a;
                for (int c2 = 0; c2 < gridSize; c2++) {
                    HolerHor = (HolerHor * gridButtonOwner[((b * gridSize) + c2)])/ a;
                    HolerVer = (HolerVer * gridButtonOwner[((c2 * gridSize) + b)])/ a;
                }
                if (HolerHor == a || HolerVer == a) {
                    winnerAnimation(a);
                }
            }
            for(int h = 0;h < gridSize; h++){
                HolerDia1 = (HolerDia1 * gridButtonOwner[h * (gridSize + 1)]) / a;
                HolerDia2 = (HolerDia2 * gridButtonOwner[(h * (gridSize - 1)) + (gridSize - 1)]) / a;
            }
            if (HolerDia1 == a || HolerDia2 == a) {
                winnerAnimation(a);
            }
        }
    }
    public void winnerAnimation(int b) {
        for (int i = 0; i < gridButton.length; i++) {
            gridButton[i].setEnabled(false);
        }
        if (b == 1){
            winnerBlue.start();
        }else{
            winnerRed.start();
        }
    }
}

これは私の質問です。プレイヤー 1 (青) などのプレイヤーが勝つと、アニメーションが再生されます (ボードが青く点滅します)。しかし、プレイヤー 1 が再び勝つと、プログラムはクラッシュします。

少し調べてみたところ、スレッドを2回開始することはできないため、スレッドを2回開始できないことがわかりました。

スレッドを一時停止し、必要に応じて再起動するにはどうすればよいですか?

4

6 に答える 6

6

新しいスレッドを作成し、必要に応じて実行するだけです。しかし、これに関係なく、Swing スレッド (EDT) を誤って踏むことによる悪意のある断続的なクラッシュを心配する必要がないため、Swing Timer を使用してアニメーションを行う方が簡単で安全です。

たとえば、

   public void myWinner(final Color flashColor) {
      int timerDelay = 300;
      new javax.swing.Timer(timerDelay , new ActionListener() {
         private static final int COUNTER_MAX = 5;
         int counter = 0;

         public void actionPerformed(ActionEvent e) {
            if (counter >= COUNTER_MAX) { // time to stop
               ((Timer)e.getSource()).stop();
               for (int i = 0; i < gridButton.length; i++) {
                  gridButton[i].setBackground(Color.white); // just to be sure
                  gridButton[i].setEnabled(true);
                  gridButtonOwner[i] = 0;
               }
            }
            Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
            for (JButton button : gridButton) {
               button.setBackground(bckgrndColor);
            }
            counter++;
         }
      }).start();
   }
于 2011-04-09T22:34:48.070 に答える
3

あなたの場合、アニメーションスレッドが再生される場合、次のことが必要になる場合があります。

  • スレッドを独自のオブジェクトとしてカプセル化します。たとえば、匿名オブジェクトを作成するRunnableのではなく、匿名オブジェクトを作成します。ThreadRunnable
  • 次に、スレッドを再作成して開始します。

    public void winnerAnimation(int b)
    {
        ...
        if (b == 1)
        {
            animationThread = new Thread(winnerBlue);
            animationThread.start();
        }
        else
        {
            animationThread = new Thread(winnerRed);
            animationThread.start();
        }
    }
    

    Threadところで、 s やsを作成するときは、Swing がスレッドセーフではないという事実を考慮する必要があるかもしれませんRunnable

于 2011-04-09T22:30:38.287 に答える
3

セマフォを使用します。

  1. プログラムの先頭でスレッドを開始します。
  2. 各 run() メソッドは継続的にループする必要があります。
  3. ループの先頭で、適切なセマフォ (赤/青) を acquire() します。
  4. 勝者が検出されたら、適切なセマフォを release() します。
于 2011-04-09T22:31:27.550 に答える
2

スレッドの別のインスタンスを作成することの何が問題になっていますか? スレッドの再利用は厄介で、非常に奇妙な動作につながる可能性があるため、一般的には行うべきではありません。

コードの繰り返しに加えて (スレッドには 1 行の違いしかありませんよね?)、スレッドを内部クラスとして簡単に実装できます。この方法では、匿名インスタンスを使用する必要がなく、何度でも再作成して開始できます。
色をパラメーターとしてこのスレッドに追加することを明確に検討する必要があります。

(ここにコンパイラ エラーがあると確信していますが、私の言いたいことはわかるはずです。)

// other stuff here...

public void winnerAnimation(int b) {
    for (int i = 0; i < gridButton.length; i++) {
        gridButton[i].setEnabled(false);
    }
    if (b == 1){
        new WinnerThread(Color.BLUE).start();
    }else{
        new WinnerThread(Color.RED).start();
    }
}

class WinnerThread extends Thread {
   private Color color;
   public WinnerThread(Color c)
   {
       color = c;
   }

   public void run() {
            for (int a = 0; a < 4; a++) {
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(color);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(Color.WHITE);
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
            }
            for (int i = 0; i < gridButton.length; i++) {
                gridButton[i].setEnabled(true);
                gridButtonOwner[i] = 0;
            }
        }
    }
}
于 2011-04-09T22:47:21.607 に答える
1

Javawait()notify()メソッドを見てください。ここにそれらのチュートリアルがあります。

于 2011-04-09T22:32:00.123 に答える