急需一个用eclipse写的五子棋代码,能运行的 5

希望能告诉我怎么让这些代码运行起来... 希望能告诉我怎么让这些代码运行起来 展开
 我来答
Green142
推荐于2017-12-16 · TA获得超过355个赞
知道小有建树答主
回答量:190
采纳率:100%
帮助的人:105万
展开全部

// 运行代码很简单:在eclipse的项目中新建一个类Five.java 把下面的代码放入该类即可

// 游戏运行:上下左右键控制方向,enter键确定棋子落定之处,黑白交替执行

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

 * 借鉴别人的。。。 

 */

public class Five extends KeyAdapter {

    boolean start = false;

    JFrame frame;

    Thread timerThread;

    int h, l;// 定义变量

    int pans[][] = new int[11][11]; // 变量初始值

    int gbx = 5;

    int gby = 5;

    int ziShu = 0;

    int shui = 0;


    Five() {

        frame = new JFrame("五子棋");

        frame.getContentPane().add(new Screen());

        frame.addKeyListener(this);

        frame.setSize(12 * 40 + 5 * 2, 12 * 40 + 22 + 5 * 2 + 100);// 窗口大小

        frame.setVisible(true);

        frame.repaint();

        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

            });// 关闭窗体

        }

        public void keyPressed(KeyEvent evt) {

        switch (evt.getKeyCode()) {

            case KeyEvent.VK_ENTER :

                if (ziShu % 2 == 0)// 子数来判断谁赢

                   {

                        heiXia();

                    } else {

                        baiXia();

                    }


                shui = shuiYing();

                break;

            case KeyEvent.VK_ESCAPE :

                System.exit(1);

                break;

            case KeyEvent.VK_LEFT :

                guangBiaoDong(-1, 0);// 光标动的坐标

                break;

            case KeyEvent.VK_RIGHT :

                guangBiaoDong(1, 0);// 光标动的坐标

                break;

            case KeyEvent.VK_DOWN :

                guangBiaoDong(0, 1);// 光标动的坐标

                break;

            case KeyEvent.VK_UP :

                guangBiaoDong(0, -1);// 光标动的坐标

                break;

            }

            frame.repaint();

}

@SuppressWarnings("serial")

class Screen extends JComponent {

            public void paint(Graphics g) {

                    Graphics2D g2d = (Graphics2D) g;

                    xianShi(g2d);// 画图

            }

}


public static void main(String[] args) {

        new Five();

}

    void xianShi(Graphics2D g2d)// 显示

    {

            xianShiQiPan(g2d);// 显示棋盘

            xianShiQiZis(g2d);// 显示旗子

            xianShiGuangBiao(g2d);// 显示光标


            g2d.setFont(new Font("STXingkai", 1, 30));// 0708第二小组

            g2d.setColor(Color.black);

            g2d.drawString("欢乐五子棋Java版", 280, 580);


            g2d.setFont(new Font("stCaiyun", 1, 40));

            if (shui == 0)// 判断

            {

                    g2d.setColor(Color.black);

                    xianShiTiShiXia(g2d);// 显示谁下棋

             } else {

                    g2d.setColor(Color.black);

                    xianShiTiShiYing(g2d); // 显示谁赢棋

            }

    }


    void guangBiaoDong(int fangX, int fangY)// //光标动

    {

                if (gbx + fangX > 10 || gbx + fangX < 0 || gby + fangY > 10|| gby + fangY < 0) {

                } else {

                    gbx = gbx + fangX;

                    gby = gby + fangY;

                }

    }


    void heiXia()// 黑棋

    {

        if (pans[gby][gbx] == 0) {

                pans[gby][gbx] = -1;

                ziShu++;

        }


    }


    void baiXia()// 白棋

    {

        if (pans[gby][gbx] == 0) {

            pans[gby][gbx] = 1;

            ziShu++;

        }

    }


    int shuiYing()// 0: 1:bai -1:hei谁赢拉

    {

        return kanFangShuiYing(1, 0) + kanFangShuiYing(0, 1)

                    + kanFangShuiYing(1, 1) + kanFangShuiYing(1, -1);

    }

    // ---------------------------------------------

    void xianShiQiPan(Graphics2D g2d)// 显示棋盘

    {

        g2d.setColor(Color.pink);

        g2d.fillRect(0, 0, 40 * 12, 40 * 12);// 0,0,40*12,40*12

        g2d.setColor(Color.BLACK);

    // 行格

    for (int i = 0; i < 11; i++)// 40,行*40+40,440,行*40+40

    {

        g2d.drawLine(40, i * 40 + 40, 440, i * 40 + 40);

    }

    // 列格

    for (int k = 0; k < 11; k++)// 列*40+40,40,列*40+40,440

    {

        g2d.drawLine(k * 40 + 40, 40, k * 40 + 40, 440);

    }

    }


    void xianShiQiZis(Graphics2D g2d)// 显示旗子

    {

        for (int h = 0; h < 11; h++) {

                for (l = 0; l < 11; l++) {

                        switch (pans[h][l]) {

                                    case 1 :

                                                g2d.setColor(Color.BLACK);

                                                g2d.fillOval(40 - 15 + l * 40, 40 - 15 + h * 40, 30, 30);// (40-                                                                15+l*40,40-15+h*40,30,30);

                                    break;

                                    case -1 :

                                                g2d.setColor(Color.white);

                                                g2d.fillOval(40 - 15 + l * 40, 40 - 15 + h * 40, 30, 30);// (40-                                                               15+l*40,40-15+h*40,30,30);

                                    break;

                                    case 0 :

                                    break;

                        }

                }

        }

    }

    

    void xianShiGuangBiao(Graphics2D g2d)// 显示光标

    {

        g2d.setColor(Color.blue);

        g2d.fillOval(40 - 5 + gbx * 40, 40 - 5 + gby * 40, 10, 10);

    }


    @SuppressWarnings("deprecation")

    void xianShiTiShiYing(Graphics2D g2d)// 显示谁赢

    {

        switch (shui) {

                            case 1 :

                                // 黑棋

                                        g2d.setColor(Color.black);

                                        g2d.drawString("黑棋win", 200, 520);

                                        JOptionPane pane = new JOptionPane("    黑棋胜利^_^");

                                        JDialog dialog = pane.createDialog(frame, "提示...");

                                        dialog.show(); 

                                        System.exit(0);

                            break;

                            case -1 :

                            // 白棋

                                        g2d.setColor(Color.magenta);

                                        g2d.drawString("白棋win", 200, 520);

                                        JOptionPane pane1 = new JOptionPane("    白棋胜利^_^");

                                        JDialog dialog1 = pane1.createDialog(frame, "提示...");

                                        dialog1.show();

                                        System.exit(0);

                            break;

        }

}


void xianShiTiShiXia(Graphics2D g2d)// //显示提示下一个棋子

{

        switch (ziShu % 2) {

        case 0 :

                    g2d.setColor(Color.magenta);// 黑棋

                    g2d.drawString("提示:", 10, 520);

                    g2d.setColor(Color.orange);

                    g2d.drawString("白", 150, 520);

                    g2d.setColor(Color.magenta);

                    g2d.drawString("棋下", 195, 520);

        break;

        case 1 :

                    g2d.setColor(Color.magenta);// 黑棋

                    g2d.drawString("提示:", 10, 520);

                    g2d.setColor(Color.black);

                    g2d.drawString("黑", 150, 520);

                    g2d.setColor(Color.magenta);

                    g2d.drawString("棋下", 195, 520);

        break;

        }

    }


    int kanFangShuiYing(int fx, int fy) {

                        int kanx = gbx;

                        int kany = gby;

                        int shu = 1;

        // zuo

        while (kanx + fx >= 0 && kanx + fx < 11 && kany + fy >= 0&& kany + fy < 11&& pans[kany                   + fy][kanx + fx] == pans[gby][gbx]) {

                shu++;

                kany = kany + fy;

                kanx = kanx + fx;

        }

        // you

        kanx = gbx;

        kany = gby;

        while (kanx - fx >= 0 && kanx - fx < 11 && kany - fy >= 0&& kany - fy < 11&& pans[kany -                     fy][kanx - fx] == pans[gby][gbx]) {

                    shu++;

                    kany = kany - fy;

                    kanx = kanx - fx;

         }


        return (shu >= 5 ? pans[gby][gbx] : 0);

    }

};


运行结果:

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式