⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 five.java

📁 五子棋的源码。自己刚写的。算法还不错。不过意铺待改进。
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package Stet;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
 * @author SUNNY
 * @2007-11-10
 * 五子棋
 */

@SuppressWarnings("serial")
public class Five extends JFrame
    implements Runnable
{
    private class Grid extends JPanel
        implements MouseListener
    {

        @SuppressWarnings("unchecked")
		public void mousePressed(MouseEvent mouseevent)
        {
            if(GameState == 2)
            {
                int i = mouseevent.getButton();
                if(i == 1)
                {
                    if(value == 0)
                    {
                        menX = x;
                        menY = y;
                        steps.add(this);
                        setValue(1);
                        WinCheck(true);
                        flag[x][y] = "1";
                        if(GameState == 2)
                            if(StepCount == 0)
                            {
                                FiveRandom();
                            } else
                            {
                                turn = false;
                                FiveAI();
                                if(!turn)
                                    FiveRandom();
                            }
                    }
                } else
                if(steps.size() != 0)
                    undo();
            }
        }

        public void mouseEntered(MouseEvent mouseevent)
        {
            if(GameState == 2)
                setSelected(true);
        }

        public void mouseExited(MouseEvent mouseevent)
        {
            if(GameState == 2)
                setSelected(false);
        }

        public void mouseClicked(MouseEvent mouseevent)
        {
        }

        public void mouseReleased(MouseEvent mouseevent)
        {
        }

        public Dimension getPreferredSize()
        {
            return new Dimension(20, 20);
        }

        public Dimension getMinimumSize()
        {
            return getPreferredSize();
        }

        public int getLocX()
        {
            return x;
        }

        public int getLocY()
        {
            return y;
        }

        public int getValue()
        {
            return value;
        }

        public void Initial()
        {
            value = grid[x][y] = 0;
            flag[x][y] = "0";
            powerP[x][y] = 0;
            powerC[x][y] = 0;
            selected = false;
            repaint();
        }

        public void setValue(int i)
        {
            locX = x;
            locY = y;
            value = grid[x][y] = i;
            repaint();
        }

        public void setSelected(boolean flag1)
        {
            selected = flag1;
            repaint();
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            if(GameState != 0 && GameState != 1)
                g.drawImage(board, 0, 0, this);
            if(value != 0)
            {
                temp = value != 1 ? white : black;
                g.drawImage(temp, 4, 4, this);
            }
        }

        int x;
        int y;
        int value;
        boolean selected;

        public Grid(int i, int j)
        {
            super();
            x = i;
            y = j;
            addMouseListener(this);
        }
    }


    @SuppressWarnings("unchecked")
	Five()
    {
        super();
        cl = getClass().getClassLoader();
        tk = Toolkit.getDefaultToolkit();
        length = 15;
        StepCount = 0;
        grid = new int[length][length];
        boarder = length - 1;
        powerP = new int[length][length];
        powerC = new int[length][length];
        flag = new String[length][length];
        line = "";
        comboP = new int[length][length];
        comboC = new int[length][length];
        displace_x = 0;
        displace_y = 0;
        steps = new ArrayList();
        dir2 = new boolean[8];
        font = new Font("new_font", 1, 20);
        grids = new Grid[length][length];
        white = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/white.png"));
        black = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/black.png"));
        title = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/title.png"));
        board = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007//board.png"));
        boardPanel = new JPanel();
        boardPanel.setLayout(new GridLayout(length, length, 0, 0));
        boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        for(int i = 0; i < length; i++)
        {
            for(int j = 0; j < length; j++)
            {
                grids[i][j] = new Grid(i, j);
                boardPanel.add(grids[i][j]);
                powerP[i][j] = 0;
                powerC[i][j] = 0;
                flag[i][j] = "0";
                comboP[i][j] = 0;
                comboC[i][j] = 0;
            }

        }

        bigpanel = new JPanel();
        bigpanel.add(boardPanel, "Center");
        getContentPane().add(bigpanel, "South");
        GameState = 0;
        ThreadStart();
        dialog.setSize(160, 100);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(null);
        final JRadioButton choice[] = new JRadioButton[2];
        ButtonGroup buttongroup = new ButtonGroup();
        JPanel jpanel = new JPanel();
        choice[0] = new JRadioButton("黑子", new ImageIcon(black), true);
        choice[1] = new JRadioButton("白子", new ImageIcon(white));
        for(int k = 0; k < choice.length; k++)
        {
            jpanel.add(choice[k]);
            buttongroup.add(choice[k]);
        }

        JButton jbutton = new JButton("确定");
        JPanel jpanel1 = new JPanel();
        jpanel1.add(jbutton);
        jbutton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
                if(choice[0].isSelected())
                {
                    white = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/white.png"));
                    black = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/black.png"));
                }
                if(choice[1].isSelected())
                {
                    temp = white;
                    white = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/black.png"));
                    black = tk.getImage(cl.getResource("C:/Users/SUNNY/work/Set/bin/Stet/2007/white.png"));
                    setMark(7, 7);
                }
                dialog.dispose();
            }
		


        }
);
        dialog.getContentPane().add(jpanel, "North");
        dialog.getContentPane().add(jpanel1, "Center");
        setIconImage(title);
        setResizable(false);
        setSize(293, 360);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main(String args[])
    {
        Five five = new Five();
        five.setDefaultCloseOperation(3);
    }

    public void WinCheck(boolean flag1)
    {
        check = flag1 ? 1 : -1;
        direction = 0;
        WinCheck2();
    }

    public void WinCheck2()
    {
        count = 1;
        switch(direction)
        {
        case 0: // '\0'
            displace_x = 1;
            displace_y = -1;
            direction = 1;
            break;

        case 1: // '\001'
            displace_x = displace_y = 1;
            direction = 2;
            break;

        case 2: // '\002'
            displace_x = 1;
            displace_y = 0;
            direction = 3;
            break;

        case 3: // '\003'
            displace_x = 0;
            displace_y = 1;
            direction = -1;
            break;
        }
        x = locX + displace_x;
        for(y = locY + displace_y; x >= 0 && x < length && y >= 0 && y < length && grid[x][y] == check; y = y + displace_y)
        {
            count = count + 1;
            x = x + displace_x;
        }

        x = locX - displace_x;
        for(y = locY - displace_y; x >= 0 && x < length && y >= 0 && y < length && grid[x][y] == check; y = y - displace_y)
        {
            count = count + 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -