【问题标题】:Trouble with my brackets我的括号有问题
【发布时间】:2014-12-16 23:08:19
【问题描述】:

我正在使用 Java 进行编码,周五我有一个最终项目,我已经完成了整个工作,但我无法在最后修复我的括号,所以整个事情都搞砸了。同样由于某种原因,标签列表在我的代码中不起作用。请看下文。我知道它们都很容易解决,但我似乎无法弄清楚!任何帮助表示赞赏。谢谢!我需要添加/修复哪些括号?

已编辑! 到处都没有回拨到我的班级位置 我定义了它们。 最佳移动 游戏结束 赢 移动

package ttt;
import ttt.position;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.text.Position;

public class Game {

position position = new position();
// the word button has a red mark next to the semicolon
protected Component button; 


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        private Game game;
        private int idx;

        public void run() {
            JFrame frame = new JFrame("Java TTT");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridLayout(3, 3));
            game = new Game();
            final JButton[] buttons = new JButton[9];
            for (int i = 0; i < 9; i++) {
                idx = i;
                final JButton button = new JButton();
                buttons[i] = button;
                button.setPreferredSize(new Dimension(100, 100));
                button.setBackground(Color.BLACK);
                button.setOpaque(true);
                button.setFont(new Font(null, Font.PLAIN, 100));
                button.addMouseListener(new MouseListener() {
                    public void mouseReleased(MouseEvent e) {}
                    public void mousePressed(MouseEvent e) {}
                    public void mouseExited(MouseEvent e) {}
                    public void mouseEntered(MouseEvent e) {}
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        button.setText("" + game.position.turn);
                        game.move(idx);
                            if (!game.position.gameEnd()) {
                                int best = game.position.bestMove();
                                buttons[best].setText("" + game.position.turn);
                                game.move(best);
                            }
                            if (game.position.gameEnd()) {
                                String message = "";
                                if (game.position.win('x')) {
                                    message = "You won! (Mr. Lebron I made the game let you win, I think that deserves an A [Quit to play again])";
                                } else if (game.position.win('o')) {
                                    message = "Computer won! (I think I still deserve an A [Quit to play again])";
                                } else {
                                    message = "It's a tie! (Good try though [Quit to play again])";
                                }

                                JOptionPane.showMessageDialog(null, message);

                            }
                        }
                });
                frame.add(button);
            }
            // realize components
            frame.pack();
            frame.setVisible(true);
        }
    } );

}

protected void move(int idx) 
{
// the words position
    position = position.move(idx); 
//the following bracket
}

}

职位类别

import java.util.LinkedList;

public class position {
public char[] board;
public char turn;
public int dim = 3;
private Integer mm;

public position() {
    this.board = "       ".toCharArray();
    this.turn = 'x';

}

public position(char[] board, char turn) {
    this.board = board;
    this.turn = turn;
}

public position(String str) {
    this.board = str.toCharArray();
    this.turn = 'x';
}

public position(String str, char turn) {
    this.board = str.toCharArray();
    this.turn = turn;
}

public String toString() {
    return new String(board);
}

public position move(int idx) {
    char[] newBoard = board.clone();
    newBoard[idx] = turn;
    return new position(newBoard, turn == 'x' ? 'o' : 'x');
}

// list type can't be generic edit

public Integer[] possibleMoves() {
    java.util.List<Integer> list = new LinkedList<Integer>();
    for (int i = 0; i < board.length; i++) {
        if (board[i] == ' ') {
            list.add(i);
        }

    }
    Integer[] array = new Integer[list.size()];
    list.toArray(array);

    return array;
}

public boolean win_line(char turn, int start, int step) {
    for (int i = 0; i < 3; i++) {
        if (board[start + step * 1] != turn) {
            return false;
        }
    }
    return true;
}

// calling win here

public boolean win(char turn) {
    for (int i = 0; i < dim; i++) {
        if (win_line(turn, i * dim, 1) || win_line(turn, i, dim)) {
            return true;
        }
    }
    if (win_line(turn, dim - 1, dim - 1) || win_line(turn, 0, dim + 1)) {
        return true;
    }
    {
        return false;
    }
}

@SuppressWarnings("null")
public int minimax() {
    if (win('x')) {
        return 100;
    }
    if (win('o')) {
        return -100;
    }
    if (possibleMoves().length == 0) {
        return 0;
    }
    mm = null;
    for (Integer idx : possibleMoves()) {
        Integer value = null;
        if (mm == null || turn == 'x' && mm < value || turn == 'o'
                && value < mm) {
            mm = value;
        }
    }
    return mm + (turn == 'x' ? -1 : 1);
}

// fix game end here
public boolean gameEnd() {

    return win('x') || win('o') || possibleMoves().length == 0;

}

public int bestMove() {
    Integer mm = null;
    int best = -1;
    for (Integer idx : possibleMoves()) {
        Integer value = move(idx).minimax();
        if (mm == null || turn == 'x' && mm < value || turn == 'o'
                && value < mm) {
            mm = value;
            best = idx;
        }
    }
    return best;
}

}

【问题讨论】:

    标签: java string list tic-tac-toe brackets


    【解决方案1】:

    您在代码中遗漏了很多匹配/右括号。不确定要将方法 possibleMoves() 放在哪里,但发布的原始代码应该如下工作:

    public class Game {
    
        position position = new position();
        // the word button has a red mark next to the semicolon
        protected Component button; 
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                private Game game;
                private int idx;
    
                public void run() {
                    JFrame frame = new JFrame("Java TTT");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridLayout(3, 3));
                    game = new Game();
                    final JButton[] buttons = new JButton[9];
                    for (int i = 0; i < 9; i++) {
                        idx = i;
                        final JButton button = new JButton();
                        buttons[i] = button;
                        button.setPreferredSize(new Dimension(100, 100));
                        button.setBackground(Color.BLACK);
                        button.setOpaque(true);
                        button.setFont(new Font(null, Font.PLAIN, 100));
                        button.addMouseListener(new MouseListener() {
                            public void mouseReleased(MouseEvent e) {}
                            public void mousePressed(MouseEvent e) {}
                            public void mouseExited(MouseEvent e) {}
                            public void mouseEntered(MouseEvent e) {}
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                button.setText("" + game.position.turn);
                                game.move(idx);
                                    if (!game.position.gameEnd()) {
                                        int best = game.position.bestMove();
                                        buttons[best].setText("" + game.position.turn);
                                        game.move(best);
                                    }
                                    if (game.position.gameEnd()) {
                                        String message = "";
                                        if (game.position.win('x')) {
                                            message = "You won! (Mr. Lebron I made the game let you win, I think that deserves an A [Quit to play again])";
                                        } else if (game.position.win('o')) {
                                            message = "Computer won! (I think I still deserve an A [Quit to play again])";
                                        } else {
                                            message = "It's a tie! (Good try though [Quit to play again])";
                                        }
    
                                        JOptionPane.showMessageDialog(null, message);
    
                                    }
                                }
                        });
                        frame.add(button);
                    }
                    // realize components
                    frame.pack();
                    frame.setVisible(true);
                }
            }
        }
    
        protected void move(int idx) 
        {
        // the words position
            position = position.move(idx); 
        //the following bracket
        }
    }
    

    【讨论】:

    • 谢谢你修复它!我一直在拉我的头发试图完成它。现在刚刚进入列表:P
    • 当我用你给我的括号修复它时,现在第 56-65 行都有红线表示方法 gameEnd() 未定义类型位置...?
    【解决方案2】:

    对于列表错误,您必须导入 java.util.*

    import java.util.*
    

    导入LinkedList不包括使用列表。

    【讨论】:

    • 这一行有多个标记 - List 类型不是通用的;它不能用参数 参数化 - List 类型不是通用的;它不能用参数 参数化
    • @AndrewRothberg 尝试将其实例化为 java.util.List list = new LinkedList();
    • @AndrewRothberg 没问题,很高兴为您提供帮助
    【解决方案3】:

    下载像sublime 这样的编辑器并在该编辑器中打开文件。

    当您将光标移动到括号的位置时,它将显示每个对应的括号。

    如果您更正了文件中的缩进,您会发现。

    你也可以试试 jsFormat:https://stackoverflow.com/a/12867043/1069083

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多