gpt4 book ai didi

java - 连接四个 WinCheck

转载 作者:行者123 更新时间:2023-12-02 06:19:35 25 4
gpt4 key购买 nike

我正在为我的高中cpt制作连接四。到目前为止,我已经显示了所有内容,但它的胜利检查引起了问题。游戏板上的点是由连接四板上的空点和填充点的单独图像创建的。当跑动并选择点时,轮到他们时自动黄色获胜(颜色为红色和黄色)。感谢您的帮助,如果您还有任何疑问,请询问。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import sun.audio.*;


public class ConnectFourV2p2 {

static JFrame mainWindow;
static JButton firstButton = new JButton("Drop");
static JButton secondButton = new JButton("Drop");
static JButton thirdButton = new JButton("Drop");
static JButton fourthButton = new JButton("Drop");
static JButton fifthButton = new JButton("Drop");
static JButton sixthButton = new JButton("Drop");
static JButton seventhButton = new JButton("Drop");
static JPanel[][] gridArray = new JPanel[6][7];
static String[][] spotOnGrid = new String[6][7];
static JLabel emptyLabel = new JLabel();
static JPanel emptyPanel;
static ImageIcon emptyBox;
static JLabel redLabel = new JLabel();
static JPanel redPanel;
static ImageIcon redBox;
static JLabel yellowLabel = new JLabel();
static JPanel yellowPanel;
static ImageIcon yellowBox;
static JPanel gridPanel;
static Boolean redTurn = true;

private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == firstButton) {
if(redTurn) {
dropRedBox(0);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(0);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}

if (e.getSource() == secondButton) {
if(redTurn) {
dropRedBox(1);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(1);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


if (e.getSource() == thirdButton) {
if(redTurn) {
dropRedBox(2);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(2);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


if (e.getSource() == fourthButton) {
if(redTurn) {
dropRedBox(3);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(3);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


if (e.getSource() == fifthButton) {
if(redTurn) {
dropRedBox(4);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(4);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


if (e.getSource() == sixthButton) {
if(redTurn) {
dropRedBox(5);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(5);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


if (e.getSource() == seventhButton) {
if(redTurn) {
dropRedBox(6);
if(winCheck("red")){
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );
}
}
else {
dropYellowBox(6);
winCheck("yellow");
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE );}
}


}
}

public static void main(String[] args) {

ButtonHandler listener = new ButtonHandler();
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
gridPanel = new JPanel();

mainPanel.setLayout(new BorderLayout());
gridPanel.setLayout(new GridLayout(6, 7));
buttonPanel.setLayout(new GridLayout(1, 7));
gridPanel.setBackground(new Color(255, 127, 38));



mainPanel.add(gridPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.NORTH);

buttonPanel.add(firstButton);
buttonPanel.add(secondButton);
buttonPanel.add(thirdButton);
buttonPanel.add(fourthButton);
buttonPanel.add(fifthButton);
buttonPanel.add(sixthButton);
buttonPanel.add(seventhButton);

firstButton.addActionListener(listener);
secondButton.addActionListener(listener);
thirdButton.addActionListener(listener);
fourthButton.addActionListener(listener);
fifthButton.addActionListener(listener);
sixthButton.addActionListener(listener);
seventhButton.addActionListener(listener);

mainWindow = new JFrame("Connect Four");
mainWindow.setContentPane(mainPanel);
mainWindow.setSize(884, 730);
mainWindow.setLocationRelativeTo(null);
mainWindow.setVisible(true);
mainWindow.setResizable(false);

createGrid();
resetGrid();

}



public static void createGrid() {
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridArray[j][k] = new JPanel();
gridPanel.add(gridArray[j][k]);
gridArray[j][k].setBackground(new Color(103,110,215));
}
}
}

public static void resetGrid(){
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridArray[j][k].removeAll();
gridArray[j][k].add(new JLabel(new ImageIcon("emptyBox.png")));
gridArray[j][k].revalidate();
spotOnGrid[j][k] = "empty";
}
}
}

public static void dropRedBox(int col) {
for(int j = 5; j >= 0; j--) {
if(spotOnGrid[j][col].equals("empty")) {
spotOnGrid[j][col] = "red";
gridArray[j][col].removeAll();
gridArray[j][col].add(new JLabel(new ImageIcon("redBox.png")));
gridArray[j][col].revalidate();
spotOnGrid[j][col] = "red";
redTurn = false;
break;
}

}
}

public static void dropYellowBox(int col){
for(int j = 5; j >=0; j--) {
if(spotOnGrid[j][col].equals("empty")) {
spotOnGrid[j][col] = "yellow";
gridArray[j][col].removeAll();
gridArray[j][col].add(new JLabel(new ImageIcon("yellowBox.png")));
gridArray[j][col].revalidate();
spotOnGrid[j][col] = "yellow";
redTurn = true;
break;
}
}
}


public static boolean winCheck(String box) {
Boolean winner = false;
int currentRow;
int currentCol;
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
if(spotOnGrid[j][k].equals(box)) {
currentRow = j;
currentCol = k;

if(((j -3) >= 0) && spotOnGrid[j -1][k].equals(box) && spotOnGrid[j -2][k].equals(box) && spotOnGrid[j -3][k].equals(box)) {
return true;
}

if(((j +3) <= 5) && spotOnGrid[j +1][k].equals(box) && spotOnGrid[j +2][k].equals(box) && spotOnGrid[j +3][k].equals(box)) {
return true;
}

if(((k -3) >= 0) && spotOnGrid[j][k -1].equals(box) && spotOnGrid[j][k -2].equals(box) && spotOnGrid[j][k -3].equals(box)) {
return true;
}
if(((k +3) <= 6) && spotOnGrid[j][k +1].equals(box) && spotOnGrid[j][k +2].equals(box) && spotOnGrid[j][k +3].equals(box)) {
return true;
}
else
return false;

}

}

}
return false;
}

}

最佳答案

您忘记将“Yellow win”放入if block 内。这就是为什么它总是显示

此外,您还可以查找按钮的索引,因此不必复制并粘贴类似的代码块

首先声明一个列表

static List<JButton> buttonz = Arrays.asList(
firstButton, secondButton, thirdButton, fourthButton,
fifthButton, sixthButton, seventhButton);

然后在按钮处理程序内部查找

private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//which button?
int buttonIndex = buttonz.indexOf(e.getSource());

//drop box, check win and show message
if (redTurn) {
dropRedBox(buttonIndex);
if (winCheck("red")) {
JOptionPane.showMessageDialog(null, "Red wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE);
}
} else {
dropYellowBox(buttonIndex);
if (winCheck("yellow"))
JOptionPane.showMessageDialog(null, "Yellow wins!", "Winner!", JOptionPane.INFORMATION_MESSAGE);
}
}
}

关于java - 连接四个 WinCheck,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128495/

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