我有一个Jframe,上面有两个Jpanel。其中一个面板上有9个jpanel排列在网格布局上,每个jpanel都有自己的网格布局和9个jbuttons。这是一个基于tic tac toe的棋盘游戏。我在81个按钮的每个按钮上都有一个动作监听器,它根据转动的顺序改变按钮的颜色。我为计算机播放器编写了一个minimax算法来对抗。这是我遇到的问题。在minimax算法完成之前,按钮不会更新。所以基本上当人类点击一个按钮进行移动时,直到AI完成其代码运行之后才会对电路板进行更新。我希望Jframe,jpanel和jbuttons在两次移动之间刷新。我已尝试在代码中的各个位置对jframe进行修复,我也尝试重新绘制按钮或面板。我搞不清楚了。
此外,AI代码在完成运行之前未显示刷新。如果我设置了两个AI来互相玩,我看到的是几分钟后游戏的结束。
如何让整个电路板刷新,以便我可以在移动之间看到电路板?
如果有帮助,这是我的董事会课程的一部分:提前感谢。
import java.awt.*;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.*;
public class TBoard
{
private int player;
private boolean GameOn = true;
private int LastGroup;
private int LastCell;
private int MoveCount = 0;
private ArrayList<TCell> LegalMoves;
private Color[] Pcolor;
private TGroup[] groups;
private JFrame Frame = new JFrame("Inception Tic Tac Toe");
private JPanel[] GrPanels;
private JPanel BigPanel;
private JPanel SidePanel;
private TCell WHO;
private JButton Reset = new JButton("New Game");
private JButton UndoBtn = new JButton("Undo");
private String[] Colors = { "RED", "BLACK", "BLUE", "PINK", "GREEN", "YELLOW" };
private JLabel lblp0 = new JLabel("Player 1");
private JLabel lblp1 = new JLabel("Player 2");
private JComboBox Player0;
private JComboBox Player1;
private DumbPlayer NimWit;
private SmartPlayer Newton;
private SmartPlayer Jenny;
public TBoard()
{
this.LegalMoves = new ArrayList<>();
player = 0;
initialSetUp();
NimWit = new DumbPlayer(this);
Newton = new SmartPlayer(this,7,1,1);
Jenny = new SmartPlayer(this,7,2,1);
}
private void initialSetUp()
{
Pcolor = new Color[2];
Pcolor[0] = Color.RED;
Pcolor[1] = Color.DARK_GRAY;
WHO = new TCell(player,Pcolor[0],Pcolor[1]);
//Frame.setLayout(new GridLayout(3,3,10,10));
BigPanel = new JPanel(new GridLayout(3,3,10,10));
Player0 = new JComboBox(Colors);
Player0.setName("Player0");
Player0.setSelectedIndex(0);
Player0.addActionListener(new ColorSelect(this));
Player1 = new JComboBox(Colors);
Player1.setName("Player1");
Player1.setSelectedIndex(1);
Player1.addActionListener(new ColorSelect(this));
BigPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
SidePanel = new JPanel();
SidePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
SidePanel.setLayout(new BoxLayout(SidePanel, BoxLayout.PAGE_AXIS));
SidePanel.add(Box.createRigidArea(new Dimension(0,3)));
SidePanel.add(WHO);
SidePanel.add(Box.createRigidArea(new Dimension(0,8)));
SidePanel.add(Reset);
SidePanel.add(Box.createRigidArea(new Dimension(0,8)));
SidePanel.add(UndoBtn);
SidePanel.add(Box.createRigidArea(new Dimension(0,100)));
SidePanel.add(lblp0);
SidePanel.add(Player0);
SidePanel.add(Box.createRigidArea(new Dimension(0,8)));
SidePanel.add(lblp1);
SidePanel.add(Player1);
SidePanel.add(Box.createRigidArea(new Dimension(0,300)));
Frame.add(BigPanel,BorderLayout.CENTER);
Frame.add(SidePanel,BorderLayout.EAST);
groups=new TGroup[9];
GrPanels = new JPanel[9];
for(int i=0; i<9; i++)
{
GrPanels[i] = new JPanel(new GridLayout(3,3,3,3));
BigPanel.add(GrPanels[i]);
groups[i]=new TGroup(i,Pcolor[0],Pcolor[1]);
for(int j=0;j<9;j++)
{
groups[i].getCell(j).addActionListener(new CellSelect(this));
groups[i].getCell(j).addMouseListener(new CellHover(this,groups[i],groups[i].getCell(j)));
GrPanels[i].add(groups[i].getCell(j));
}
}
Reset.addActionListener(new ResetGame(this));
UndoBtn.addActionListener(new UndoMove(this));
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setSize(700, 600);
Frame.setVisible(true);
//cycleTurn();
}
private void cycleTurn()
{
switch(player) // eventually change to check for the player type and then play that type
{
case 0:
//Jenny.play();
break;
case 1:
// NimWit.setMoves(LegalMoves);
Newton.play();
break;
// case dumb:
// break;
// case Smart:
// break;
}
}
public void play(int g,int c)
{
if(Validate(g,c) && GameOn)
{
//Record for Undo
LastGroup = g;
LastCell = c;
MoveCount++;
UndoBtn.setEnabled(true);
//Set Cell Color
groups[g].setCellState(c,player);
//Check for group win
if(GroupWin(g,c))
{
// check for Game win
GameWin(g);
}
//activate group(s)
if(GameOn)
{
ActivateGroup(c);
}
//next players turn
if(player == 1)
{
player = 0;
}else
{
player = 1;
}
WHO.setState(player);
cycleTurn();
}
}