2016-09-08 1 views
0

tic-tac-toeゲームの勝利状態を評価しようとします。Tic-Tac-Toeの勝利状態を評価

だから私は、forループでボタンのgetText()メソッドを使用してcells[0][i].getText()cells[0][1]cells[0][2]に等しい場合、たとえば、評価しようとしている通常やって終わるものを。
次に、cells[0][2]が空白でない(" "と等しくない)場合は、最初の行が終了します。
問題は、これがうまくいかず、誰かが私にこのアプローチのよりよい代替手段を提供してくれれば感謝します。

package tictactoe; 

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 

import javax.swing.JFrame; 

public class TicTacToe implements ActionListener 
{ 
    JFrame window; 
    static JButton[][]cells=new JButton[3][3]; 
    boolean playing; 
    char turn='X'; 
     public TicTacToe() 
     { 
      //SETS UP THE WINDOW 
      window=new JFrame("TicTacToe"); 
      window.setSize(new Dimension(400,400)); 
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      window.setResizable(false); 
      //**********************// 
      createCells(); 

      window.setVisible(true); 
     } 
public static void main(String[] args) 
{ 
    new TicTacToe(); 
} 
private void createCells() 
{ 
    window.setLayout(new GridLayout(3,3)); 
    for(int i=0;i<cells.length;i++) 
    { 
     for(int j=0;j<cells.length;j++) 
     { 
      cells[i][j]=new JButton(); 
      cells[i][j].addActionListener(this); 
      window.add(cells[i][j]); 
     } 
    } 
} 

@Override 
public void actionPerformed(ActionEvent e) 
{ 
    playing=true; 
    JButton _buttonPressed=(JButton)e.getSource(); 
     while(playing) 
     { 
      if(turn=='X') 
      { 
      _buttonPressed.setText("X"); 
      _buttonPressed.setEnabled(false); 
      turn='O'; 
      } 
      else 
      { 
       _buttonPressed.setText("O"); 
       _buttonPressed.setEnabled(false); 
      } 
    } 

} 
} 
+0

を、あなたは、デバッガを使用してプログラムもののステップしようとしたことがありますか? –

答えて

2

私はあなたが勝者の状態を評価しているのではありませんが、クリックリスナから無限ループを削除しようとしています。

@Override 
public void actionPerformed(ActionEvent e) { 
    JButton _buttonPressed = (JButton) e.getSource(); 

    _buttonPressed.setText("" + turn); 
    _buttonPressed.setEnabled(false); 
    if (turn == 'X') { 
     turn = 'O'; 
    } else { 
     turn = 'X'; 
    } 
} 

そして、これらのほとんどが提案されているので、あなたが望むならば、これを無視して自由に感じます。

TicTacToeクラスをJFrameにする - インスタンス化する必要はありません。

ます:

public class TicTacToe extends JFrame implements ActionListener { 
    private JButton[][] cells = new JButton[3][3]; 
    private char turn = 'X'; 
    private boolean playing; 

    public TicTacToe() { 
     setTitle("TicTacToe"); 
     setSize(new Dimension(400, 400)); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(false); 
     createCells(); 
     pack(); 
    } 

第二に、Swingのアプリを起動する推奨方法は、三目並べの勝者を見つけることに非常に

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      TicTacToe ttt = new TicTacToe(); 
      ttt.setVisible(true); 
     } 
    }); 
} 
+0

ありがとう、私はゲームの状態メソッドをポストしていないが、私はループを削除し、再びそれを試してみましたので、ありがとう! – Twoshock

0

良いアプローチのように独自のスレッド上にありますユーザによる最後の移動がボードを勝利状態にしたかどうかをチェックする必要があるだけである。ユーザーは、位置ボード[x] [y]にXまたはOのいずれかを配置したことも知っています。

それはこのようなものになります。

public bool gameOver(cells, x, y) 
    if cells[0,y] == cells[1,y] == cells[2,y] 
     //win on vertical line 
    if cells[x,0] == cells[x,1] == cells[x,2] 
     //win on horizontal line 
    if x == y and cells[0,0] == cells[1,1] == cells[2,2] 
     //win on diagonal 
    if x == y and cells[0,2] == cells[1,1] == cells[2,0] 
     //win on other diagonal 
    return false 
関連する問題