private ImageIcon player1Img = new ImageIcon("Pieces\\x.png");
private JLabel player1Label = new JLabel(player1Img);
private ImageIcon player2Img = new ImageIcon("Pieces\\o.png");
private JLabel player2Label = new JLabel(player2Img);
public TicTacToeSim() {
setTitle("TicTacToe");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 3));
buildboard();
buildPieceImgList();
buildGameBoard();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
add(gameBoard[i][j]);
}
}
buildButtonBar();
add(new JLabel());
add(buttonBar);
pack();
setVisible(true);
}
private void buildGameBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gameBoard[i][j] = new JPanel();
boardStartImg = new ImageIcon("Pieces\\blank.png");
options = new JLabel(boardStartImg);
gameBoard[i][j].add(options);
}
}
}
private class newGameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int turn = 0;
while (turn < 9) {
for (int i = 0; i < 4; i++) {
Random rand1 = new Random();
Random rand2 = new Random();
int choice1S = rand1.nextInt(3);
int choice2S = rand2.nextInt(3);
gameBoard[choice1S][choice2S].add(player1Label);
turn += 1;
Random rand3 = new Random();
Random rand4 = new Random();
int choice3S = rand3.nextInt(3);
int choice4S = rand4.nextInt(3);
gameBoard[choice3S][choice4S].add(player2Label);
turn += 1;
}
}
}
}
董事会得到建立,但当newgame按钮是按下没有“打破”,但什么也没有发生。我希望两个CPU随机选择一个点,直到一个赢家或平局可以宣布比赛。我不确定是什么导致了这个问题,因为我对在gui中使用2d数组还是个新手。我不知道在这里写什么,因为我已经解释了我的问题,我想让这个做什么。
暂无答案!
目前还没有任何答案,快来回答吧!