吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5811|回复: 17
收起左侧

[Java 转载] Java入门之五子棋实战

  [复制链接]
流星的孤单 发表于 2018-7-23 17:07
本帖最后由 流星的孤单 于 2018-7-25 09:48 编辑

界面
[Java] 纯文本查看 复制代码
package fivechessgame;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;



@SuppressWarnings("serial")
public class MyChessFrame extends JFrame implements MouseListener{
        double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();        //得到电脑屏幕的高和宽
        double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        private int x = 0;
        private int y = 0;
        private int option = 1;                                //设置按钮功能的操作数,初始化为1,功能两人PK,置为2时,功能人机大战
        static boolean White = true;                
        boolean canplay = false;                        //标志数
        String message = "白方先行";                        
        private static int flag;                        //标志数
        private int allchess[][] = new int [11][11];//0表示没棋子,1表示白棋,2表示黑棋
        
        String state = "五子棋规则:\n "
                        + "1. 五子棋专用盘为 11条横线×11条竖线组成,交叉的每个点都可以行棋。\n"
                        + " 2. 游戏分黑白两方,每局由规定白方先行。白方玩家移动鼠标在棋盘中击行棋。\n"
                        + "3. 当白方行棋完毕,转由黑方行棋。游戏结束后黑白方互换颜色。\n"
                        + "胜负判定: 某一方玩家最先在棋盘上形成横、竖、斜连成五连或长连。将获得胜利。\n"; 
        

        public  MyChessFrame() {
                this.setResizable(false);
                this.setVisible(true);                        //设置窗口
                this.setTitle("五子棋");                        //设置标题
                this.setSize(750, 470);                        //设置窗口大小
                this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);        //设置窗口关闭方式
                this.setLocation((int)(width-600)/2, (int)(height-300)/2);//使窗口居中
                this.addMouseListener(this);        //添加鼠标监听器
                Container ct = null;
                ct=this.getContentPane();
                
                getContentPane().setLayout(null);
                // 游戏说明按钮,按下可显示说明文档
                
                Button button_1 = new Button("游戏说明");
                button_1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                JOptionPane.showMessageDialog(getComponent(0), 
                                                state); 
                        }
                });
                button_1.setForeground(Color.BLACK);
                button_1.setBackground(new Color(0, 191, 255));
                button_1.setBounds(605, 241, 99, 35);
                getContentPane().add(button_1);
//                游戏说明按钮,按下可显示相关信息
                Button button_2 = new Button("关于");
                button_2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                JOptionPane.showMessageDialog(getComponent(0), 
                                                "作者:傅佳垚, 学号B14060303,制作时间:2016年8月26日11:58:21\n"
                                +"添加人机大战功能时间:2016年11月28日09:59:14");
                        }
                });
                button_2.setForeground(Color.BLACK);
                button_2.setBackground(new Color(0, 191, 255));
                button_2.setBounds(605, 299, 99, 35);
                getContentPane().add(button_2);
//                游戏说明按钮,按下可退出游戏
                Button button_3 = new Button("退出");
                button_3.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                System.exit(0);
                        }
                });
                button_3.setForeground(Color.BLACK);
                button_3.setBackground(new Color(0, 191, 255));
                button_3.setBounds(605, 353, 99, 35);
                getContentPane().add(button_3);                        
                
                Button button = new Button("重新开始");
                button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                for (int i=0; i<=10; i++){                        //重置棋盘
                                        for (int j=0; j<=10; j++){
                                                allchess[i][j] = 0;
                                                
                                        }
                                }
                                canplay = true;                
                                repaint();
                        }
                });
                button.setBounds(605, 34, 99, 35);
                button.setForeground(Color.BLACK);
                button.setBackground(new Color(0, 191, 255));        
                getContentPane().add(button);
                
                Button button_4 = new Button("两人PK");
                button_4.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                for (int i=0; i<=10; i++){                        //重置棋盘
                                        for (int j=0; j<=10; j++){
                                                allchess[i][j] = 0;
                                                
                                        }
                                }
                                canplay = true;
                                option = 1;
                                repaint();
                        }
                });
                button_4.setForeground(Color.BLACK);
                button_4.setBackground(new Color(0, 191, 255));
                button_4.setBounds(605, 105, 99, 35);
                getContentPane().add(button_4);
                
                Button button_5 = new Button("人机大战");
                button_5.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                for (int i=0; i<=10; i++){                        //重置棋盘
                                        for (int j=0; j<=10; j++){
                                                allchess[i][j] = 0;
                                                
                                        }
                                }
                                option = 2;
                                canplay = true;
                                repaint();
                        }
                });
                button_5.setForeground(Color.BLACK);
                button_5.setBackground(new Color(0, 191, 255));
                button_5.setBounds(605, 180, 99, 35);
                getContentPane().add(button_5);
                                
        }
        
        
        
        public void paint (Graphics g) {        
                Image bimage = null;
                String str = "/images/background.jpg" ;
                bimage = new ImageIcon(this.getClass().getResource(str)).getImage();
                g.drawImage(bimage, 0, 0, this);
                g.setFont(new Font("楷体",Font.BOLD, 30));
                g.drawString("游戏信息:", 200, 75);
                g.drawString( message, 375, 75);
                for (int i=0;i<=10; i++){
                        g.setColor(Color.white);
                        g.drawLine(200, 100+i*30, 500, 100+i*30);//竖线
                        g.drawLine(200+i*30, 100, 200+i*30, 400);//横线
                }

                x = x*30 + 200;
                y = y*30 + 100;
                for (int i=0; i<=10; i++){
                        for (int j=0; j<=10; j++){
                                if (allchess[i][j] == 1){
                                        g.setColor(Color.WHITE);
                                        int ax = i*30 + 200;
                                        int by = j*30 + 100;
                                        g.fillOval(ax-7, by-7, 15, 15);                //画白棋
                                        
                                }
                                if (allchess[i][j] == 2){
                                        g.setColor(Color.BLACK);
                                        int ax = i*30 + 200;
                                        int by = j*30 + 100;
                                        g.fillOval(ax-7, by-7, 15, 15);                //画黑棋
                                        
                                }
                        }
                }
        }
        @Override
        public void mouseClicked(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }@Override
        public void mouseExited(MouseEvent e) {
        }@Override
        public void mousePressed(MouseEvent e) {
                x = e.getX();
                y = e.getY();
                if (canplay){                                                                        //按下按钮时.置为true.可以进入一下内容
                        if (x>=200 && x<=500 && y>=100 && y<=400){
                                x = (x-185)/30;                                                        //得到棋盘的坐标
                                y = (y-85)/30;
                                if(option==1){                                                //PK功能
                                        Player player = new Player (allchess);
                                        player.twoPlayer(x, y, White);
                                        
                                        if (flag==1){
                                                message = "轮到黑方";
                                                flag = 0;                                //初始化静态变量flag的值
                                        }else if(flag == 2){
                                                message = "轮到白方";
                                                flag = 0;
                                        }else{                                        
                                                flag = 0;
                                                JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新落子!");
                                        }
                                }
                                if(option==2){                                        //功能:人机大战
                                        Computer com = new Computer(allchess);
                                        com.ComputerPlayer(x, y, White);
                                        message = "人机大战";
                                        if (flag == 3){
                                                flag = 0;
                                                JOptionPane.showMessageDialog(this, "当前位置已经有棋子,请重新落子!");
                                        }
                                        
                                }
                                
                                if (isWin(x,y,allchess)){
                                        if(allchess[x][y] == 1)
                                                JOptionPane.showMessageDialog(this, "游戏结束!白方获胜!!!");
                                        if(allchess[x][y] == 2)
                                                JOptionPane.showMessageDialog(this, "游戏结束!黑方获胜!!!");
                                        canplay = false;
                                        
                                }
                                this.repaint();
                        }

                }
                
        }@Override
        public void mouseReleased(MouseEvent e) {
        }
        public static boolean isWin(int x, int y,int allchess[][]){
                int color = allchess[x][y];
                int count = 1;
                int i = 1;
                //判断x轴是否有5个相连的棋子
                while ( x+i < 11 && allchess[x+i][y]==color){
                        count++;
                        i++;
                }
                i = 1;
                while ( x-i >= 0 && allchess[x-i][y]==color ){
                        count++;
                        i++;
                }
                if (count >= 5){
                        return true;
                }
                //判断y轴是否有5个相连的棋子
                count = 1;
                i = 1;
                while (y+i < 11 && allchess[x][y+i]==color){
                        count++;
                        i++;
                }
                i = 1;
                while ( y-i >= 0 && allchess[x][y-i]==color){
                        count++;
                        i++;
                }
                if (count >= 5){
                        return true;
                }
                //判断直线y = x 上是否有5个相连的棋子
                count = 1;
                i = 1;
                while (x+i < 11 && y+i < 11 && allchess[x+i][y+i]==color  ){
                        count++;
                        i++;
                }
                i = 1;
                while ( x-i >= 0 && y-i >= 0 && allchess[x-i][y-i]==color){
                        count++;
                        i++;
                }
                if (count >= 5){
                        return true;
                }
                //判断直线y = -x 上是否有5个相连的棋子
                count = 1;
                i = 1;
                while (x-i >= 0 && y+i < 11 && allchess[x-i][y+i]==color ){
                        count++;
                        i++;
                }
                i = 1;
                while ( x+i < 11 && y-i >= 0 && allchess[x+i][y-i]==color ){
                        count++;
                        i++;
                }
                if (count >= 5){
                        return true;
                }
                return false;
        }
        
        public static String setMessage(int chessColor){
                String message = null;
                if (chessColor==1){
                        message = "轮到黑方";
                        return message;
                }else{
                        message = "轮到白方";
                        return message;
                }
        }
        public static void isWhite(boolean white){
                MyChessFrame.White = white;
        }
        public static void changeFlag(int f){
                flag = f;
        }
}




人机算法

[Asm] 纯文本查看 复制代码
package fivechessgame;

import javax.swing.JOptionPane;
import fivechessgame.*;

class Computer {
        private int chessBoard[][] = new int[11][11];

        public Computer(int[][] chessBoard) {
                super();
                this.chessBoard = chessBoard;
        }

        public void ComPrint() {
                System.out.println();
                for (int i = 0; i < 11; i++) {
                        for (int j = 0; j < 11; j++) {
                                System.out.print("chessBoard=" + chessBoard[i][j] + "\t");
                        }
                        System.out.println();
                }
        }

        public void ComputerPlayer(int x, int y, boolean isW) {
                int white = 1;
                int black = 2;

                if (isW) {
                        if (chessBoard[x][y] == 0) {        //若此处无棋.可下子
                                chessBoard[x][y] = white;
                                ComRun();                        
                        } else {
                                MyChessFrame.changeFlag(3);
                        }
                }
        }
        //功能:电脑下棋
        public void ComRun() {
                int count = 0;
                int black = 2;
                int white = 1;
                boolean flag = false;
                int total = 4;                        //设置优先级.4为最高
                for (; total > 0; total--) {
                        for (int i = 0; i < 11; i++) {                //遍历棋盘.寻找白棋
                                for (int j = 0; j < 11; j++) {

                                        if (chessBoard[i][j] == 1) {        //找到白棋.阻止其达到5个
                                                if (Judge(i, j, white, total)) {
                                                        flag = true;
                                                        break;                                //若已经下了棋.则直接跳出循环
                                                }
                                                if (chessBoard[i][j] == 2) {
                                                        flag = Judge(i, j, black, total);
                                                        if (flag == true)
                                                                break;
                                                }
                                        }

                                        if (flag)
                                                break;
                                }
                        }

                        if (flag)
                                break;

                }
        }
        //功能:判断指定颜色的棋是否达到指定数目
        public boolean Judge(int x, int y, int color, int total) {
                int count = 1;
                int i = 1, j = 1;
                int white = 1;
                int black = 2;

                // 判断x轴是否有total个相连的棋子
                while (x + i < 11 && chessBoard[x + i][y] == color) {
                        count++;
                        i++;
                }

                while (x - j >= 0 && chessBoard[x - j][y] == color) {
                        count++;
                        j++;
                }
                if (count == total) {        //若达到total个相连的棋子.下黑棋阻止
                        if (x + i < 11) {
                                if (chessBoard[x + i][y] == 0) {
                                        chessBoard[x + i][y] = black;
                                        return true;
                                }
                        }
                        if (x - j >= 0) {
                                if (chessBoard[x - j][y] == 0) {
                                        chessBoard[x - j][y] = black;
                                        return true;
                                }
                        }
                }
                // 判断y轴是否有total个相连的棋子
                count = 1;
                i = 1;
                j = 1;
                while (y + i < 11 && chessBoard[x][y + i] == color) {
                        count++;
                        i++;
                }

                while (y - j >= 0 && chessBoard[x][y - j] == color) {
                        count++;
                        j++;
                }
                if (count == total) {
                        if (y + i < 11) {
                                if (chessBoard[x][y + i] == 0) {
                                        chessBoard[x][y + i] = black;
                                        return true;
                                }
                        }
                        if (y - j >= 0) {
                                if (chessBoard[x][y - j] == 0) {
                                        chessBoard[x][y - j] = black;
                                        return true;
                                }
                        }
                }
                // 判断直线y = x 上是否有total个相连的棋子
                count = 1;
                i = 1;
                j = 1;
                while (x + i < 11 && y + i < 11 && chessBoard[x + i][y + i] == color) {
                        count++;
                        i++;
                }

                while (x - j >= 0 && y - j >= 0 && chessBoard[x - j][y - j] == color) {
                        count++;
                        j++;
                }
                if (count == total) {
                        if (x + i < 11 && y + i < 11) {
                                if (chessBoard[x + i][y + i] == 0) {
                                        chessBoard[x + i][y + i] = black;
                                        return true;
                                }
                        }
                        if (x - j >= 0 && y - j >= 0) {
                                if (chessBoard[x - j][y - j] == 0) {
                                        chessBoard[x - j][y - j] = black;
                                        return true;
                                }
                        }
                }
                // 判断直线y = -x 上是否有total个相连的棋子
                count = 1;
                i = 1;
                j = 1;
                while (x - i >= 0 && y + i < 11 && chessBoard[x - i][y + i] == color) {
                        count++;
                        i++;
                }

                while (x + j < 11 && y - j >= 0 && chessBoard[x + j][y - j] == color) {
                        count++;
                        j++;
                }
                if (count == total) {
                        if (x - i >= 0 && y + i < 11) {
                                if (chessBoard[x - i][y + i] == 0) {
                                        chessBoard[x - i][y + i] = black;
                                        return true;
                                }
                        }
                        if (x + j < 11 && y - j >= 0) {
                                if (chessBoard[x + j][y - j] == 0) {
                                        chessBoard[x + j][y - j] = black;
                                        return true;
                                }
                        }
                }
                return false;
        }

}



play类
[Java] 纯文本查看 复制代码
package fivechessgame;

import javax.swing.JOptionPane;
import fivechessgame.*;
public class Player {
        private int chessBoard[][] = new int [11][11];
         public Player(int[][] chessBoard) {
                        super();
                        this.chessBoard = chessBoard;
        }
         public void playerPrint (){
                 System.out.println();
                        for (int i=0;i<11; i++){
                                for (int j=0; j<11; j++){
                                        System.out.print("chessBoard=" + chessBoard[i][j]  + "\t");
                                }
                                System.out.println();
                        }
         }

        public void twoPlayer(int x, int y, boolean white) {
                int black;
                int chess;
                int flag = 0;
                String message = null;
                if (chessBoard[x][y] == 0) {
                        if (white) {
                                chessBoard[x][y] = 1;
                                // message = "轮到黑方";
                                MyChessFrame.changeFlag(1);
                                MyChessFrame.isWhite(false);
                        } else {
                                chessBoard[x][y] = 2;
                                // message = "轮到白方";
                                MyChessFrame.changeFlag(2);
                                MyChessFrame.isWhite(true);
                        }
                } else {
                        MyChessFrame.changeFlag(3);
                }
        }

}



src.zip

92.12 KB, 下载次数: 136, 下载积分: 吾爱币 -1 CB

五子棋源代码(由于上传文件大小限制,需要额外导入jre)

免费评分

参与人数 2热心值 +2 收起 理由
Sheng——zac + 1 谢谢@Thanks!
fkcb + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

未老话已桑 发表于 2018-7-24 11:35
虽然看不懂,但是支持一下
冥界3大法王 发表于 2018-7-24 15:14
@流星的孤单
希望下个版本 加入VCF 、长连、捉禁手;做梅花做八卦等操作
crazy木 发表于 2018-7-24 22:00 来自手机
yzyou 发表于 2018-7-24 22:43
少了个Player类的样子啊
Genius_linghu 发表于 2018-7-25 09:15
支持一下退出舞台的swing
兜kong157 发表于 2018-8-3 16:08
正好学习下,自己也试试写个
oppo580 发表于 2018-8-3 17:55 来自手机
照着试一下
小蜜蜂丫丫 发表于 2018-8-3 18:05
支持学习哈哈哈哈哈哈哈
hunmeng 发表于 2018-10-2 23:57
支持,刚好可以学习下!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 13:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表