2017-08-09 15 views
0

私のTicTacToeはターンを変えません。私がセルを押すと、それは にマーク 'x'のセルだけをマークします。言い換えれば、 'x'ピクチャのみをロードし、他の '0'はロードしません。このゲームは、誰かが勝ったときには認識されますが、勝利したターンでは最後のマークを描きません。前もって感謝します。TicTacToeはターンを変更しません

public class Frame extends JFrame{ 


private Cell cell[][] = new Cell[3][3]; 
private JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0)); 

Frame(){ 
    setSize(300, 300); 
    setName("TicTacToe"); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
    for(int i=0;i<3;i++){ 
     for(int j=0; j<3; j++){ 
      panel.add(cell[i][j] = new Cell()); 
     } 
    } 
     add(panel); 
    } 
public boolean CheckWin(char token){ 
    for(int i=0; i<3; i++){ 
     for(int j=0;j<3;j++){ 
      if(cell[i][0].getToken() == token && 
        cell[i][1].getToken() == token 
        && cell[i][2].getToken() == token){ 
       return true; 
      } 
      if(cell[0][j].getToken() == token && 
        cell[1][j].getToken() == token 
        && cell[2][j].getToken() == token){ 
       return true; 
      } 
      } 
     } 
    if(cell[0][0].getToken() == token && 
      cell[1][1].getToken() == token && 
      cell[2][2].getToken() == token){ 
    return true; 
    } 
    return false; 
} 
public boolean IsFull(){ 
    for(int i=0; i<3; i++){ 
     for(int j=0;j<3;j++){ 
      if(cell[i][j].getToken() == ' '){ 
       return false; 
      } 
     } 
} 
    return true; 

} 


public class Cell extends JPanel{ 

private char whoseTurn = 'x'; 
private char token = ' '; 


Cell(){ 
    setBorder(new LineBorder(Color.red, 1)); 
    addMouseListener(new MyMouseListener()); 
} 
public char getToken() { 
    return token; 
} 

public void setToken(char token) { 
    this.token = token; 
    MakePictures(); 

} 
public void MakePictures(){ 
    if(getToken() == 'x'){ 
     ImageIcon image = new ImageIcon(getClass().getResource("/Pictures/x.png")); 
     JLabel label = new JLabel(image); 
     pack(); 
     add(label); 
     setVisible(true); 
    } 
    else if(getToken() == '0'){ 
     ImageIcon image = new ImageIcon(getClass().getResource("/Pictures/0.png")); 
     JLabel label = new JLabel(image); 
     add(label); 
     pack(); 
     setVisible(true); 
    } 
    } 


    public class MyMouseListener extends MouseAdapter{ 


     public void mouseClicked(MouseEvent e){ 
      if(token == ' ' && whoseTurn != ' ') 
       setToken(whoseTurn); 

      if(CheckWin(token)){ 
       whoseTurn = ' '; 
       System.out.println("Voitit"); 

      } 
       else if(IsFull()){ 
        System.out.println("Tasapeli"); 
        whoseTurn = ' '; 
       } 
       else{ 
        whoseTurn = (whoseTurn == 'x') ? '0' : 'x'; 
        System.out.println(whoseTurn); 

      } 
     } 
    } 
} 
} 

ここはメインクラスです。

public class TicTacToe { 

public static void main(String[] args) { 
    Frame frame = new Frame(); 
} 

} 
+0

ここで、 'token'を' x'から '0'に切り替えるのですか? –

+1

個人的には、どのように私がプロデューサ/消費者セマフォであるためにトークンを利用していたかを変更するだろう...しかし、私はatm ..コーディングのように感じていません –

+1

コードをデバッグしようとしましたか?もしそうなら、あなたは何を見つけましたか? – Egl

答えて

0

この宣言は、それは外セルクラスでなければなりません

private char whoseTurn = 'x'; 

間違っています。そうでない場合、各セルは、その「ターン」が「x」に設定されているので、ターンは決して変化しません。

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

public class Frame extends JFrame { 

    private char whoseTurn = 'x'; 

乾杯。

+0

私はここでこれを変えるべきだと思う:theTurn =(whichTurn == 'x')? '0': 'x';System.out.println(itsTurn); –

+0

変更されます。しかし、すべてのセルには独自の 'withTurn'変数があり、変数を共有する必要があります。 – sdsc81

+0

@ ArtturiThhtinenはうまくいきましたか?それをaprovedとしてマークできますか? – sdsc81

関連する問題