五子棋再度更新(暂未测试,先记录一下)

/*
        目前仅仅只支持自己测试,待完善

        黑白棋凑够五个格子所有方格全部变成胜利者的颜色,控制台输出胜利者所持有的格子
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Ts {
    public static void main(String[] args) {
        //创建一个指定大小的格子
        JS2 js2 = new JS2();
        js2.setVisible(true);
        js2.init(15, 15);
    }
}

class JS2 extends JFrame implements ActionListener {
    private StringBuilder sBuilder = new StringBuilder();
    JTextField text = new JTextField();
    int rows;//有多少行
    int cols;//有多少列
    public int index = 1;//第几次操作
    List<JButton> jButtonList = new ArrayList<>();//按钮集合

    //生成两个角色
    Map<Integer, List<Integer>> peoper1 = new HashMap<>();//角色操作的记录
    Map<Integer, List<Integer>> peoper2 = new HashMap<>();//角色操作的记录

    JS2() {
    }

    public void init(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        for (int i = 0; i < cols; i++) {
            peoper1.put(i, new ArrayList<>());
            peoper2.put(i, new ArrayList<>());
        }

        setBounds(100, 100, 500, 500);
        setTitle("五子棋");
//        JMenuBar menubar = new JMenuBar();
        this.setLayout(new BorderLayout());
        JPanel p2 = new JPanel();
        this.add(p2, BorderLayout.CENTER);
        p2.setLayout(new FlowLayout());
        p2.setLayout(new GridLayout(rows, cols));    //添加按钮
        for (int col = 0; col < cols; col++) {
            for (int row = 0; row < rows; row++) {
                JButton j = new JButton();
                jButtonList.add(j);
                p2.add(j);
                int finalCol = col;
                int finalRow = row;
                j.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(finalCol + " " + finalRow);
                        j.setEnabled(false);
                        index++;
                        if (index % 2 == 0) {
                            Color color = new Color(0x000000);
                            List<Integer> list = peoper2.get(finalCol);
                            list.add(finalRow);
                            if (isSuccess(peoper2)) {
                                System.out.println(peoper2);
                                add(color);
                            }
                            j.setBackground(color);
                        } else {
                            Color color = new Color(0xFFFFFF);
                            List<Integer> list = peoper1.get(finalRow);
                            list.add(finalCol);
                            if (isSuccess(peoper1)) {
                                System.out.println(peoper1);
                                add(color);
                            }
                            j.setBackground(color);
                        }
                    }
                });
            }
        }
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void add(Color color) {
        for (int i = 0; i < jButtonList.size(); i++) {
            jButtonList.get(i).setBackground(color);
            jButtonList.get(i).setEnabled(false);
        }
    }

    public boolean isSuccess(Map<Integer, List<Integer>> peoper) {
        for (Map.Entry<Integer, List<Integer>> entry : peoper.entrySet()) {
            Integer key = entry.getKey();
            List<Integer> listValue = entry.getValue();
            //判断一行
            for (int k = 0; k < listValue.size(); k++) {
                int initValue = listValue.get(k);
                int count = 1;
                for (int i = 1; i <= 5; i++) {
                    if (listValue.contains(initValue + i)) {
                        count++;
                    }
                    if (count >= 5) {
                        return true;
                    }
                }
            }
            //判断一列
            for (int k = 0; k < listValue.size(); k++) {
                int initValue = listValue.get(k);
                int count = 1;
                for (int i = 1; i <= 5; i++) {
                    if (peoper.containsKey(key + i) && peoper.get(key + i).contains(initValue)) {
                        count++;
                    }
                    if (count >= 5) {
                        return true;
                    }
                }
            }
            //判断一撇
            for (int k = 0; k < listValue.size(); k++) {
                int initValue = listValue.get(k);
                int count = 1;
                for (int i = 1; i <= 5; i++) {
                    if (peoper.containsKey(key + i) && peoper.get(key + i).contains(initValue-i)) {
                        count++;
                    }
                    if (count >= 5) {
                        return true;
                    }
                }
            }
            //判断一捺
            for (int k = 0; k < listValue.size(); k++) {
                int initValue = listValue.get(k);
                int count = 1;
                for (int i = 1; i <= 5; i++) {
                    if (peoper.containsKey(key + i) && peoper.get(key + i).contains(initValue+i)) {
                        count++;
                    }
                    if (count >= 5) {
                        return true;
                    }
                }
            }

        }
        return false;
    }

    public void add(List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            int value = list.get(i);

        }

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String lab = e.getActionCommand();
        if (lab.equals("0")) {
            sBuilder.append("0");
            text.setText(sBuilder.toString());
        }
    }

    class button1ActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("????");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值