Git Product home page Git Product logo

fivechess's Introduction

五子棋

Author: Gahon

注:此项目来自网络,为了方便保存,所以上传至此(原作者:WangHao

配置环境: JDk1.8

棋子默认是黑子先行


主要功能:

  • 人人对弈
  • 人机对弈
  • 电脑对弈

使用说明:

  • 系统栏中,有重新开始悔棋退出等功能
  • 中间的面板展示棋局,默认黑子先行,最后下的棋子用红色矩阵框住显示
  • 底部按钮有人人对弈人机对弈电脑对弈三种模式,每次点击按钮选择对应模式,并清除棋盘重新开始

程序说明

程序中主要分为两个包,代码结构如下所示

  • BaseChess
  •   ChessBoard.java
  •   Point.java
  • Strategy
  •   ChessStrategy.java
  •   Main.java

BaseChess包

BaseChess包主要包含是棋盘ChessBoard和棋子Point的两个基本类

Strategy包

Strategy包含是棋子的策略Strategy和程序主函数Main两个类

下面对每个类别中函数进行简单的说明解释。

主函数 Main.java

  1. public Main(){}构造函数:初始化程序布局,添加对应的空间和布局

  2. private class MyItemListener implements ActionListener{}:实现控件事件监听

棋子类 Point.java

包含棋子的基本信息

  1. private int x:棋子的X坐标

  2. private int y:棋子的Y坐标

  3. private Color color:棋子的颜色

  4. public static final int DIAMETER = 30:绘制棋子的直径大小

棋盘类 ChessBoard.java

棋盘类,继承JPanel类,实现基本的棋盘绘画功能;实现Runnable接口和MouseListener监听接口。主要的功能有:

  1. 棋盘和棋子的绘制

  2. 界面监听机制实现

  3. 增加棋子

  4. 搜索棋子和得到棋子对应的坐标

  5. 判断棋局胜利

  6. 使用多线程实现人人对弈,人机对弈和电脑对弈三种模式

主要函数解释如下

  1. public ChessBoard(){}构造函数:初始化棋局、设置背景和添加监听机制

  2. public void paintChess(Graphics g){}:绘制棋子

  3. public void paintComponent(Graphics g){}:棋盘界面绘制

  4. public Point[] addChess(int x,int y){}:根据棋子坐标添加新的棋子,返回添加后的棋子数组,并判断棋局胜利

  5. public Dimension getPreferredSize(){}:绘制矩形框

  6. public void mousePressed(MouseEvent e){}:鼠标在棋盘界面监听机制实现。在人人对弈模式零下正常监听,在人机对弈模式一下,只在人在下棋步骤时监听棋盘。在电脑对弈模式二下,对棋盘监听到的事件不再理会。

  7. private boolean isWin(){}:判断棋局的输赢

  8. public void restartGame(){}重新开始比赛

  9. public void goback(){}:悔棋

  10. public void run(){}: 使用多线程,分别实现棋局的三种模式

棋子策略类 ChessStrategy.java

实现棋局的下子策略

  1. public Point WhiteNextStep(Point[] chessList){}:根据当前棋局,确定白子的下一个最佳位置

  2. public Point BlackNextStep(Point[] chesslistB){}:根据当前棋局,确定黑子的下一个最佳位置

  3. public Point BlackNextStep(Point[] chesslistB){}:根据当前棋局,找到使棋子最大的连子的位置

  4. public boolean findNeedPoint(int xIndex, int yIndex, Color c, Point[] chesslist){}:能够找到对应的坐标的棋子

  5. public ArrayList<Point> PointEvalution(ArrayList<Point> plist, Point[] chesslist, int longnum){} :对候选的棋子根据评价函数返回最佳评价的棋子

  6. public ArrayList<ArrayList<Point>> NextCandidatedPoints(Point[] chesslist){}:根据当前棋盘,分别得到黑白子下一步的候选棋子

  7. public boolean IsHasChess(int xIndex, int yIndex, Point[] chesslist){}:当前位置是否有棋子

  8. public int numsofthelongchess(Point p, Point[] chesslist, int longnum){}:给定的棋子位置最长连子数的个数

  9. public int lineSearch(Point p, Point[] chesslist){}:根据当前棋子位置,直线搜索连子的数目

  10. public int rowSearch(Point p, Point[] chesslist){}:根据当前棋子位置,垂直搜索连子的数目

  11. public int rightupSearch(Point p, Point[] chesslist){}:根据当前棋子位置,右上搜索连子的数目

  12. public int leftupSearch(Point p, Point[] chesslist){}:根据当前棋子位置,左上搜索连子的数目

  13. public void computerVscomputer(ChessBoard chessboard){}:电脑自动对弈函数实现

程序展示

###示例图片1 棋盘布局 ###示例图片2 下棋中 ###示例图片3 棋局结束

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.