import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game_02 {
//登陆界面;用户名:王旭 密码:20000601
public void landing(String[][] array) {
JFrame jFrame = new JFrame("登陆界面");
jFrame.setSize(600, 600);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
Font font = new Font("宋体", Font.BOLD, 25);
JLabel jLabel = new JLabel("用户:");
jLabel.setFont(font);
JLabel jLabel1 = new JLabel("密码:");
jLabel1.setFont(font);
JTextField jTextField = new JTextField();
jTextField.setPreferredSize(new Dimension(200, 35));
jTextField.setFont(font);
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setPreferredSize(new Dimension(200, 35));
jPasswordField.setFont(font);
JPanel jPanel = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton jButton = new JButton("登陆");
jButton.setPreferredSize(new Dimension(100, 50));
jButton.setFont(font);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str1 = new String(jPasswordField.getPassword());
if (str1.equals("20000601") && jTextField.getText().equals("王旭")) {
frame(array);
} else {
JOptionPane.showMessageDialog(
jPanel,
"请您重新输入哦!",
"嗯哼?",
JOptionPane.WARNING_MESSAGE
);
}
jTextField.setText(null);
jPasswordField.setText(null);
}
});
JButton jButton1 = new JButton("退出");
jButton1.setPreferredSize(new Dimension(100, 50));
jButton1.setFont(font);
jButton1.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(
jFrame,
"确认退出吗?小主",
"提示",
JOptionPane.YES_NO_CANCEL_OPTION
);
if (result == 0) {
System.exit(0);
}
});
Box box = Box.createVerticalBox();
Component vbox = Box.createVerticalStrut(80);
Component vbox1 = Box.createVerticalStrut(80);
box.add(vbox);
jPanel.add(jLabel);
jPanel.add(jTextField);
box.add(jPanel);
jPanel2.add(jLabel1);
jPanel2.add(jPasswordField);
box.add(jPanel2);
jPanel1.add(jButton);
jPanel1.add(jButton1);
box.add(jPanel1);
box.add(vbox1);
jFrame.setContentPane(box);
jFrame.setVisible(true);
}
//菜单界面
private JFrame frame(String[][] array) {
JFrame jFrame = new JFrame("五子棋菜单界面");
jFrame.setSize(400, 300);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setResizable(true);
Font font = new Font("楷体", Font.BOLD, 15);
JPanel jPanel = new JPanel();
JButton btn01 = new JButton("开始游戏");
btn01.setPreferredSize(new Dimension(100, 50));
btn01.setFont(font);
btn01.addActionListener(e -> {
ChessBord(jFrame, array);
rule(jFrame);
});
JButton btn02 = new JButton("退出游戏");
btn02.setPreferredSize(new Dimension(100, 50));//王旭20000103
btn02.setFont(font);
btn02.addActionListener(e -> {
jFrame.dispose();
});
Component vbox = Box.createVerticalStrut(40);
Component vbox2 = Box.createVerticalStrut(40);
Box vbox1 = Box.createVerticalBox();
vbox1.add(vbox);
vbox1.add(btn01);
vbox1.add(vbox2);
vbox1.add(btn02);
jPanel.add(vbox1);
jFrame.setContentPane(jPanel);
jFrame.setVisible(true);
return jFrame;
}
//定义游戏规则
private void rule(JFrame relativeWindow) {
JFrame jFrame = new JFrame("规则");
jFrame.setSize(500, 300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
JLabel jLabel = new JLabel();
jLabel.setSize(500, 500);
jLabel.setFont(new Font("宋体", Font.BOLD, 25));
jLabel.setText("<html>请在方框内输入指令,如“ab”,<br>其中前者a为行," +
"后者b为列,输入<br>错误会提醒," +
"按确定键后重新输入<br>重来操作为棋盘恢复为无子棋盘,<br>本程序无悔棋操作,要认真下子哦!</html>");
jFrame.setLocationRelativeTo(null);
JButton jButton = new JButton("确定");
jButton.setVerticalTextPosition(SwingConstants.BOTTOM);//
jButton.setHorizontalTextPosition(SwingConstants.CENTER);//
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jFrame.dispose();
}
});
jPanel.add(jLabel);
jPanel.add(jButton);
jFrame.setContentPane(jPanel);
jFrame.setVisible(true);
}
//设置五子棋棋盘及操作游戏执行
private String[][] ChessBord(JFrame relativeWindow, String[][] array) {
Font font = new Font("宋体", Font.BOLD, 20);
Font font2 = new Font("宋体", Font.PLAIN, 30);
String[] array_02 = {" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};
JFrame chessBorder = new JFrame();
chessBorder.setTitle("五子棋棋盘");
chessBorder.setSize(1000, 700);
chessBorder.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
JButton[][] grids = new JButton[17][17];
jPanel.setLayout(new GridLayout(18, 18));
jPanel.setSize(1200, 700);
for (int i = 0; i <= 16; i++) {
for (int j = 0; j <= 16; j++) {
grids[i][j] = new JButton();
grids[i][j].setBackground(Color.WHITE);
if (i == 0 || j == 0) {
grids[i][j].setText(array_02[j]);
if (j == 0) {
grids[i][j].setText(array_02[i]);
}
} else {
grids[i][j].setText(array[i][j]);
}
jPanel.add(grids[i][j]);
}
}
Dimension dimension1 = new Dimension(50, 30);
JTextField jTextField = new JTextField();
jTextField.setPreferredSize(dimension1);
jTextField.setFont(font);
JLabel jLabel = new JLabel(" @方:");
jLabel.setFont(font);
jTextField.addActionListener(e -> {
});
JTextField jTextField1 = new JTextField(null);
jTextField1.setPreferredSize(dimension1);
jTextField1.setFont(font);
JLabel jLabel1 = new JLabel(" $方:");
jLabel1.setFont(font);
jTextField1.addActionListener(e -> {
});
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
JPanel jPanel8 = new JPanel();
JButton button = new JButton("确定");
JButton button1 = new JButton("确定");
JButton button2 = new JButton("重来");
JButton button3 = new JButton("退出");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("".equals(jTextField.getText()) != true) {
int x = Integer.valueOf(jTextField.getText().charAt(0) - 96);
int y = Integer.valueOf(jTextField.getText().charAt(1) - 96);
if (x <= 16 && x >= 1 && y <= 16 && y >= 1) {
for (int j = 0; j <= 16; j++) {
for (int i = 0; i <= 16; i++) {
if (x == j && y == i) {
if (array[i][j] == " ") {
array[i][j] = "@";
grids[x][y].setFont(font2);
grids[x][y].setText(array[i][j]);
jTextField.setText(null);
boolean systm = findWinner_01(array);
if (systm == true) {
JFrame jFrame1 = new JFrame("结果");
jFrame1.setLocationRelativeTo(chessBorder);
jFrame1.setSize(400, 400);
JPanel jPanel9 = new JPanel();
JLabel jLabel_01 = new JLabel();
jLabel_01.setFont(new Font("宋体", Font.PLAIN, 40));
jLabel_01.setText(" @方胜利!");
JButton jButton = new JButton("确定");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame1.dispose();
}
});
jPanel9.setLayout(new BorderLayout());
jPanel9.add(jButton, BorderLayout.SOUTH);
jPanel9.add(jLabel_01);
jFrame1.setContentPane(jPanel9);
jFrame1.setLocationRelativeTo(chessBorder);
jFrame1.setVisible(true);
}
} else {
jTextField.setText(null);
reInput();
}
}
}
}
} else {
jTextField.setText(null);
reInput();
}
jPanel.updateUI();
} else {
jTextField.setText(null);
reInput();
}
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("".equals(jTextField1.getText()) != true) {
int x = Integer.valueOf(jTextField1.getText().charAt(0) - 96);
int y = Integer.valueOf(jTextField1.getText().charAt(1) - 96);
if (x <= 16 && x >= 1 && y <= 16 && y >= 1) {
for (int j = 0; j <= 16; j++) {
for (int i = 0; i <= 16; i++) {
if (x == j && y == i) {
if (array[i][j] == " ") {
array[i][j] = "$";
grids[x][y].setText("$");
grids[x][y].setFont(font2);
jTextField1.setText(null);
boolean systm = findWinner_02(array);
if (systm == true) {
JFrame jFrame1 = new JFrame("结果");
jFrame1.setLocationRelativeTo(chessBorder);
jFrame1.setSize(400, 400);
JPanel jPanel10 = new JPanel();
JLabel jLabel_01 = new JLabel();
jLabel_01.setFont(new Font("宋体", Font.PLAIN, 40));
jLabel_01.setText(" $方胜利!");
JButton jButton = new JButton("确定");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame1.dispose();
}
});
jPanel10.setLayout(new BorderLayout());
jPanel10.add(jButton, BorderLayout.SOUTH);
jPanel10.add(jLabel_01);
jFrame1.setContentPane(jPanel10);
jFrame1.setLocationRelativeTo(chessBorder);
jFrame1.setVisible(true);
}
} else {
jTextField1.setText(null);
reInput();
}
}
}
}
jPanel.updateUI();
} else {
jTextField1.setText(null);
reInput();
}
} else {
jTextField1.setText(null);
reInput();
}
}
});
button2.addActionListener(e -> {
for (int i = 0; i <= 16; i++) {
for (int j = 0; j <= 16; j++) {
if (grids[i][j].getText() == "@" || grids[i][j].getText() == "$") {
grids[i][j].setText(" ");
}
}
}
jPanel.updateUI();
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chessBorder.dispose();
}
});
jPanel1.add(jLabel);
jPanel.add(jPanel1);
jPanel2.add(jTextField);
jPanel.add(jPanel2);
jPanel3.add(button);
jPanel.add(jPanel3);
jPanel4.add(jLabel1);
jPanel.add(jPanel4);
jPanel5.add(jTextField1);
jPanel.add(jPanel5);
jPanel6.add(button1);
jPanel.add(jPanel6);
jPanel7.add(button2);
jPanel.add(jPanel7);
jPanel8.add(button3);
jPanel.add(jPanel8);
chessBorder.setLocationRelativeTo(null);
chessBorder.setContentPane(jPanel);
chessBorder.setVisible(true);
return array;
}
//定义重新输入提醒
private void reInput() {
JFrame jFrame2 = new JFrame("提醒");
jFrame2.setSize(400, 400);
JPanel jPanel9 = new JPanel();
JLabel jLabel_02 = new JLabel();
jLabel_02.setFont(new Font("宋体", Font.PLAIN, 30));
jLabel_02.setText(" 输入错误!请您重新输入");
JButton jButton = new JButton("确定");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame2.dispose();
}
});
jPanel9.setLayout(new BorderLayout());
jPanel9.add(jButton, BorderLayout.SOUTH);
jPanel9.add(jLabel_02);
jFrame2.setContentPane(jPanel9);
jFrame2.setLocationRelativeTo(null);
jFrame2.setVisible(true);
}
//定义胜利规则
private boolean findWinner_01(String[][] array) {
boolean end = false;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (array[i][j] == "@") {
if (i >= 5 && i <= 16) {
if (array[i + 1][j] == array[i + 2][j] && array[i + 3][j] == array[i + 4][j]) {
if (array[i + 1][j] == array[i + 3][j] && array[i + 1][j] == "@") {
i = 15;
break;
}
}
}
if (i >= 1 && i <= 12) {
if (array[i - 1][j] == array[i + 1][j] && array[i + 2][j] == array[i + 3][j]) {
if (array[i - 1][j] == array[i + 2][j] && array[i - 1][j] == "@") {
i = 15;
break;
}
}
}
if (i >= 2 && i <= 13) {
if (array[i - 2][j] == array[i - 1][j] && array[i + 1][j] == array[i + 2][j]) {
if (array[i - 2][j] == array[i + 1][j] && array[i - 2][j] == "@") {
i = 15;
break;
}
}
}
if (i >= 3 && i <= 14) {
if (array[i - 3][j] == array[i - 2][j] && array[i - 1][j] == array[i + 1][j]) {
if (array[i - 3][j] == array[i - 1][j] && array[i - 3][j] == "@") {
i = 15;
break;
}
}
}
if (i >= 4 && i <= 15) {
if (array[i - 4][j] == array[i - 3][j] && array[i - 2][j] == array[i - 1][j]) {
if (array[i - 4][j] == array[i - 2][j] && array[i - 4][j] == "@") {
i = 15;
break;
}
}
}
if (j >= 5 && j <= 16) {
if (array[i][j + 1] == array[i][j + 2] & array[i][j + 3] == array[i][j + 4]) {
if (array[i][j + 1] == array[i][j + 3] & array[i][j + 1] == "@") {
i = 15;
break;
}
}
}
if (j >= 1 && j <= 12) {
if (array[i][j - 1] == array[i][j + 1] && array[i][j + 2] == array[i][j + 3]) {
if (array[i][j - 1] == array[i][j + 2] & array[i][j - 1] == "@") {
i = 15;
break;
}
}
}
if (j >= 2 && j <= 13) {
if (array[i][j - 2] == array[i][j - 1] && array[i][j + 1] == array[i][j + 2]) {
if (array[i][j - 2] == array[i][j + 1] & array[i][j - 2] == "@") {
i = 15;
break;
}
}
}
if (j >= 3 && j <= 14) {
if (array[i][j - 3] == array[i][j - 2] && array[i][j - 1] == array[i][j + 1]) {
if (array[i][j - 3] == array[i][j - 1] & array[i][j - 3] == "@") {
i = 15;
break;
}
}
}
if (j >= 4 && j <= 15) {
if (array[i][j - 4] == array[i][j - 3] && array[i][j - 2] == array[i][j - 1]) {
if (array[i][j - 4] == array[i][j - 2] & array[i][j - 4] == "@") {
i = 15;
break;
}
}
}
if (i >= 5 && i <= 16 && j >= 5 && j <= 16) {
if (array[i + 1][j + 1] == array[i + 2][j + 2] && array[i + 3][j + 3] == array[i + 4][j]) {
if (array[i + 1][j + 1] == array[i + 3][j + 3] && array[i + 1][j + 1] == "@") {
i = 15;
break;
}
}
}
if (i >= 1 && i <= 12 && j >= 1 && j <= 12) {
if (array[i - 1][j - 1] == array[i + 1][j + 1] && array[i + 2][j + 2] == array[i + 3][j + 3]) {
if (array[i - 1][j - 1] == array[i + 2][j + 2] && array[i - 1][j - 1] == "@") {
i = 15;
break;
}
}
}
if (i >= 2 && i <= 13 && j >= 2 && j <= 13) {
if (array[i - 2][j - 2] == array[i - 1][j - 1] && array[i + 1][j + 1] == array[i + 2][j + 2]) {
if (array[i - 2][j - 2] == array[i + 1][j + 1] && array[i - 2][j - 2] == "@") {
i = 15;
break;
}
}
}
if (i >= 3 && i <= 14 && j >= 3 && j <= 14) {
if (array[i - 3][j - 3] == array[i - 2][j - 2] && array[i - 1][j - 1] == array[i + 1][j + 1]) {
if (array[i - 3][j - 3] == array[i - 1][j - 1] && array[i - 1][j - 1] == "@") {
i = 15;
break;
}
}
}
if (i >= 4 && i <= 15 && j >= 4 && j <= 15) {
if (array[i - 4][j - 4] == array[i - 3][j - 3] && array[i - 2][j - 2] == array[i - 1][j - 1]) {
if (array[i - 4][j - 4] == array[i - 2][j - 2] && array[i - 2][j - 2] == "@") {
i = 15;
break;
}
}
}
}
}
if (i == 15) {
end = true;
}
}
return end;
}
//定义胜利规则
private boolean findWinner_02(String[][] array) {
boolean end = false;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (array[i][j] == "$") {
if (i >= 5 && i <= 16) {
if (array[i + 1][j] == array[i + 2][j] && array[i + 3][j] == array[i + 4][j]) {
if (array[i + 1][j] == array[i + 3][j] && array[i + 1][j] == "$") {
i = 15;
break;
}
}
}
if (i >= 1 && i <= 12) {
if (array[i - 1][j] == array[i + 1][j] && array[i + 2][j] == array[i + 3][j]) {
if (array[i - 1][j] == array[i + 2][j] && array[i - 1][j] == "$") {
i = 15;
break;
}
}
}
if (i >= 2 && i <= 13) {
if (array[i - 2][j] == array[i - 1][j] && array[i + 1][j] == array[i + 2][j]) {
if (array[i - 2][j] == array[i + 1][j] && array[i - 2][j] == "$") {
i = 15;
break;
}
}
}
if (i >= 3 && i <= 14) {
if (array[i - 3][j] == array[i - 2][j] && array[i - 1][j] == array[i + 1][j]) {
if (array[i - 3][j] == array[i - 1][j] && array[i - 3][j] == "$") {
i = 15;
break;
}
}
}
if (i >= 4 && i <= 15) {
if (array[i - 4][j] == array[i - 3][j] && array[i - 2][j] == array[i - 1][j]) {
if (array[i - 4][j] == array[i - 2][j] && array[i - 4][j] == "$") {
i = 15;
break;
}
}
}
if (j >= 5 && j <= 16) {
if (array[i][j + 1] == array[i][j + 2] & array[i][j + 3] == array[i][j + 4]) {
if (array[i][j + 1] == array[i][j + 3] & array[i][j + 1] == "$") {
i = 15;
break;
}
}
}
if (j >= 1 && j <= 12) {
if (array[i][j - 1] == array[i][j + 1] && array[i][j + 2] == array[i][j + 3]) {
if (array[i][j - 1] == array[i][j + 2] & array[i][j - 1] == "$") {
i = 15;
break;
}
}
}
if (j >= 2 && j <= 13) {
if (array[i][j - 2] == array[i][j - 1] && array[i][j + 1] == array[i][j + 2]) {
if (array[i][j - 2] == array[i][j + 1] & array[i][j - 2] == "$") {
i = 15;
break;
}
}
}
if (j >= 3 && j <= 14) {
if (array[i][j - 3] == array[i][j - 2] && array[i][j - 1] == array[i][j + 1]) {
if (array[i][j - 3] == array[i][j - 1] & array[i][j - 3] == "$") {
i = 15;
break;
}
}
}
if (j >= 4 && j <= 15) {
if (array[i][j - 4] == array[i][j - 3] && array[i][j - 2] == array[i][j - 1]) {
if (array[i][j - 4] == array[i][j - 2] & array[i][j - 4] == "$") {
i = 15;
break;
}
}
}
if (i >= 5 && i <= 16 && j >= 5 && j <= 16) {
if (array[i + 1][j + 1] == array[i + 2][j + 2] && array[i + 3][j + 3] == array[i + 4][j]) {
if (array[i + 1][j + 1] == array[i + 3][j + 3] && array[i + 1][j + 1] == "$") {
i = 15;
break;
}
}
}
if (i >= 1 && i <= 12 && j >= 1 && j <= 12) {
if (array[i - 1][j - 1] == array[i + 1][j + 1] && array[i + 2][j + 2] == array[i + 3][j + 3]) {
if (array[i - 1][j - 1] == array[i + 2][j + 2] && array[i - 1][j - 1] == "$") {
i = 15;
break;
}
}
}
if (i >= 2 && i <= 13 && j >= 2 && j <= 13) {
if (array[i - 2][j - 2] == array[i - 1][j - 1] && array[i + 1][j + 1] == array[i + 2][j + 2]) {
if (array[i - 2][j - 2] == array[i + 1][j + 1] && array[i - 2][j - 2] == "$") {
i = 15;
break;
}
}
}
if (i >= 3 && i <= 14 && j >= 3 && j <= 14) {
if (array[i - 3][j - 3] == array[i - 2][j - 2] && array[i - 1][j - 1] == array[i + 1][j + 1]) {
if (array[i - 3][j - 3] == array[i - 1][j - 1] && array[i - 1][j - 1] == "$") {
i = 15;
break;
}
}
}
if (i >= 4 && i <= 15 && j >= 4 && j <= 15) {
if (array[i - 4][j - 4] == array[i - 3][j - 3] && array[i - 2][j - 2] == array[i - 1][j - 1]) {
if (array[i - 4][j - 4] == array[i - 2][j - 2] && array[i - 2][j - 2] == "$") {
i = 15;
break;
}
}
}
}
}
if (i == 15) {
end = true;
}
}
return end;
}
//程序执行窗口
public static void main(String[] args) {
String[][] array = new String[17][17];
for (int i = 0; i <= 16; i++) {
for (int j = 0; j <= 16; j++) {
array[i][j] = " ";
}
}
Game_02 test = new Game_02();
test.landing(array);
}
}
五子棋
最新推荐文章于 2024-08-15 14:47:41 发布