TicTacToe如果它打领带?

时间:2016-10-21 18:12:49

标签: java tic-tac-toe

我是Java的初学者,我开始做一个tic tac toe游戏。但如果它是一个平局,我找不到怎么办:(

我试图在任何地方添加“else”,但是当我这样做时,它会直接出现“这是一个平局”窗口。

以下是我的代码:

Main.java

    /**
 * 
 */
package Game;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * @author Yasin Demirci
 *
 */
public class Main {
    public static boolean playerTurn = true; 
    public static boolean playerWon = false;
    public static boolean computerWon = false;

    public static tictactoe board = new tictactoe(); 

    public static void main(String[] args) {
        if(board.isVisible() == false){
            board.setVisible(true);
        }
    }
        public static void checkforwin(){
            if(board.button1.getText().equals("X")){
                if(board.button4.getText().equals("X")){
                    if(board.button7.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                        board.setVisible(false);
                    }
                }
            }

            if(board.button1.getText().equals("X")){
                if(board.button5.getText().equals("X")){
                    if(board.button9.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button1.getText().equals("X")){
                if(board.button2.getText().equals("X")){
                    if(board.button3.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button3.getText().equals("X")){
                if(board.button5.getText().equals("X")){
                    if(board.button7.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button3.getText().equals("X")){
                if(board.button6.getText().equals("X")){
                    if(board.button9.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button7.getText().equals("X")){
                if(board.button8.getText().equals("X")){
                    if(board.button9.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button4.getText().equals("X")){
                if(board.button5.getText().equals("X")){
                    if(board.button6.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }


            if(board.button2.getText().equals("X")){
                if(board.button5.getText().equals("X")){
                    if(board.button8.getText().equals("X")){
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
                    }
                }
            }



            if(board.button1.getText().equals("O")){
                if(board.button4.getText().equals("O")){
                    if(board.button7.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }

            if(board.button1.getText().equals("O")){
                if(board.button5.getText().equals("O")){
                    if(board.button9.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button1.getText().equals("O")){
                if(board.button2.getText().equals("O")){
                    if(board.button3.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button3.getText().equals("O")){
                if(board.button5.getText().equals("O")){
                    if(board.button7.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button3.getText().equals("O")){
                if(board.button6.getText().equals("O")){
                    if(board.button9.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button7.getText().equals("O")){
                if(board.button8.getText().equals("O")){
                    if(board.button9.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button4.getText().equals("O")){
                if(board.button5.getText().equals("O")){
                    if(board.button6.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }


            if(board.button2.getText().equals("O")){
                if(board.button5.getText().equals("O")){
                    if(board.button8.getText().equals("O")){
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
                    }
                }
            }

        if(playerWon == true || computerWon == true){
            board.setVisible(false);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在checkforwin()函数中,我会添加一个名为tie initialized为true的本地布尔值。

当任一玩家获胜时发生的所有if语句都会将该布尔值更改为false。

然后在末尾添加一个if语句来检查布尔值是否仍然为真,如果是,则导致一个平局。

if (tie)
{
  JOptionPane.showMessageDialog(new JFrame(), "Game was a tie!");
}