私の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();
}
}
ここで、 'token'を' x'から '0'に切り替えるのですか? –
個人的には、どのように私がプロデューサ/消費者セマフォであるためにトークンを利用していたかを変更するだろう...しかし、私はatm ..コーディングのように感じていません –
コードをデバッグしようとしましたか?もしそうなら、あなたは何を見つけましたか? – Egl