我正在使用Java进行编码,我将在星期五之前完成一个最终项目,并且我完成了所有工作,但我无法在最后修复括号,所以整个事情都搞砸了。由于某种原因,标签列表将无法在我的代码中使用。请看下面。我知道他们都很容易修复,但我似乎无法搞清楚!任何帮助表示赞赏。谢谢!我需要添加/修复哪些括号?
编辑! 到处都没有回到我班级的位置 我定义了它们。 bestMove gameEnd 赢得 移动
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;
}
}
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
下载sublime之类的编辑器,然后在该编辑器中打开该文件。
当您将光标移动到括号位置时,它会显示每个对应的括号。
如果你更正了文件中的缩进,你会发现。
你也可以给jsFormat一个:https://stackoverflow.com/a/12867043/1069083
答案 2 :(得分:0)
对于列表错误,您必须导入java.util。*
import java.util.*
导入LinkedList不包括使用列表。