Java 我的支架有问题吗
我正在用Java编写代码,周五我有一个最后的项目,我已经完成了所有的工作,但是最后我无法修复我的括号,所以整个事情都搞砸了。此外,由于某些原因,标记列表在我的代码中不起作用。请看下面。我知道这两个问题都很容易解决,但我似乎无法解决!感谢您的帮助。谢谢我需要添加/修复哪些括号 编辑! 到处都有,不回想起我班上的位置 我定义了它们。 最佳移动 游戏结束 赢 移动Java 我的支架有问题吗,java,string,list,tic-tac-toe,brackets,Java,String,List,Tic Tac Toe,Brackets,我正在用Java编写代码,周五我有一个最后的项目,我已经完成了所有的工作,但是最后我无法修复我的括号,所以整个事情都搞砸了。此外,由于某些原因,标记列表在我的代码中不起作用。请看下面。我知道这两个问题都很容易解决,但我似乎无法解决!感谢您的帮助。谢谢我需要添加/修复哪些括号 编辑! 到处都有,不回想起我班上的位置 我定义了它们。 最佳移动 游戏结束 赢 移动 包ttt; 导入ttt位置; 导入java.awt.Color; 导入java.awt.Component; 导入java.awt.Dim
包ttt;
导入ttt位置;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.GridLayout;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.SwingUtilities;
导入javax.swing.JButton;
导入javax.swing.JPanel;
导入javax.swing.text.Position;
公开课游戏{
位置=新位置();
//word按钮在分号旁边有一个红色标记
受保护组件按钮;
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
私人游戏;
私有int idx;
公开募捐{
JFrame=新的JFrame(“Java TTT”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的GridLayout(3,3));
游戏=新游戏();
最终JButton[]按钮=新JButton[9];
对于(int i=0;i<9;i++){
idx=i;
最终JButton按钮=新JButton();
按钮[i]=按钮;
按钮。设置首选尺寸(新尺寸(100100));
按钮。背景(颜色。黑色);
按钮。设置不透明(true);
button.setFont(新字体(null,Font.PLAIN,100));
button.AddMouseStener(新的MouseStener(){
公共无效MouseEvent e{}
public void mousePressed(MouseEvent e){}
公共无效mouseExited(MouseEvent e){}
公共无效mouseenterned(MouseEvent e){}
@凌驾
公共无效mouseClicked(MouseEvent e){
按钮.setText(“+game.position.turn”);
移动游戏(idx);
如果(!game.position.gameEnd()){
int best=game.position.bestMove();
按钮[best].setText(“+game.position.turn”);
游戏。移动(最佳);
}
if(game.position.gameEnd()){
字符串消息=”;
如果(游戏位置赢('x')){
信息=“你赢了!(勒布朗先生,我做的游戏让你赢了,我认为这应该是一个[退出再玩]);
}else if(游戏位置赢('o')){
message=“Computer赢了!(我想我仍然应该得到一个A[退出继续玩]”;
}否则{
message=“这是一场平局!(很好的尝试[再次退出比赛]”;
}
showMessageDialog(空,消息);
}
}
});
框架。添加(按钮);
}
//实现组件
frame.pack();
frame.setVisible(true);
}
} );
}
受保护的无效移动(int idx)
{
//单词位置
位置=位置移动(idx);
//以下括号
}
}
职位类别
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;
}
import java.util.LinkedList;
公共阶级地位{
公共委员会;
公众人物转向;
公共int dim=3;
私有整数mm;
公共职位(){
this.board=”“.toCharArray();
this.turn='x';
}
公共位置(字符[]板,字符旋转){
this.board=董事会;
this.turn=turn;
}
公共位置(字符串str){
this.board=str.toCharArray();
this.turn='x';
}
公共位置(字符串str、字符turn){
this.board=str.toCharArray();
this.turn=turn;
}
公共字符串toString(){
返回新字符串(板);
}
公共位置移动(int idx){
char[]newBoard=board.clone();
新船[idx]=转弯;
返回新位置(新板,转弯='x'?'o':'x');
}
//列表类型不能是泛型编辑
公共整数[]可能的移动(){
java.util.List List=新建LinkedList();
对于(int i=0;iimport 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;
}
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.*