java - 在简单的 TicTacToe 游戏中切换大小写

标签 java switch-statement case

我正在编写简单的井字棋游戏。一切都很顺利,直到我要换转弯。我创建了 switch-case 来在 X 转和 O 转之间切换。不幸的是,当我单击第一个单元格时,它会绘制 X,但随后它会在下一个单元格上绘制 Os。

这是主类:

public class TicTacToe_10 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    JFrame ticTacToe = new Game_frame();
    ticTacToe.setTitle("TicTacToe Game");
    ticTacToe.setSize(600, 600);
    ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ticTacToe.setLocationRelativeTo(null);
    ticTacToe.setVisible(true);
}

这是我的 Cell 类,其中包含 Click 类,其中包含 switch-case:

public class Cell extends JPanel{

//dial of cell


protected static String dial="";
  String cturn=getTurn();


public Cell(){
    setBorder(new LineBorder(Color.black,1));
    addMouseListener(new Click());
}

//get and set dials

public static String getDial(){
    return dial;
}

public void setDial(String d){
    dial=d;
    repaint();
}

//defining shapes
@Override
protected void paintComponent(Graphics g){
   super.paintComponent(g);

   if (dial.equals("X")){
       g.drawLine(10, 10, getWidth()-10, getHeight()-10);
       g.drawLine(getWidth()-10, 10, 10, getHeight()-10);
   }

   if(dial.equals("O")){
       g.drawOval(10, 10, getWidth()-20, getHeight()-20);
   }

}


private class Click extends MouseAdapter
   {
       @Override
       public void mouseClicked(MouseEvent e)
       {

           // if the cell is empty 
           if (dial.equals(""))
               setDial(cturn);


           else
           {
               switch (cturn) {
                   case "X":
                       cturn="O";
                       setDial(cturn);
                       break;
                   case "O":
                       cturn="X";
                       setDial(cturn);
                       break;
               }


           }
       }
   } 

}

还有 game_frame 类,它只保存单元格:

public class Game_frame extends JFrame{

    private static String turn="X";

    //cell grid
    private  Cell[][] cells = new Cell[3][3];


    public Game_frame(){
        JPanel panel = new JPanel (new GridLayout(3,3,0,0));
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                panel.add(cells[i][j] = new Cell());

        panel.setBorder(new LineBorder (Color.red,1));

        add(panel,BorderLayout.CENTER);
    }
    public static String getTurn(){
        return turn;
    } 
}

最佳答案

难道是你做错了什么? 但我真的不知道,因为我试图测试你的代码,但这不起作用,因为似乎缺少一种方法:/ 我觉得应该是这样的:

switch (dial) {
    case "X":
        cturn="O";
        setDial(cturn);
        break;
    case "O":
        cturn="X";
        setDial(cturn);
        break;
}

关于java - 在简单的 TicTacToe 游戏中切换大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454979/

相关文章:

java - 将属性文件添加到类路径但收到 FileNotFoundException

Javascript switch 语句根据情况执行一些相同的功能

c++ - 案例标签不需要空格?

mysql - 1 列 2 结果的总和每个结果不同!如此简单但无法正常工作

sql - psql:案例用法

java - 如何验证是否使用任何参数调用了方法?

java - 使用 JpaRepository 的可调用语句

java - 将 switch 语句与 imageview 可见性连接起来

mysql - 基于空值生成值的情况

java - 在 dom4j 中指定 XML 路径