五子棋游戏的设计说明书及源代码
- 格式:pdf
- 大小:491.43 KB
- 文档页数:22
*******************实践教学***********************大学计算机与通信学院2016年春季学期C程序设计课程设计题目:五子棋游戏专业班级:16级物联网1班姓名:******__学号:1616520147指导教师:******成绩:摘要五子棋是一种两人对弈或者人机对弈的纯策略型棋类游戏,应用C语言编写程序可以在计算机上实现人机对弈五子棋功能。
人机对弈五子棋程序由开始界面,棋盘,判断胜负和计分等子程序构成;程序中应用了数组、全局变量、按键处理和图形编程等元素和语句。
程序通过棋盘和棋子图像生成、玩家移子与电脑判断分数高低而落子和判断胜负等功能的实现,在计算机上用VC++6.0实现了人机五子棋对弈。
关键词:C语言;VC++6.0目录摘要 (I)第1章序言 (1)1.1设计背景 (1)1.2设计目的 (1)第2章需求分析 (2)第3章程序总体设计 (3)3.1程序总体框架图 (3)3.2程序流程图 (4)第4章程序详细设计 (5)4.1各类头文件及自定义函数 (5)4.2棋盘数组 (5)4.3开始界面函数 (5)4.4棋盘界面函数 (5)4.5胜负判断函数 (6)4.6得分判断函数 (6)4.7主函数 (6)第5章系统测试 (7)5.1开始界面测试 (7)5.2棋盘界面测试 (8)5.3玩家输赢界面测试 (11)设计总结 (13)参考文献 (14)致谢 (155)附录(源代码) (166)第1章序言1.1 设计背景五子棋相传起源于四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在“尧造围棋”之前,民间就已有五子棋游戏。
有关早期五子棋的文史资料与围棋有相似之处,因为古代五子棋的棋具与围棋是完全相同的。
在上古的神话传说中有“女娲造人,伏羲做棋”一说,《增山海经》中记载:“休舆之山有石焉,名曰帝台之棋,五色而文状鹑卵。
”李善注引三国魏邯郸淳《艺经》中曰:“棋局,纵横各十七道,合二百八十九道,白黑棋子,各一百五十枚”。
实验报告实验一五子棋游戏北方工业大学 2013级计算机技术米鹏一、实验原理及方法五子棋游戏开发借用Visual Studio 2012软件开发平台,选用C#语言进行编写。
整体程序主要分为三部分:界面操作部分、AI逻辑部分和棋子定点分析部分。
1、界面操作部分界面操作部分代码主要针对图像呈现、对应矩阵存储、下棋过程控制等可见的操作环节进编写。
同时负责整个程序的初始化工作。
图像呈现采用C#中Graphics进行绘制。
棋盘被划分为15行15列,每个划分出的小方格均为30*30的正方形,棋盘可操作的范围规定在(20,20)、(460,460)两点的确定的正方形区域内。
通过鼠标左击来确定下子地点。
程序会根据鼠标鼠标点击的位置进行计算,计算得到时对应矩阵的行列,之后再改变对应矩阵的内容后,在通过行列值乘以小方格边长计算得到在显示区域中的具体位置,再稍加变动后画到显示区域中。
以X点坐标为例,下面是计算X(Column)的流程图:在对应矩阵存储方面,后面AI逻辑和棋子分析所用到的矩阵都是来源这里。
同时AI 逻辑和棋子分析不能去修改对应矩阵内容。
图像呈现点的位置、重绘的根据都是来源这里。
在下棋过程控制方面采用信号亮的机制,当操作者下过后,根据信号AI会立即计算将要下点的位置同时改变信号亮变量。
当AI下过棋子后,由于信号亮的的限制就等待操作者去下棋,同时改变信号亮变量内容。
AI和操作者的所有下子、修改矩阵、显示棋子的过程都是统一的。
在每一盘游戏开始时程序会对一些重要的变量进行初始化这里包括矩阵、信号亮、第一步棋子颜色、呈现图像等内容进行初始化。
同时AI会在棋盘中央下第一子。
2、AI逻辑部分AI逻辑部分算是整个程序策略的灵魂。
其中的一些关键性判别的前后关系将影响AI 的下棋的结果。
同时加大和降低AI的难度也是这里。
下面是我设计的策略过程:从下棋者的考虑角度进行考虑,尽可能保证每一次下子都是有必要的、都是在情理当中的。
我所设计的策略并不是完整,漏洞在与没有考虑三棋子连续的情况。
手机五子棋游戏的设计与实现专业:姓名:班级:学号:指导教师:J2ME(Java 2 Micro Edition)是近年来随着各种不同设备,尤其是移动通信设备的飞速发展而诞生的一项开发技术。
它因其“write once,run anywhere”的Java特性而提高了开发的效率。
随着手机性能的不断提高,手机休闲娱乐应用将成为PC休闲娱乐应用之后又一重要业务增长点。
棋类游戏规则单一,比较适合在手机等便携终端推广。
由于具有跨平台、易于移植、占用空间小的优势,J2ME成为移动应用开发平台的主流,并提供了很多用以支持移动应用软件的开发的API。
现将该技术用于这次的手机游戏开发,可以实现游戏的快速开发,不但便于查看游戏运行过程中内存的占用量和程序的每一部分代码消耗了多少处理器时间,而且可以不断地优化代码,使代码具有高度的复用性、可扩展性、可维护性。
游戏的开发以J2ME为平台,利用Java技术,结合J2ME的MIDP技术,并对于程序设计思想,重要类、方法等展开讨论。
在对弈部分,分析设计走棋算法,选择合适的方式组织成代码,实现基本的人工智能。
过程中使用了J2ME中的CLDC/MIDP软件体系,主要运用了MID Profile的特定类的支持,来完成游戏的开发。
关键词:J2ME;CLDC;MIDPJ2ME is a kind of fast developing technology implemented on various devices especially mobile communication equipments. It improves the efficiency of the development process because of its "write once, run anywhere" nature. The development trend of the entertainment market based on the cell phone is very obvious because the handset performance enhances unceasingly. The entertainment market based on the cell phone will to be the new important business growth point follow the PC entertainment market. As the rules of a single chess game, it is more suitable for mobile phones and other portable terminal extension.J2ME has been the preferred platform for development because of its platform independent and compatibility, and provides a lot of APIs to support the development of mobile application software. The technology for mobile game development, can achieve the rapid development of the game. It is not only easy to observe the memory consumption and processor consumed time during the operation of the game, but also can optimize the code, so that the code has a high degree of reusability, scalability, maintainability.The game has designed by J2ME, the Java technology and the MIDP technology. I studied the procedure thought, the important class and the method. In the playing chess part, I have analyzed the algorithm, choosed the appropriate way to organize the code and realized the basic artificial intelligence. On the other hand,I learned software system of CLDC/MIDP and the specific class of the MID Profile to complete the game development.Key words: J2ME;CLDC;MIDP目录1 概述 (5)1.1 课题研究背景 (5)1.2 课题研究意义 (5)2 开发技术背景 (6)2.1 JAVA语言概述 (6)2.2 J2ME简介 (6)2.3 移动信息设备简表 (6)3 系统分析及总体设计 (7)3.1 可行性分析 (7)3.2 需求分析 (8)3.3 系统概要设计 (8)4 系统详细设计 (9)4.1 界面设计 (9)4.1.1 图形的低级绘制 (10)4.1.2 用户按键设计 (10)4.2 走棋算法 (11)4.3 胜负判断 (11)5 系统测试 (11)5.1 测试方案 (11)5.2 测试结果 (12)6总结 (13)基于J2ME的手机五子棋游戏的设计与实现1 概述1.1 课题研究背景五子棋是当前非常流行的一种棋。
*******************实践教学*******************软件学院2012年秋季学期C语言课程设计说明书题目:五子棋游戏的设计专业班级:姓名:学号:指导教师:成绩:目录目录 (I)摘要 (I)序言 (II)1、需求分析 (1)1.1总体分析 (1)1.2初始化 (1)1.3主循环控制模块 (1)1.4玩家下子 (1)1.5盘面分析填写棋型表 (2)1.6对方下子 (2)1.7胜负判断 (2)2、设计思路 (3)2.1五子棋的起源与发展 (3)2.2五子棋的基本术语及规则 (3)2.2.1五子棋的基本术语: (3)2.2.2五子棋的规则 (4)2.3功能描述 (5)3、详细设计 (6)3.1流程图 (6)4、运行调试与分析讨论 (7)4、运行调试与分析讨论 (8)4.1运行结果 (8)4.2分析讨论 (12)5、设计体会与小结 (13)5.1课设中的体会及小结 (13)参考文献 (14)致谢 (15)附录一源代码 (16)摘要游戏设计已经成为现在社会的一种热门行业。
五子棋是一种受大众广泛喜爱的游戏,其规则简单,变化多端,只需两人即可进行,非常富有趣味性和消遣性。
这里设计和实现了一个双人对下的黑白五子棋单机游戏。
主要应用了Visual C++编程来完成这个游戏的设计的。
本文主要介绍了本游戏开发环境Microsoft Visual C++ 6.0,使用当前优秀的开发工具VC++编程。
包含了五子棋程序的棋盘初始化、游戏规则、胜负判断方法,及帮助等。
关键词:五子棋;初始化;VC++编程;判断;单机游戏。
序言五子棋是起源于中国古代的传统黑白棋种之一,是一种训练人逻辑思维严密性的游戏。
该设计以五子棋游戏业务为背景,设计、开发一套“五子棋游戏”软件。
通过该题目的设计过程,可以提高基本编程能力和游戏开发的技巧,熟悉C语言图形模式下的编程。
本程序中涉及很多图形函数,包括:坐标定位;颜色设置(背景色和文本颜色设置),线条颜色、填充颜色;直线风格、文本风格、填充风格;画线、画圆、画矩形函数;图形系统的初始化和关闭;图形屏幕和文字屏幕的消除;从键盘获取键值等。
C++课程设计--五子棋游戏课程设计说明书五子棋游戏The Game of Gobang学院名称:机械工程学院专业班级:测控10xx 学生姓名:江x 指导教师姓名:张xx 指导教师职称:副教授2012年6月目录第一章需求分析 (7)1.1系统概述 (7)1.1.1概述: (7)1.2系统运行环境 (7)1.2.1运行环境 (7)1.3功能需求描述 (7)1.3.1功能需求 (7)第二章总体设计 (8)2.1开发与设计的总体思想 (8)2.1.1总体设计思路 (8)2.1.2屏幕的组成 (9)2.1.3形状的组成 (9)2.1.4形状的统一 (10)第三章概要设计 (10)3.1系统流程图 (10)3.2 软件功能模块 (12)3.3 系统功能模块 (13)第四章详细设计 (13)4.1.界面的设计 (13)4.1.1总体界面的设计 (13)4.1.2界面棋子的设计 (13)4.2.显示界面的准备 (15)4.2.1在窗体类中添加存放黑白棋两维数组和背景棋盘、黑棋、白棋位图对象 (15)4.2.2 在窗体构造函数中初始化wb,装入棋盘、黑棋、白棋位图 (15)4.3. 显示棋盘和棋子代码 (15)4.4. 轮流下子代码 (16)4.5. 判断输赢 (19)4.5.1判断是否白棋胜代码 (19)4.5.2判断是否黑棋胜代码 (20)4.5.3判断是否平局代码 (21)4.6. 悔棋 (21)4.7. 重新开始 (22)4.8. 背景音乐 (23)4.9. 保存读取游戏文件 (23)4.9.1保存文件: (24)4.9.2读取文件: (25)第五章测试分析 (26)结论和心得体会 (27)致谢 (28)参考文献: (29)课程设计任务书一、课程设计目的课程设计是工科各专业的主要实践性教学环节之一,是围绕一门主要基础课或专业课,运用所学课程的知识,结合实际应用设计而进行的一次综合分析设计能力的训练。
课程设计旨在培养学生在本课程范围内,初步掌握解决实际应用问题时所应具有的查阅资料、综合运用所学知识的能力,为课程设计及今后从事专业工作打下基础。
c语言设计五子棋代码五子棋,亦称五目、五子、顺手拍子、连珠、下五子、悔棋、先手必胜等,是一种两人对弈的纯策略型棋类游戏。
通常用黑白两色的棋子在白色棋盘上进行游戏,以先把五个棋子连成一线者获胜。
实现五子棋游戏需要完成以下步骤:1. 棋盘的绘制2. 棋子的下落判断,包括判断是否胜利3. 玩家之间的交替下棋首先是棋盘的绘制。
采用二维数组来表示棋盘,用“+”、“-”、“|”等符号来绘制边框和交叉点。
同时还要用“ ”表示空白区域,用“●”和“○”代表黑白棋子。
然后是棋子的下落判断。
需要记录下棋子的位置,并判断落子是否在棋盘范围内,以及是否与已落下的棋子位置冲突。
在判断胜负时,需要遍历棋盘上所有的落子情况,检查是否存在连续的五个同色棋子。
最后是玩家之间的交替下棋。
可以使用一个计数器来记录当前下棋者,每次下完一子后,计数器加1,判断下一个下棋者,直到游戏结束为止。
下面是一份实现五子棋游戏的 C 代码:#include <stdio.h>#include <stdlib.h>#define ROW 15 // 棋盘行数#define COL 15 // 棋盘列数#define BLACK '●' // 黑色棋子#define WHITE '○' // 白色棋子#define SPACE ' ' // 空白区域char board[ROW][COL]; // 棋盘二维数组// 初始化棋盘void init_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {board[i][j] = SPACE;}}}// 打印棋盘void print_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {if (j == 0) printf("%2d ", i + 1); printf("%c ", board[i][j]);}printf("\n");}for (int i = 0; i < COL; i++) {if (i == 0) printf(" ");printf("%c ", 'A' + i);}printf("\n");}// 判断是否胜利int is_win(int row, int col, char ch) {int count = 1;// 判断横向for (int i = col - 1; i >= 0; i--) { if (board[row][i] == ch) {count++;} else {break;}}for (int i = col + 1; i < COL; i++) { if (board[row][i] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断纵向count = 1;for (int i = row - 1; i >= 0; i--) { if (board[i][col] == ch) {count++;} else {break;}}for (int i = row + 1; i < ROW; i++) {if (board[i][col] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左上到右下斜向count = 1;for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row + 1, j = col + 1; i < ROW && j < COL; i++, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左下到右上斜向count = 1;for (int i = row + 1, j = col - 1; i < ROW && j >= 0; i++, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row - 1, j = col + 1; i >= 0 && j < COL; i--, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}return 0;}。
设计题目:五子棋。
一、程序目的和功能实现图形用户界面的五子棋双人对战。
二、各个类的功能说明1.ChessPad面板类,用于绘制棋盘,实现用鼠标进行走棋的动作。
棋盘如下:关键代码:public void paint(Graphics g){for (int i=40;i<=400;i=i+20){g.drawLine(40,i,400,i);}for(int j=40;j<=400;j=j+20){g.drawLine(j,40,j,400);}g.fillOval(97,97,6,6);g.fillOval(337,97,6,6);g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);g.fillOval(217,217,6,6);}定义鼠标在棋盘上的走棋,关键代码:public void mousePressed(MouseEvent e)//走棋2.ChessPoint_black画布类,绘制黑棋,实现黑棋的悔棋,关键代码:public void paint(Graphics g){g.setColor(Color.black);g.fillOval(0,0,18,18);}public void mousePressed(MouseEvent e){if(e.getModifiers()==InputEvent.BUTTON3_MASK){chesspad.remove(this);chesspad.棋子颜色=1;chesspad.i--;chesspad.text_3.setText("这是第"+chesspad.i+"步");chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");}}3.ChessPoint_white画布类,绘制白棋,实现白棋的悔棋,关键代码与ChessPoint_black类似。
JAVA课程设计设计题目:五子棋游戏一.简要的介绍五子棋1.五子棋的起源五子棋,又被称为“连五子、五子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”。
五子棋相传起源于四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在“尧造围棋”之前,民间就已有五子棋游戏。
有关早期五子棋的文史资料与围棋有相似之处,因为古代五子棋的棋具与围棋是完全相同的。
2.现在五子棋标准棋盘(如图所示)3.五子棋的棋子五子棋采用两种颜色棋子,黑色棋子和白色棋子,和围棋相同,4.五子棋规则五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,由于黑棋先行,优势太大,所以对黑棋设了禁手,又规定了“三手交换”,就是黑棋下第 2 手棋,盘面第 3 着棋之后,白方在应白 2 之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。
和“五手两打法”,就是黑棋在下盘面上关键的第 5 手时,必须下两步棋,让白方在这两步棋中任选一步,然后再续下。
不过一般爱好者不需要遵循这么多规则。
二.程序流程三.代码设计与分析main方法创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。
public class FiveChessAppletDemo {public static void main(String args[]){ChessFrame cf = new ChessFrame();cf.show();}}用类ChessFrame创建五子棋游戏主窗体和菜单import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;class ChessFrame extends JFrame implements ActionListener { private String[] strsize={"标准棋盘","改进棋盘","扩大棋盘"}; private String[] strmode={"人机对战","人人对战"};public static boolean iscomputer=true,checkcomputer=true; private int width,height;private ChessModel cm;private MainPanel mp;构造五子棋游戏的主窗体public ChessFrame() {this.setTitle("五子棋游戏");cm=new ChessModel(1);mp=new MainPanel(cm);Container con=this.getContentPane();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(new ChessWindowEvent());MapSize(14,14);JMenuBar mbar = new JMenuBar();this.setJMenuBar(mbar);JMenu gameMenu = new JMenu("游戏");mbar.add(makeMenu(gameMenu, new Object[] {"开局", null,"棋盘",null,"模式", null, "退出"}, this));JMenu lookMenu =new JMenu("外观");mbar.add(makeMenu(lookMenu,new Object[] {"类型一","类型二","类型三"},this));JMenu helpMenu = new JMenu("版本");mbar.add(makeMenu(helpMenu, new Object[] {"关于"}, this));}构造五子棋游戏的主菜单public JMenu makeMenu(Object parent, Object items[], Object target){ JMenu m = null;if(parent instanceof JMenu)m = (JMenu)parent;else if(parent instanceof String)m = new JMenu((String)parent);elsereturn null;for(int i = 0; i < items.length; i++)if(items[i] == null)m.addSeparator();else if(items[i] == "棋盘"){JMenu jm = new JMenu("棋盘");ButtonGroup group=new ButtonGroup();JRadioButtonMenuItem rmenu;for (int j=0;j<strsize.length;j++){rmenu=makeRadioButtonMenuItem(strsize[j],target);if (j==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}else if(items[i] == "模式"){JMenu jm = new JMenu("模式");ButtonGroup group=new ButtonGroup();JRadioButtonMenuItem rmenu;for (int h=0;h<strmode.length;h++){rmenu=makeRadioButtonMenuItem(strmode[h],target);if(h==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elsem.add(makeMenuItem(items[i], target));return m;}构造五子棋游戏的菜单项public JMenuItem makeMenuItem(Object item, Object target){ JMenuItem r = null;if(item instanceof String)r = new JMenuItem((String)item);else if(item instanceof JMenuItem)r = (JMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}构造五子棋游戏的单选按钮式菜单项public JRadioButtonMenuItem makeRadioButtonMenuItem(Object item, Object target){JRadioButtonMenuItem r = null;if(item instanceof String)r = new JRadioButtonMenuItem((String)item);else if(item instanceof JRadioButtonMenuItem)r = (JRadioButtonMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}public void MapSize(int w,int h){setSize(w * 24, h * 27);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}public boolean getiscomputer(){return this.iscomputer;}public void restart(){int modeChess = cm.getModeChess();if(modeChess <= 3 && modeChess >= 0){cm = new ChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}}public void actionPerformed(ActionEvent e){String arg=e.getActionCommand();try{if (arg.equals("类型三"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");else if(arg.equals("类型二"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel" );SwingUtilities.updateComponentTreeUI(this);}catch(Exception ee){}if(arg.equals("标准棋盘")){this.width=14;this.height=14;cm=new ChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("改进棋盘")){this.width=18;this.height=18;cm=new ChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("扩大棋盘")){this.width=22;this.height=22;cm=new ChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对战")){this.checkcomputer=true;this.iscomputer=true;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对战")){this.checkcomputer=false;this.iscomputer=false;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("开局")){restart();}if(arg.equals("关于"))JOptionPane.showMessageDialog(null, "第一版", "版本",JOptionPane.PLAIN_MESSAGE );if(arg.equals("退出"))System.exit(0);}}用类ChessModel实现了整个五子棋程序算法的核心import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;class ChessModel {规定棋盘的宽度、高度、棋盘的模式private int width,height,modeChess;规定棋盘方格的横向、纵向坐标private int x=0,y=0;棋盘方格的横向、纵向坐标所对应的棋子颜色,数组arrMapShow只有3个值:1,2,3,-1,其中1代表该棋盘方格上下的棋子为黑子,2代表该棋盘方格上下的棋子为白子,3代表为该棋盘方格上没有棋子,-1代表该棋盘方格不能够下棋子private int[][] arrMapShow;交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOdd,isExist;public ChessModel() {}该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModel(int modeChess){this.isOdd=true;if(modeChess == 1){PanelInit(14, 14, modeChess);}if(modeChess == 2){PanelInit(18, 18, modeChess);}if(modeChess == 3){PanelInit(22, 22, modeChess);}}按照棋盘模式构建棋盘大小private void PanelInit(int width, int height, int modeChess){ this.width = width;this.height = height;this.modeChess = modeChess;arrMapShow = new int[width+1][height+1];for(int i = 0; i <= width; i++){for(int j = 0; j <= height; j++){arrMapShow[i][j] = -1;}}}获取是否交换棋手的标识符public boolean getisOdd(){return this.isOdd;}设置交换棋手的标识符public void setisOdd(boolean isodd){ if(isodd)this.isOdd=true;elsethis.isOdd=false;}获取某棋盘方格是否有棋子的标识值public boolean getisExist(){return this.isExist;}获取棋盘宽度public int getWidth(){return this.width;}获取棋盘高度public int getHeight(){return this.height;}获取棋盘模式public int getModeChess(){return this.modeChess;}获取棋盘方格上棋子的信息public int[][] getarrMapShow(){return arrMapShow;}判断下子的横向、纵向坐标是否越界private boolean badxy(int x, int y){if(x >= width+20 || x < 0)return true;return y >= height+20 || y < 0;}计算棋盘上某一方格上八个方向棋子的最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExist(int i,int j){if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)return true;return false;}判断该坐标位置是否可下棋子public void readyplay(int x,int y){if(badxy(x,y))return;if (chessExist(x,y))return;this.arrMapShow[x][y]=3;}在该坐标位置下棋子public void play(int x,int y){if(badxy(x,y))return;if(chessExist(x,y)){this.isExist=true;return;}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;}else{setisOdd(true);this.arrMapShow[x][y]=2;}}计算机走棋说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后得出棋子数最大值的坐标,下子public void computerDo(int width,int height){int max_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("计算机走棋 ...");for(int i = 0; i <= width; i++){for(int j = 0; j <= height; j++){算法判断是否下子if(!chessExist(i,j)){判断白子的最大值max_white=checkMax(i,j,2);判断黑子的最大值max_black=checkMax(i,j,1);max_temp=Math.max(max_white,max_black);if(max_temp>max){max=max_temp;this.x=i;this.y=j;}}}}setX(this.x);setY(this.y);this.arrMapShow[this.x][this.y]=2;}记录电脑下子后的横向坐标public void setX(int x){this.x=x;}记录电脑下子后的纵向坐标public void setY(int y){this.y=y;}获取电脑下子的横向坐标public int getX(){return this.x;}获取电脑下子的纵向坐标public int getY(){return this.y;}计算棋盘上某一方格上八个方向棋子的最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下public int checkMax(int x, int y,int black_or_white){ int num=0,max_num,max_temp=0;int x_temp=x,y_temp=y;int x_temp1=x_temp,y_temp1=y_temp;判断右边for(int i=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}判断左边x_temp1=x_temp;for(int i=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num<5)max_temp=num;判断上面x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}判断下面y_temp1=y_temp;for(int i=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;判断左上方x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1-=1;y_temp1-=1;if(y_temp1<0 || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}判断右下方x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<5;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;判断右上方x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0 || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}判断左下方x_temp1=x_temp;for(int i=1;i<5;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;return max_num;}判断胜负public boolean judgeSuccess(int x,int y,boolean isodd){ int num=1;int arrvalue;int x_temp=x,y_temp=y;if(isodd)arrvalue=2;elsearrvalue=1;int x_temp1=x_temp,y_temp1=y_temp;判断右边胜负for(int i=1;i<6;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}判断左边胜负x_temp1=x_temp;for(int i=1;i<6;i++){x_temp1-=1;break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;判断上方胜负x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}判断下方胜负y_temp1=y_temp;for(int i=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;判断左上胜负x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){y_temp1-=1;if(y_temp1<0 || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}判断右下胜负x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<6;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}if(num==5)return true;判断右上胜负x_temp1=x_temp;y_temp1=y_temp;num=1;for(int i=1;i<6;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0 || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++;elsebreak;}判断左下胜负x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<6;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)return true;return false;}赢棋后的提示public void showSuccess(JPanel jp){JOptionPane.showMessageDialog(jp,"你赢了","结果",RMATION_MESSAGE);}输棋后的提示public void showDefeat(JPanel jp){JOptionPane.showMessageDialog(jp,"你输了","结果",RMATION_MESSAGE);}}用类MainPanel主要完成如下功能:1、构建一个面板,在该面板上画上棋盘;2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;class MainPanel extends JPanelimplements MouseListener,MouseMotionListener{设定棋盘的宽度和高度private int width,height;private ChessModel cm;根据棋盘模式设定面板的大小MainPanel(ChessModel mm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}根据棋盘模式设定棋盘的宽度和高度public void setModel(ChessModel mm){cm = mm;width = cm.getWidth();height = cm.getHeight();}根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),然后调用draw方法在棋盘上画出相应的棋子public void paintComponent(Graphics g){super.paintComponent(g);for(int j = 0; j <= height; j++){for(int i = 0; i <= width; i++){int v = cm.getarrMapShow()[i][j];draw(g, i, j, v);}}}根据提供的棋子信息(颜色、坐标)画棋子public void draw(Graphics g, int i, int j, int v){ int x = 20 * i+20;int y = 20 * j+20;画棋盘if(i!=width && j!=height){g.setColor(Color.darkGray);g.drawRect(x,y,20,20);}画黑色棋子if(v == 1 ){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.black);g.fillOval(x-8,y-8,16,16);}画白色棋子if(v == 2 ){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.white);g.fillOval(x-8,y-8,16,16);}if(v ==3){g.setColor(Color.cyan);g.drawOval(x-8,y-8,16,16);}}响应鼠标的点击事件,根据鼠标的点击来下棋,根据下棋判断胜负等public void mousePressed(MouseEvent evt){int x = (evt.getX()-10) / 20;int y = (evt.getY()-10) / 20;System.out.println(x+" "+y);if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){cm.play(x,y);System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]); repaint();if(cm.judgeSuccess(x,y,cm.getisOdd())){cm.showSuccess(this);evt.consume();ChessFrame.iscomputer=false;}判断是否为人机对弈if(ChessFrame.iscomputer&&!cm.getisExist()){puterDo(cm.getWidth(),cm.getHeight());repaint();if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){ cm.showDefeat(this);evt.consume();}}}}public void mouseClicked(MouseEvent evt){}public void mouseReleased(MouseEvent evt){}public void mouseEntered(MouseEvent mouseevt){}public void mouseExited(MouseEvent mouseevent){}public void mouseDragged(MouseEvent evt){}响应鼠标的拖动事件public void mouseMoved(MouseEvent moveevt){int x = (moveevt.getX()-10) / 20;int y = (moveevt.getY()-10) / 20;cm.readyplay(x,y);repaint();}}import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;响应退出窗口class ChessWindowEvent extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(0);}ChessWindowEvent(){}}四.程序调试与运行运行:标准棋盘改进棋盘:扩大棋盘:外观类型二:外观类型三:人机对战:结果:五.结论通过对五子棋游戏的编写,使自己对java语言有了更深的了解。