gpt4 book ai didi

Java GUI 卡住井字游戏

转载 作者:行者123 更新时间:2023-11-30 07:46:02 25 4
gpt4 key购买 nike

我用 Java 编写了 Tic-Tac-Toe。 我似乎遇到的问题是,当(人类)玩家 1 和(计算机)玩家 2 之间存在联系时,GUI 卡住。我在“Buttonlistener”类和“方法”中都创建了一个 tieCheck 来捕捉平局。

我的程序的工作方式是,当按下一个按钮时,它会将一个值传递给方法类中的数组。在这个数组中,1 = 玩家 1,2 = 玩家 2。

人类玩家总是先走,所以当人类玩家走完 4 次后,我会在最后一个回合结束前使用 tieCheck(turncount) 检查平局;此方法然后利用方法类中的 tieCheck() 将 1 放在最后一个位置,然后检查获胜者。如果未找到获胜者,则返回 true。然后 ButtonListener 类中的 tieCheck() 将禁用所有按钮,并说“这是平局”。 但是,这些都不起作用。该程序仍将允许我进行最后一步,并将导致我必须使用任务管理器关闭的卡住窗口。请帮忙!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MediumPanel extends JPanel {

private static final long serialVersionUID = 1L;
private JButton playAgain;
private JButton[] buttons = new JButton[9];

private JLabel label, turn;

public MediumPanel() {

ButtonListener listener = new ButtonListener();

Font f1 = new Font("Arial", Font.BOLD , 100);
Font f2 = new Font("Arial", Font.PLAIN, 50);

JPanel ButtonPanel = new JPanel();
ButtonPanel.setLayout(new GridLayout(3, 3));

for (int i = 0; i <= 8; i++) {
buttons[i] = new JButton("");
buttons[i].addActionListener(listener);
buttons[i].setBackground(Color.white);
buttons[i].setFont(f1);
ButtonPanel.add(buttons[i]);
}

playAgain = new JButton();

label = new JLabel();
label.setFont(f2);

turn = new JLabel("");
turn.setFont(f1);

playAgain.addActionListener(listener);
playAgain.setText("Click to Play Again");
playAgain.setFont(f2);

setBackground(Color.green);
setLayout(new BorderLayout());
setPreferredSize(new Dimension(500, 500));

add(playAgain, BorderLayout.SOUTH);
add(label, BorderLayout.NORTH);
add(ButtonPanel);
}

private class ButtonListener implements ActionListener {

Methods method = new Methods();

public void reset() {

label.setText("");

for (int i = 0; i <= 8; i++) {

buttons[i].setEnabled(true);
buttons[i].setText("");
}

method.reset();
}

// inserts
public void insertG(int num) {

for (int i = 0; i <= 8; i++) {

if (num == i) {
buttons[i].setText("O");
buttons[i].setEnabled(false);
}
}
}

public void disable() {

for(int i=0; i<=8; i++) {

buttons[i].setEnabled(false);

}

}

// Checks array using tieCheck from Methods class for a tie
public void tieCheck(int turncount) {

if (turncount == 4 && method.tieCheck() == true) {

disable();
label.setText("It's a tie!");
}
}

// Checks for buttons being pressed
public void actionPerformed(ActionEvent event) {

int turncount = 0;


//Resets array board, GUI buttons, and label when clicked
if (event.getSource() == playAgain) {
reset();
}



// Button 0
if (event.getSource() == buttons[0]) {



buttons[0].setText("X");
buttons[0].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(0, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();



} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();
}


}

}
// Button 1
if (event.getSource() == buttons[1]) {

buttons[1].setText("X");
buttons[1].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(1, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
// Button 2
if (event.getSource() == buttons[2]) {

buttons[2].setText("X");
buttons[2].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(2, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
// Button 3
if (event.getSource() == buttons[3]) {



buttons[3].setText("X");
buttons[3].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(3, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
// Button 4
if (event.getSource() == buttons[4]) {



buttons[4].setText("X");
buttons[4].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(4, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
// Button 5
if (event.getSource() == buttons[5]) {


buttons[5].setText("X");
buttons[5].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(5, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
//Button 6
if (event.getSource() == buttons[6]) {

buttons[6].setText("X");
buttons[6].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(6, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}

}

}
// Button 7
if (event.getSource() == buttons[7]) {

buttons[7].setText("X");
buttons[7].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(7, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}
//Button 8
if (event.getSource() == buttons[8]) {

buttons[8].setText("X");
buttons[8].setEnabled(false);
turncount++;
tieCheck(turncount);

method.insertArray(8, 1);

if (method.winCheck(1) == 1) {

label.setText("You Won!");
disable();

} else {

insertG(method.smartMove(1, 2));

if (method.winCheck(2) == 1) {

label.setText("You Lost!");
disable();

}


}

}

}
}
}



import java.util.*;

public class Methods {

Random rand = new Random();
Scanner scan = new Scanner(System.in);

// represents Tick-Tack-Toe Play Field
int[] board = new int[9];

// resets board array
public void reset() {

for (int i = 0; i < 9; i++) {
board[i] = 0;
}

}

// inserts player on a specific spot
public void insertArray(int spot, int player) {
board[spot] = player;
}

// for hard mode
public void expertMove(int player1, int player2) {

}

// for medium
public int smartMove(int player1, int player2) {

boolean turntaken = false;

for (int i = 0; i < 9; i++) {

if (board[i] == 0) {

board[i] = player2;

if (winCheck(player2) == 1) {

return i;

} else {
board[i] = 0;
}

}

}

for (int i = 0; i < 9; i++) {

if (board[i] == 0) {

board[i] = player1;

if (winCheck(player1) != 1) {

board[i] = 0;

} else {

board[i] = player2;
return i;
}
}
}
// If the opposite player is not about to win, then Computer goes randomly

if (turntaken == false) {
return randomMove(player2);
}

return 0;
}

// For easy mode and also utilized in smartMove() for medium mode
public int randomMove(int player) {

int rnum = 0;

rnum = rand.nextInt(8);
while (emptyCheck(rnum) != true) {

rnum = rand.nextInt(8);

}

board[rnum] = player;

return rnum;
}

//Returns 1 if player won the game
public int winCheck(int player) {

for (int ii = 0; ii <= 2; ii++) {
if (board[ii] == player && board[ii + 3] == player && board[ii + 6] == player)
return 1;
}
for (int z = 0; z <= 6; z = z + 3) {
if (board[z] == player && board[z + 1] == player && board[z + 2] == player)
return 1;
}
if (board[0] == player && board[4] == player && board[8] == player) {
return 1;
}
if (board[2] == player && board[4] == player && board[6] == player) {
return 1;
}
return 0;
}

//Returns true if tie
public boolean tieCheck() {

for(int i=0;i < 9; i++) {

if(board[i] == 0) {

board[i] = 2;

if(winCheck(1) != 1 && winCheck(2) != 1) {

return true;

}else {
board[i] = 0;
}

}
}
return false;
}

// Checks if empty: True if empty/false if taken by player
public boolean emptyCheck(int rnum) {

if (board[rnum] == 0) {
return true;
} else {
return false;
}

}

}

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;


public class Driver {


public static void main(String []args) {

JTabbedPane difficulty = new JTabbedPane();
//difficulty.addTab("Easy", new EasyPanel());
difficulty.addTab("Medium", new MediumPanel());
//difficulty.addTab("Hard", new HardPanel());

Font f = new Font("Arial", Font.PLAIN, 20);
difficulty.setFont(f);




JFrame frame = new JFrame("Tic-Tac-Toe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(difficulty);



frame.pack();
frame.setVisible(true);



}

最佳答案

在每次点击时,您设置turncount = 0:

    // Checks for buttons being pressed
public void actionPerformed(ActionEvent event) {

int turncount = 0;

但是在你的方法tieCheck中:

        // Checks array using tieCheck from Methods class for a tie
public void tieCheck(int turncount) {

if (turncount == 4 && method.tieCheck() == true) {

disable();
label.setText("It's a tie!");
}
}

您检查 turncount == 4 但它始终是 1。您应该将 turncount 变量从本地更改为全局。

然后在方法 randomMove 中,你有一个无限循环:

    // For easy mode and also utilized in smartMove() for medium mode
public int randomMove(int player) {

int rnum = 0;

rnum = rand.nextInt(8);
while (emptyCheck(rnum) != true) { // <--------- HERE

rnum = rand.nextInt(8);

}

关于Java GUI 卡住井字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com