JAVA课程设计 五子棋

攀枝花学院课程业设计五子棋学生姓名:学号:所在院(系):数学院计算机学院专业:信息与计算科学指导教师:讲师二〇一四年六月攀枝花学院教务处制攀枝花学院本科学生课程设计任务书注:任务书由指导教师填写。

摘要五子棋作为一类棋类竞技运动,现在很流行,很多人把它作为一类开发智力的一种游戏,锻炼人的思维。

这次课题主要是完成人机对战,在对战中电脑根据人的思维做出相应的反应,电脑对战中通过深度的搜索,使得自身有更大的胜算,估值计算也是一种方法,可以提高电脑的智能度。

分析模块中影响智能的因素也不少,通过节点比较与节点连接后的结果做出估计提高智能,了解递归算法、电脑学习等对此有很大帮助。

算法是程序的灵魂,一旦算法正确那么程序将很好。

不同的人工智能将会有不同的帮助,多去了解将更能提高智能程度。

五子棋是我国的一门文化,这将使得它更受世界人们的欢迎。

有助我国文化发展。

关键词五子棋,智能,算法,模块,人机对战目录1 需求分析 (1)1.1需求来源 (1)1.2设计目的 (1)2 功能需求分析 (1)2.1功能需求 (1)3 设计与实现 (2)3.1设计思想 (2)3.2系统模块结构 (2)3.3流程图 (2)4 概要设计 (4)4.1抽象数据类型定义 (4)4.2程序包含模块 (4)4.3模块间关系 (4)4.4系统功能实现 (4)5 模块设计 (5)5.1主界面模块 (5)5.2选择模块 (5)5.3判断模块、 (5)5.4显示模块 (5)参考文献 (9)1 需求分析1.1需求来源计算机在我们的生活中有越来越重要的角色,我们也越来越离不开计算机,计算机带给我们许多便利,学习好计算机知识已经是必不可少的一项技能了。

特别是电子商务、电子邮件等,人工智能现在的热点方向,人们感叹计算机的高效是也感叹自己的聪明,人工智能现在是很好的以方面。

1.2设计目的该软件为用户提供一个在Windows系统上运行的五子棋游戏小系统。

应达到的目的是:建立人机对战的模块,可以机器思考如何应对。

已达到提高智力的效果设计出五子棋的游戏界面,创建对象可以在地图中站位,在每一步后计算机会自己运算自己的下一步,任何一方不可以越界,当一方达到五子是判断谁是胜利者,在过程中尽量使得游戏智能程度更高。

2 功能需求分析2.1功能需求现在研究五子棋的算法已经很多了,比较经典的有递归、二叉树等,这也是很基础的,不同算法要求也不同,要求的能力也不同,每一种算法都是使得程序清晰明白,当五子相连就算胜利。

故我们要理解中间过程。

每个代码实现功能也是不同的,有的是判断,有的是理解,有的是更好知道程序,程序在执行时我们可以知道哪里出错。

哪里会有问题,修改便利。

、在错误中学习,加强自己的基础知识与算法的能力。

3 设计与实现3.1设计思想对五子棋进行模块合理需求分析后,在思考整体布局,首先是如何进行设计与开发的思维,在此基础上用图表画出大体的框架图,在进行合理的模块划分,并用框架图画出,在分模块实现。

总体思维:首先是整体的框架地图,在然后是游戏上的功能键设置,功能选项设计,在游戏开始后棋手与电脑的对战,在胜利的时候做出判断(即五子相连时可以有结束提示);关键是电脑部分要尽量智能,保证游戏价值。

3.2系统模块结构系统模块结构:大概分为界面模块、功能模块、判断模块、主要也就是判断模块,在棋手或者电脑达到五子时给出正确的结果,及在开始前就要有功能选择,保证在功能选择部分设计。

大体模块如下图。

3.3流程图4 概要设计4.1抽象数据类型定义(1)对界面设计,可以选择界面的大小。

public ChessFrame() {界面的窗口设计}(2)主要的框架设计和参数public JMenu makeMenu(Object parent, Object items[], Object target){ }(3)判断分析public boolean judgeSuccess(int x,int y,boolean isodd){是否五子相连}4.2程序包含模块界面模块public ChessFrame(){}界面窗口的大小选择,显示界面。

地图范围模块public void MapSize(int w,int h){限制游戏者的游戏范围}结束模块public boolean judgeSuccess(int x,int y,boolean isodd){是否五子相连}4.3模块间关系4.4系统功能实现要实现五子棋游戏关键是地图设计与功能设计,选择地图与谁先开始是一个关键,先要进行选择,进行后,要判断谁先达到五子,保证谁胜利。

5 模块设计5.1主界面模块该模块主要是进行显示地图大小,限制地图的长与宽,保证游戏者的棋子不会超过范围,可以正常的进行游戏。

5.2选择模块主要进行选择功能,选择地图的大小,游戏者谁先进行游戏。

5.3判断模块、主要是判断谁先达到五子,判定达到五子的为胜利,在判断的过程中每一步都进行判断,一直到谁最先到五子相连,后一步都不可以。

5.4显示模块就是界面显示的一部分,用于显示结果。

测试与运行开始前的界面功能选择开始后的界面最后的结果结论通过这次设计,使我在java的运用有新的理解、java的选择应用、算法的设计及其实现等方面加深了对课程基本内容的理解。

同时,在程序设计方法以及上机操作等基本技能和科学作风方面受到比较系统和严格的训练。

在此之前,我脑袋里面没有java思想,java在我心中就是对象的创建。

当时就感觉好难,什么也不能做,但是真正接触后并没有想象的那么难,有什么不懂的上网去搜索,这是一个信息横流的世界,有google,baidu就没有不能解决的知识难题。

在网上搜索点有关的资料后,然后再利用自己在课堂上所学的知识,与和我一组的同学讨论并且将所有内容连接起来,所有问题就迎刃而解。

课程设计是以学生自学为主、指导教师指导为辅,认真、独立地完成课程设计的任务,有问题就要及时主动与指导教师沟通。

在老师的悉心指导下和同学们的帮助下,使我顺利地完成了此次课程设计。

所以,要特别感谢指导老师和各位同学,我相信这次课程设计将会是我以后不管是工作还是学习不可多得的经验。

参考文献[1]朱福喜.《Java语言程序设计》(第二版).科学出版社[2]陈国君等.《Java程序设计基础》(第二版).清华大学出版社[3] Deitel.《Java大学基础教程》(第六版).电子工业出版社[4] MaryCampione.《Java语言导学》(第四版).机械工业出版社[5] Y.Daniel Liang.《Java语言程序设计基础篇》(第六版). 机械工业出版社[6] Kathy Sierra.《Head First Java》(第二版).东南大学出版社package day17.gobang;import java.util.Arrays;public class GoBangGame {public static final char BLANK='*'; public static final char BLACK='@';public static final char WHITE='O';public static final int MAX = 16;private static final int COUNT = 5; //棋盘private char[][] board; public GoBangGame() {}//开始游戏public void start() {board = new char[MAX][MAX];//把二维数组都填充‘*’for(char[] ary: board){Arrays.fill(ary, BLANK);} }public char[][] getChessBoard(){return board; }public void addBlack(int x, int y) throws ChessExistException{ //@//char blank = '*';//System.out.println( x +"," + y + ":" + board[y][x] + ",//+ BLANK);if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子board[y][x] = BLACK; return; }throw new ChessExistException("已经有棋子了!");}public void addWhite(int x, int y) throws ChessExistException{if(board[y][x] == BLANK){// x, y 位置上必须是空的才可以添棋子board[y][x] = WHITE; return;} throw new ChessExistException("已经有棋子了!");} //chess 棋子:'@'/'O'public boolean winOnY(char chess, int x, int y){//先找到y方向第一个不是blank的棋子int top = y; while(true){if(y==0 || board[y-1][x]!=chess){//如果y已经是棋盘的边缘,或者的前一个不是chess//就不再继续查找了break;} y--;top = y; }//向回统计所有chess的个数,如果是COUNT个就赢了int count = 0; y = top; while(true){if(y==MAX || board[y][x]!=chess){//如果找到头或者下一个子不是chess 就不再继续统计了break;} count++;y++; } return count==COUNT; }//chess 棋子:'@'/'O'public boolean winOnX(char chess, int x, int y){//先找到x方向第一个不是blank的棋子int top = x; while(true){ if(x==0 || board[y][x-1]!=chess){//如果x已经是棋盘的边缘,或者的前一个不是chess//就不再继续查找了break; }x--;top = x; }//向回统计所有chess的个数,如果是COUNT个就赢了int count = 0; x = top;while(true){if(x==MAX || board[y][x]!=chess){//如果找到头或者下一个子不是chess 就不再继续统计了break;} count++;x++; } return count==COUNT; }//chess 棋子:'@'/'O'public boolean winOnXY(char chess, int x, int y){//先找MAX向第一个不是blank的棋子int top = y; int left = x; while(true){ if(x==0 || y==0 || board[y-1][x-1]!=chess){//如果x已经是棋盘的边缘,或者的前一个不是chess//就不再继续查找了break;} x--; y--;top = y; left=x; }//向回统计所有chess的个数,如果是COUNT个就赢了int count = 0; x = left; y = top;while(true){if(x==MAX || y==MAX || board[y][x]!=chess){//如果找到头或者下一个子不是chess 就不再继续统计了break;} count++;x++;搜索y++; } return count==COUNT; }//chess 棋子:'@'/'O'public boolean winOnYX(char chess, int x, int y){//先找到x方向第一个不是blank的棋子int top = y; int left = x;while(true){ if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){//如果x已经是棋盘的边缘,或者的前一个不是chess//就不再继续查找了break;} x++; y--; top = y;left=x; }//向回统计所有chess的个数,如果是COUNT个就赢了int count = 0; x = left; y = top;while(true){if(x==0 || y==MAX || board[y][x]!=chess){//如果找到头或者下一个子不是chess 就不再继续统计了break; } count++;x--; y++; }return count==COUNT; }public boolean whiteIsWin(int x, int y) {//在任何一个方向上赢了,都算赢return winOnY(WHITE, x, y) ||winOnX(WHITE, x, y) ||winOnXY(WHITE, x, y) ||winOnYX(WHITE, x, y); }public boolean blackIsWin(int x, int y) {return winOnY(BLACK, x, y) ||winOnX(BLACK, x, y) ||winOnXY(BLACK, x, y) ||winOnYX(BLACK, x, y); }}。

合集下载

java五子棋课程设计

java五子棋课程设计

java五子棋课程设计一、课程目标知识目标:1. 学生能理解五子棋游戏的基本规则和胜负判断方法。

2. 学生掌握Java编程语言的基本语法,如变量声明、循环结构、条件判断等。

3. 学生能运用面向对象编程思想,设计五子棋游戏的相关类和对象。

技能目标:1. 学生能运用所学知识,独立编写五子棋游戏的代码,实现人机对战和胜负判断功能。

2. 学生通过编程实践,提高解决问题的能力和逻辑思维能力。

3. 学生能运用调试工具,优化代码,提高程序的执行效率和稳定性。

情感态度价值观目标:1. 学生在编程过程中,培养耐心、细心和团队合作精神。

2. 学生通过五子棋游戏,体验编程的乐趣,激发对计算机科学的兴趣和求知欲。

3. 学生在学习过程中,树立正确的竞争观念,学会尊重对手,公平竞争。

课程性质:本课程为实践性课程,注重培养学生的动手能力和编程思维。

学生特点:五年级学生,具备一定的计算机操作能力和逻辑思维能力,对编程有浓厚兴趣。

教学要求:结合学生特点,注重理论与实践相结合,以学生为主体,引导学生主动探究、合作学习,培养其编程素养。

将课程目标分解为具体的学习成果,以便在教学设计和评估中有的放矢。

二、教学内容1. 五子棋游戏规则及胜负判断方法介绍- 游戏规则讲解- 胜负判断方法分析2. Java基本语法复习- 数据类型与变量- 循环结构(for、while)- 条件判断(if-else、switch)3. 面向对象编程思想- 类与对象- 封装、继承、多态4. 五子棋游戏设计与实现- 游戏界面设计- 游戏逻辑实现(人机对战、胜负判断)- 代码编写与调试5. 教学内容的安排与进度- 第一节课:五子棋游戏规则介绍,Java基本语法复习- 第二节课:面向对象编程思想学习,五子棋游戏设计思路分析- 第三节课:五子棋游戏代码编写与调试,实现人机对战和胜负判断功能6. 教材章节及内容关联- 第五章:面向对象编程- 第六章:数组与字符串- 第七章:异常处理与调试技巧教学内容依据课程目标进行选择和组织,保证科学性和系统性。

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现五子棋是一种双人对弈的棋类游戏,也是一种非常经典的游戏类型。

设计一个基于JAVA的五子棋游戏系统,需要考虑到以下几个方面:游戏规则、游戏界面和游戏功能的实现。

一、游戏规则的设计:五子棋的规则非常简单,双方轮流下棋,先连成五子的一方为胜。

游戏区域是一个15x15的棋盘,棋盘上有黑白两种棋子。

玩家可以通过点击棋盘的不同位置来下棋。

二、游戏界面的设计与实现:1. 创建一个游戏主界面,并在界面上绘制棋盘。

可以使用JAVA Swing来创建界面,并使用Graphics类来实现绘制棋盘的功能。

2.在棋盘上绘制出15x15的格子,并设置鼠标点击事件,以便玩家可以通过点击棋盘的不同位置来下棋。

3. 在玩家下棋后,使用Graphics类来在棋盘上绘制出对应的棋子。

可以使用不同的颜色来区分黑白两种棋子。

三、游戏功能的实现:1.实现轮流下棋的功能。

通过一个变量来记录当前轮到哪个玩家下棋,玩家可以通过鼠标点击事件来确定落子的位置。

2.实现判断胜负的功能。

在每次下棋后,判断是否已经有五子连线的情况出现。

可以使用一个二维数组来保存棋盘上每个位置的棋子情况,并根据棋子位置来判断是否满足胜利条件。

3.实现悔棋功能。

当玩家点击悔棋按钮时,可以将最后一步下的棋子清除,并将轮到下棋的玩家切换至上一步下棋的玩家。

4.实现重新开始的功能。

当游戏结束后,可以通过点击重新开始按钮来重新开始游戏。

以上是一个基于JAVA的五子棋游戏系统的设计与实现的简要思路。

在实际的开发过程中,还需要考虑到异常处理、界面美化、多人对战等更多的细节。

希望以上内容能够帮助您完成五子棋游戏系统的设计与实现。

基于JAVA的五子棋游戏设计

基于JAVA的五子棋游戏设计

基于JAVA的五子棋游戏设计五子棋是一种非常有趣的棋类游戏,主要目标是在棋盘上先形成五个连续的子的一方取胜。

为了设计一款基于JAVA的五子棋游戏,我们需要考虑以下几个方面:1.游戏界面设计:游戏界面需要包括一个棋盘和一些控制按钮。

可以使用Java GUI库(如Swing或JavaFX)来创建游戏界面,使用布局管理器来放置棋盘和按钮。

2.游戏规则设计:游戏规则非常简单。

两位玩家交替下棋,玩家可以在棋盘上的任意空闲位置下子。

首先形成五个连续的子的一方获胜。

可以使用二维数组来表示棋盘的状态,并在每次下子后更新数组的值。

3.游戏逻辑设计:游戏逻辑包括判断玩家下子的合法性、判断游戏是否结束以及判断获胜方等。

可以使用条件语句和循环结构来实现游戏逻辑。

下面是一种可能的五子棋游戏设计方案:```javaimport javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class GomokuGame extends JFrameprivate final int BOARD_SIZE = 15;private JButton[][] buttons;private int[][] board;private int currentPlayer;public GomokuGamsetTitle("Gomoku Game");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(600, 600);setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));buttons = new JButton[BOARD_SIZE][BOARD_SIZE];board = new int[BOARD_SIZE][BOARD_SIZE];currentPlayer = 1;for (int i = 0; i < BOARD_SIZE; i++)for (int j = 0; j < BOARD_SIZE; j++)buttons[i][j] = new JButton(;buttons[i][j].setBackground(Color.WHITE);buttons[i][j].addActionListener(new ButtonListener(i, j));add(buttons[i][j]);}}setVisible(true);}private class ButtonListener implements ActionListener private int row;private int col;public ButtonListener(int row, int col)this.row = row;this.col = col;}public void actionPerformed(ActionEvent e)if (buttons[row][col].getBackground( == Color.WHITE)buttons[row][col].setBackground(currentPlayer == 1 ? Color.BLACK : Color.WHITE);board[row][col] = currentPlayer;if (isWinningMove(row, col))JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");System.exit(0);}currentPlayer = currentPlayer == 1 ? 2 : 1;} elseJOptionPane.showMessageDialog(null, "Invalid move!");}}}private boolean isWinningMove(int row, int col)// Check horizontalint count = 0;for (int i = row - 4; i <= row + 4; i++)if (i >= 0 && i < BOARD_SIZE && board[i][col] == currentPlayer)count++;if (count >= 5)return true;}} elsecount = 0;}}// Check verticalcount = 0;for (int j = col - 4; j <= col + 4; j++)if (j >= 0 && j < BOARD_SIZE && board[row][j] == currentPlayer)count++;if (count >= 5)return true;}} elsecount = 0;}}// Check diagonal1count = 0;for (int i = row - 4, j = col - 4; i <= row + 4 && j <= col + 4; i++, j++)if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && board[i][j] == currentPlayer)count++;if (count >= 5)return true;}} elsecount = 0;}}// Check diagonal2count = 0;for (int i = row - 4, j = col + 4; i <= row + 4 && j >= col - 4; i++, j--)if (i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE && board[i][j] == currentPlayer)count++;if (count >= 5)return true;}} elsecount = 0;}}return false;}public static void main(String[] args)new GomokuGame(;}```以上是一种简单的基于JAVA的五子棋游戏设计方案,游戏界面使用了JFrame和JButton来创建,游戏逻辑通过按钮的点击事件来实现。

java课程设计报告_五子棋

java课程设计报告_五子棋

目录第一章软件平台……………………………游戏设计思路…………………………第二章小游戏五子棋的开发过程………………第一节JAVA基础知识………………………第二节启动程序的分析……………………第三节游戏设计过程………………………第三章总结…………………………………………第四章参考文献……………………………………第五章附录…………………………………………第一章游戏设计思路:第二章第一节java程序基础知识本系统有4个程序每个程序都导入了不同的包和类运用了不同的所学知识。

不同的类和包提供的方法、类型等都不同,本程序还使用了继承。

以至使其能够完成不同的功能。

本节主要讲述本系统所使用的基础知识。

1、类的基础<1、类的定义JAVA中的类实质上是一种对象类型,它是对具有相同属性和相同行为对象的一种抽象。

类是java程序中最小的组成单位。

<2、Java中类定义的格式为:[类修饰符] class 类名[extends 基类] [implements 接口]{// 成员变量声明(Member variable declaration)// 成员方法声明(Member method declaration)}其中,class、extends和implements都是Java的关键字。

类修饰符、extends和implements是关于修饰符、继承和接口的内容。

2、继承继承性是面向对象程序设计语言的一个重要特征,通过继承可以实现代码的复用。

Java语言中,所有的类都是直接或间接的继承ng.object类而得到的。

被继承的类称为基类或父类,继承而的来的类成为子类。

子类继承基类的方法和属性,同时也可以修改基类的方法和属性,并增加自己新的属性和方法。

Java不支持多重继承。

即一个子类不能拥有两个或以上的父类。

3、包<1、包的定义在Java中,把相关的代码组织到一起,称为“包”。

包市一种将相关类、接口或其他包组织起来的集合体,目的是为了将包含类代码的文件组织起来,易于查找和使用。

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现五子棋是一种非常有趣的棋类游戏,本文将基于JAVA语言对五子棋游戏系统进行设计与实现。

1.系统需求分析首先,我们需要对五子棋游戏系统的需求进行分析。

五子棋游戏的基本规则是:两名玩家轮流落子,先连成五子的一方获胜。

游戏棋盘是一个15*15的方格,玩家可以在空白的方格上落子,每次只能落一个子。

2.系统设计基于需求分析,我们将五子棋游戏系统分为四个模块:游戏界面、游戏规则、游戏控制和游戏存储。

游戏界面模块:该模块负责游戏的显示和用户交互。

我们可以使用图形用户界面(GUI)来实现游戏界面,可以使用JAVA的Swing或JavaFX来进行界面设计。

游戏规则模块:该模块负责规定游戏的规则和判断胜负。

该模块主要包括判断是否有玩家连成五子和判断当前位置是否可以落子等方法。

游戏控制模块:该模块负责控制游戏的流程,包括轮流落子、判断胜负、判断平局等。

该模块需要和游戏规则模块进行交互。

游戏存储模块:该模块负责保存游戏的进度和记录,可以使用文件系统或数据库来进行存储。

3.系统实现接下来,我们开始实现五子棋游戏系统。

1)游戏界面模块:可以使用JavaFX实现游戏界面,包括棋盘的绘制、玩家落子的交互等。

2)游戏规则模块:实现判断是否有玩家连成五子的方法,以及判断当前位置是否可以落子的方法。

3)游戏控制模块:实现游戏的流程控制,包括轮流落子、判断胜负、判断平局等。

4)游戏存储模块:实现游戏进度和记录的保存,可以使用文件系统或数据库进行存储。

4.系统测试与优化完成系统实现后,我们需要对系统进行测试和优化。

可以通过对游戏进行多次测试,检查游戏的各个模块是否正常工作,并根据测试结果对系统进行优化。

5.系统部署与使用完成测试和优化后,我们可以将游戏系统进行部署,供用户使用。

总结:通过以上设计与实现,我们成功地基于Java语言实现了一个五子棋游戏系统。

该系统不仅具备了基本的游戏功能,还具备了用户友好的界面和存储功能。

java课程设计五子棋游戏完整版

java课程设计五子棋游戏完整版

沈阳大学沈阳大学沈阳大学沈阳大学沈阳大学沈阳大学沈阳大学沈阳大学算下一步棋,isMatrix位true参数为插入点棋格坐标,否则参数为插入点像素坐标。

当游戏结束的时候使用Graphics g调用方法,在游戏过程中如果是黑方或是白方获胜在棋盘中就会显示是哪方获胜。

3.设计结果与分析(1)当进入到游戏当中可以选择是哪个颜色的棋先出,选择好之后便可以单机开始进行下棋,如果在游戏过程中出现什么问题可以单机再来一局重新开始游。

五子棋游戏的主界面如图2所示:图2.程序主界面(2)如果实黑棋先开始那么开始后的界面中先出的棋显示的是黑棋,如果是白棋先出则显示的是白棋,以黑棋为例如图3所示:沈阳大学图3.黑棋先出的界面图(3)对于五子棋游戏,无论人机对战,还是双人对战,都需要判断棋盘上是否存在五子连珠情况,这既是游戏名字的由来,也是游戏结束的标志。

判断五子连珠的原理是从横,竖,左斜线,右斜线4条线上判断是否存在5个相连的同类棋子,黑棋的五个棋子在横、竖或斜方向上连接成一线即为该方赢如图4所示:沈阳大学图4.游戏结束界面(4)白棋的五个棋子在横、竖或斜方向上连接成一线即为该方赢如图4所示:沈阳大学图5.白棋胜利界面图4.设计体会通过这次课程设计,我进一步加深对基础理论的理解,扩大专业知识面,对收集资料、查阅文献、方案制定等实践方面得到了很好的锻练,促进对所学知识应用能力的提高。

同时我渐渐的复习了Java使用方法和编程语法,之后的编程过程也相对得心应手,基本完成了预期计划的要求。

在此感谢我的指导老师—原玥老师,她在课题的研究上不遗余力的给予指导。

她严谨的治学态度深深的影响了我,使我受益匪浅!这个课设题对我来说有一定的挑战性,因为我自己也不知道自己到底学到了一个什么样的境界。

但可以肯定,这个课设题的要求绝对在我的能力范围以上。

之所以选择这个题目是想逼着自己去查更多的资料,学到更多的东西。

结果也确实是这样的,我在这次课设中学到了不少东西,也理解了许多原来不理解的东西。

java课程设计报告五子棋

工作原理图2.2功能的规划1.建立一个棋盘类,绘制棋盘的样式,在棋盘面板上添加各种按钮及相应胡触发事件。

同时注意面板的设计,使面板设计的尽量胡的合理美观。

a.棋盘的绘制public void draw_qipan(Graphics G) //画棋盘 15*15{G.setColor(Color.lightGray);G.fill3DRect(10,10,300,300,true);G.setColor(Color.black);for(int i=1;i<16;i++){G.drawLine(20,20*i,300,20*i);G.drawLine(20*i,20,20*i,300);}}b.添加按钮与按钮胡规划Button b1=new Button("开始");Button b2=new Button("重置游戏");Label lblWin=new Label(" ");Checkbox ckbHB[]=new Checkbox[3];Button exist = new Button("退出");public void init(){ckbHB[0]=new Checkbox("执白",ckgHB,false);ckbHB[1]=new Checkbox("执黑",ckgHB,false);ckbHB[2]=new Checkbox("观看",ckgHB, false);}C.鼠标棋子胡相应触发事件public void mouseClicked(MouseEvent e) {Graphics g=getGraphics();int x1,y1;x1=e.getX();y1=e.getY();if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()>300) { return;}if (x1%20>10) {x1+=10;}if(y1%20>10) {y1+=10;}x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1);m*=(-1);}d.按钮的触发事件public void actionPerformed(ActionEvent e) {Graphics g=getGraphics();if (e.getSource()==b1) {Game_start();}else{Game_re();}if(e.getSource()==exist){Game_re();lblWin.setText(Get_qizi_color(color_Qizi)+"输了!");intGame_Start=0;}2.编写一个功能判断落子的位置及画出相应的黑白棋子public void set_Qizi(int x,int y) //落子{if (intGame_Start==0) //判断游戏未开始{return;}if (intGame_Body[x/20][y/20]!=0) {return;}Graphics g=getGraphics();if (color_Qizi==1)//判断黑子还是白子{g.setColor(Color.black);color_Qizi=0;}else{g.setColor(Color.white);color_Qizi=1;}g.fillOval(x-10,y-10,20,20);intGame_Body[x/20][y/20]=color_Qizi+1;}3.判断胜负if (Game_win_1(x/20,y/20)) //判断输赢1{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_2(x/20,y/20)) //判断输赢2{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_3(x/20,y/20)) //判断输赢3{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_4(x/20,y/20)) //判断输赢4{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;} }4.添加用户聊天功能NetchatClient chat=new NetchatClient();chat.frame();chat.setVisible(true);三具体实现3.1创建棋盘类Public class WcyChess extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener{ int color_Qizi=0;//旗子的颜色标识 0:白子 1:黑子int intGame_Start=0;//游戏开始标志 0未开始 1游戏中int intGame_Body[][]=new int[16][16]; //设置棋盘棋子状态int m=-1;Button b1=new Button("开始");Button b2=new Button("重置游戏");Label lblWin=new Label(" ");Checkbox ckbHB[]=new Checkbox[3];Button exist = new Button("退出");CheckboxGroup ckgHB=new CheckboxGroup();NetchatClient chat=new NetchatClient();public void init(){setLayout(null);addMouseListener(this);add(b1);b1.setBounds(330,50,80,30);b1.addActionListener(this);add(b2);b2.setBounds(330,90,80,30);b2.addActionListener(this);ckbHB[0]=new Checkbox("执白",ckgHB,false);ckbHB[0].setBounds(320,20,60,30);ckbHB[1]=new Checkbox("执黑",ckgHB,false);ckbHB[1].setBounds(380,20,60,30);ckbHB[2]=new Checkbox("观看",ckgHB, false);add(ckbHB[0]);add(ckbHB[1]);add(ckbHB[2]);ckbHB[0].addItemListener(this);ckbHB[1].addItemListener(this);add(lblWin);lblWin.setBounds(330,180,80,30);lblWin.setBackground(Color.red);lblWin.setText("胜利者!");//没有显示?ckbHB[2].setBounds(440, 20,60, 30);add(exist);exist.setBounds(330,130,80,30);exist.addActionListener(this);// add(chat);chat.setBounds(20, 500, 300, 300);chat.frame();chat.setVisible(true);Game_start_csh();setSize(500,600);setVisible(true);}public void itemStateChanged(ItemEvent e) {if (ckbHB[0].getState()) //选择黑子还是白子{color_Qizi=0;}else{color_Qizi=1;}}public void mousePressed(MouseEvent e){}public void mouseClicked(MouseEvent e) {Graphics g=getGraphics();int x1,y1;x1=e.getX();y1=e.getY();if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()>300) { return;}if (x1%20>10) {x1+=10;}if(y1%20>10) {y1+=10;}x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1);m*=(-1);}public void actionPerformed(ActionEvent e) {Graphics g=getGraphics();if (e.getSource()==b1) {Game_start();}else {Game_re();}if(e.getSource()==exist){Game_re();lblWin.setText(Get_qizi_color(color_Qizi)+"输了!");intGame_Start=0;}}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseReleased(MouseEvent e){}public void mouseDragged(MouseEvent e){}public void mouseMoved(MouseEvent e){}public void paint(Graphics g) {draw_qipan(g);}3.2判断输赢功能实现if (Game_win_1(x/20,y/20)){ //判断输赢1lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_2(x/20,y/20)){//判断输赢2lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_3(x/20,y/20)) //判断输赢3{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}if (Game_win_4(x/20,y/20)) //判断输赢4{lblWin.setText(Get_qizi_color(color_Qizi)+"赢了!");intGame_Start=0;}//判断输赢程序实现在详细过程public boolean Game_win_1(int x,int y){ //横向判断输赢int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ) {if (x1>15) {break;}if (intGame_Body[x1+i][y1]==intGame_Body[x][y]) {t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<1)break;}if(intGame_Body[x1-i][y1]==intGame_Body[x][y])t+=1;}else{break;}}if (t>4){return true;}else{return false;}}public boolean Game_win_2(int x,int y) { //纵向判断输赢int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1][y1+i]==intGame_Body[x][y]){ t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1][y1-i]==intGame_Body[x][y]){ t+=1;}else{break;}}if (t>4){return true;}else{return false;}}public boolean Game_win_3(int x,int y) //左斜判断输赢{ int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ){if (x1>15){break;}if (intGame_Body[x1+i][y1-i]==intGame_Body[x][y]){ t+=1;}else{break;}}for (int i=1;i<5 ;i++ ){if (x1<1){break;}if(intGame_Body[x1-i][y1+i]==intGame_Body[x][y]){ t+=1;}else{break;}}if (t>4){return true;}else{return false;}}public boolean Game_win_4(int x,int y) { //左斜判断输赢int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i++ ) {if (x1>15) {break;}if (intGame_Body[x1+i][y1+i]==intGame_Body[x][y]) {t+=1;}else{break;}}for (int i=1;i<5 ;i++ ) {if (x1<1) {break;}if(intGame_Body[x1-i][y1-i]==intGame_Body[x][y]) {t+=1;}else{break; } }if (t>4) {return true;}else{return false;}}3.3实现聊天功能类package NetchatClient;import java.awt.*;import java.awt.event.*;import .*;import java.io.*;import javax.swing.*;public class NetchatClient extends Frame {Frame x=new Frame("liaotianchengxu");TextField tf=new TextField();TextArea ta=new TextArea();Socket s=null;DataOutputStream dos=null;public static void main(String[] args) {new NetchatClient().frame();}public void frame(){System.out.println("frame()");setLocation(20, 400);setSize(300,300);add(tf,BorderLayout.SOUTH);add(ta,BorderLayout.NORTH);pack();setVisible(true);connect();this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent arg0) { cconnect();System.exit(0);}});addWindowListener(new WindowAdapter()//窗口适配器 {public void windowClosing(WindowEvent e) {System.exit(0);//程序退出} } );tf.addActionListener(new nclistener());}public void connect() {System.out.println("connnect()..");try {s=new Socket("127.0.0.1",8888);//设置客户端的IP和端口 dos=new DataOutputStream(s.getOutputStream());} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void cconnect(){try {dos.close();s.close();} catch (IOException e) {e.printStackTrace();} }public class nclistener implements ActionListener{public void actionPerformed(ActionEvent e) {String st=tf.getText();ta.setText(st);tf.setText("");try {dos.writeUTF(st);dos.flush();} catch (IOException e1) {e1.printStackTrace();} } } }四测试结果4.1 进入游戏的界面分析:游戏开始的界面有三个选择项,用户可以选择相应的角色,选择完毕后点击开始进入游戏。

java课程设计五子棋

java课程设计五子棋一、课程目标知识目标:1. 理解五子棋游戏的规则及基本算法。

2. 掌握Java编程语言的基本语法,如变量声明、条件语句、循环结构等。

3. 学会使用Java中的数组进行数据存储和处理。

4. 了解面向对象编程的基本概念,如类的定义、对象创建和使用。

技能目标:1. 能够编写出实现五子棋游戏核心功能(如棋盘绘制、落子判断、胜负判断)的Java程序。

2. 通过编程实践,提升问题分析能力,学会将现实问题转化为计算机程序解决问题。

3. 能够使用合适的调试工具检查和修复程序中的错误,提高代码调试能力。

4. 培养良好的编程习惯,如编写规范的注释、合理的变量命名等。

情感态度价值观目标:1. 培养学生对编程的兴趣,激发其探究计算机科学领域的好奇心。

2. 鼓励学生积极合作,通过小组讨论和交流,培养团队协作精神。

3. 培养学生的逻辑思维能力和创新意识,使其在面对问题时能够主动思考、寻找解决方案。

4. 增强学生的自信心,使其在完成五子棋项目的过程中感受到编程带来的成就感。

课程性质分析:本课程为实践性较强的学科项目,结合Java编程语言,以五子棋游戏为载体,引导学生学习编程知识,培养其编程技能。

学生特点分析:学生处于能够理解编程基本概念,具备初步编程能力的阶段,对实际项目充满兴趣,希望将所学知识应用于实际问题。

教学要求:教师应注重理论与实践相结合,引导学生主动探索,关注学生个体差异,提供个性化指导,确保学生在完成课程目标的同时,提高编程能力和综合素质。

二、教学内容1. Java基本语法复习:变量声明与赋值、数据类型、运算符、条件语句(if-else、switch)、循环结构(for、while)。

相关教材章节:第一章至第三章。

2. 数组的使用:一维数组的定义、初始化、遍历;二维数组的应用。

相关教材章节:第四章。

3. 面向对象基础:类的定义、属性、方法;对象的创建与使用。

相关教材章节:第五章。

4. 五子棋游戏设计:a. 棋盘绘制:使用二维数组表示棋盘,绘制初始界面。

JAVA课程设计 五子棋(内附完整代码)

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语言有了更深的了解。

java课程设计五子棋

下面的原始码分为4个文件;chessclient.java:客户端主程式。

chessinterface.java:客户端的界面。

chesspad.java:棋盘的绘制。

chessserver.java:服务器端。

可同时容纳50个人同时在线下棋,聊天。

没有加上周详注释,不过绝对能运行,j2sdk1.4下通过。

/****************************************************************** ***************************1.chessclient.java******************************************************************* ***************************/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class clientthread extends thread{chessclient chessclient;clientthread(chessclient chessclient){this.chessclient=chessclient;}public void acceptmessage(string recmessage){if(recmessage.startswith("/userlist ")){stringtokenizer usertoken=new stringtokenizer(recmessage," "); int usernumber=0;erlist.removeall();erchoice.removeall();erchoice.additem("所有人");while(usertoken.hasmoretokens()){string user=(string)usertoken.nexttoken(" ");if(usernumber>0 && !user.startswith("[inchess]")){erlist.add(user);erchoice.additem(user);}usernumber++;}erchoice.select("所有人");}else if(recmessage.startswith("/yourname ")){chessclient.chessclientname=recmessage.substring(10);chessclient.settitle("java五子棋客户端 "+"用户名:"+chessclient.chessclientname);}else if(recmessage.equals("/reject")){try{chessclient.chesspad.statustext.settext("不能加入游戏");chessclient.controlpad.cancelgamebutton.setenabled(false);chessclient.controlpad.joingamebutton.setenabled(true);chessclient.controlpad.creatgamebutton.setenabled(true);}catch(exception ef){chessclient.chatpad.chatlinearea.settext("chessclient.chesspad.ches ssocket.close无法关闭");}chessclient.controlpad.joingamebutton.setenabled(true);}else if(recmessage.startswith("/peer ")){chessclient.chesspad.chesspeername=recmessage.substring(6);if(chessclient.isserver){chessclient.chesspad.chesscolor=1;chessclient.chesspad.ismouseenabled=true;chessclient.chesspad.statustext.settext("请黑棋下子");}else if(chessclient.isclient){chessclient.chesspad.chesscolor=-1;chessclient.chesspad.statustext.settext("已加入游戏,等待对方下子...");}}else if(recmessage.equals("/youwin")){chessclient.isonchess=false;chessclient.chesspad.chessvictory(chessclient.chesspad.chesscolor);chessclient.chesspad.statustext.settext("对方退出,请点放弃游戏退出连接");chessclient.chesspad.ismouseenabled=false;}else if(recmessage.equals("/ok")){chessclient.chesspad.statustext.settext("创建游戏成功,等待别人加入...");}else if(recmessage.equals("/error")){chessclient.chatpad.chatlinearea.append("传输错误:请退出程式,重新加入\n");}else{chessclient.chatpad.chatlinearea.append(recmessage+"\n");chessclient.chatpad.chatlinearea.setcaretposition(chessclient.chatpad.chatlinearea.gettext().length());}}public void run(){string message="";try{while(true){message=chessclient.in.readutf();acceptmessage(message);}}catch(ioexception es){}}}public class chessclient extends frame implements actionlistener,keylistener{userpad userpad=new userpad();chatpad chatpad=new chatpad();controlpad controlpad=new controlpad();chesspad chesspad=new chesspad();inputpad inputpad=new inputpad();socket chatsocket;datainputstream in;dataoutputstream out;string chessclientname=null;string host=null;int port=4331;boolean isonchat=false; //在聊天?boolean isonchess=false; //在下棋?boolean isgameconnected=false; //下棋的客户端连接?boolean isserver=false; //如果是下棋的主机boolean isclient=false; //如果是下棋的客户端panel southpanel=new panel();panel northpanel=new panel();panel centerpanel=new panel();panel westpanel=new panel();panel eastpanel=new panel();chessclient(){super("java五子棋客户端");setlayout(new borderlayout());host=controlpad.inputip.gettext(); westpanel.setlayout(new borderlayout()); westpanel.add(userpad,borderlayout.north); westpanel.add(chatpad,borderlayout.center); westpanel.setbackground(color.pink); inputpad.inputwords.addkeylistener(this); chesspad.host=controlpad.inputip.gettext(); centerpanel.add(chesspad,borderlayout.center); centerpanel.add(inputpad,borderlayout.south);centerpanel.setbackground(color.pink);controlpad.connectbutton.addactionlistener(this); controlpad.creatgamebutton.addactionlistener(this); controlpad.joingamebutton.addactionlistener(this); controlpad.cancelgamebutton.addactionlistener(this); controlpad.exitgamebutton.addactionlistener(this); controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(false); southpanel.add(controlpad,borderlayout.center); southpanel.setbackground(color.pink); addwindowlistener(new windowadapter(){public void windowclosing(windowevent e){if(isonchat){try{chatsocket.close();}catch(exception ed){}}if(isonchess || isgameconnected) {try{chesspad.chesssocket.close();}catch(exception ee){}}system.exit(0);}public void windowactivated(windowevent ea) {}});add(westpanel,borderlayout.west);add(centerpanel,borderlayout.center);add(southpanel,borderlayout.south);pack();setsize(670,548);setvisible(true);setresizable(false);validate();}public boolean connectserver(string serverip,int serverport) throws exception{try{chatsocket=new socket(serverip,serverport);in=new datainputstream(chatsocket.getinputstream());out=new dataoutputstream(chatsocket.getoutputstream());clientthread clientthread=new clientthread(this);clientthread.start();isonchat=true;return true;}catch(ioexception ex){chatpad.chatlinearea.settext("chessclient:connectserver:无法连接,建议重新启动程式\n");}return false;}public void actionperformed(actionevent e){if(e.getsource()==controlpad.connectbutton){host=chesspad.host=controlpad.inputip.gettext();try{if(connectserver(host,port)){chatpad.chatlinearea.settext("");controlpad.connectbutton.setenabled(false);controlpad.creatgamebutton.setenabled(true);controlpad.joingamebutton.setenabled(true);chesspad.statustext.settext("连接成功,请创建游戏或加入游戏"); }}catch(exception ei){chatpad.chatlinearea.settext("controlpad.connectbutton:无法连接,建议重新启动程式\n");}}if(e.getsource()==controlpad.exitgamebutton){if(isonchat){try{chatsocket.close();}catch(exception ed){}}if(isonchess || isgameconnected){try{chesspad.chesssocket.close();}catch(exception ee){}}system.exit(0);}if(e.getsource()==controlpad.joingamebutton){string selecteduser=erlist.getselecteditem();if(selecteduser==null || selecteduser.startswith("[inchess]") || selecteduser.equals(chessclientname)){chesspad.statustext.settext("必须先选定一个有效用户");}else{try{if(!isgameconnected){if(chesspad.connectserver(chesspad.host,chesspad.port)){isgameconnected=true;isonchess=true;isclient=true;controlpad.creatgamebutton.setenabled(false);controlpad.joingamebutton.setenabled(false);controlpad.cancelgamebutton.setenabled(true);chesspad.chessthread.sendmessage("/joingame"+erlist.getselecteditem()+" "+chessclientname);}}else{isonchess=true;isclient=true;controlpad.creatgamebutton.setenabled(false);controlpad.joingamebutton.setenabled(false);controlpad.cancelgamebutton.setenabled(true);chesspad.chessthread.sendmessage("/joingame"+erlist.getselecteditem()+" "+chessclientname);}}catch(exception ee){isgameconnected=false;isonchess=false;isclient=false;controlpad.creatgamebutton.setenabled(true);controlpad.joingamebutton.setenabled(true);controlpad.cancelgamebutton.setenabled(false);chatpad.chatlinearea.settext("chesspad.connectserver无法连接\n"+ee);}}}if(e.getsource()==controlpad.creatgamebutton){try{if(!isgameconnected){if(chesspad.connectserver(chesspad.host,chesspad.port)){isgameconnected=true;isonchess=true;isserver=true;controlpad.creatgamebutton.setenabled(false);controlpad.joingamebutton.setenabled(false);controlpad.cancelgamebutton.setenabled(true);chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname);}}else{isonchess=true;isserver=true;controlpad.creatgamebutton.setenabled(false);controlpad.joingamebutton.setenabled(false);controlpad.cancelgamebutton.setenabled(true);chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname);}}catch(exception ec){isgameconnected=false;isonchess=false;isserver=false;controlpad.creatgamebutton.setenabled(true);controlpad.joingamebutton.setenabled(true);controlpad.cancelgamebutton.setenabled(false);ec.printstacktrace();chatpad.chatlinearea.settext("chesspad.connectserver无法连接\n"+ec);}}if(e.getsource()==controlpad.cancelgamebutton){if(isonchess){chesspad.chessthread.sendmessage("/giveup "+chessclientname); chesspad.chessvictory(-1*chesspad.chesscolor);controlpad.creatgamebutton.setenabled(true);controlpad.joingamebutton.setenabled(true);controlpad.cancelgamebutton.setenabled(false);chesspad.statustext.settext("请建立游戏或加入游戏");}if(!isonchess){controlpad.creatgamebutton.setenabled(true);controlpad.joingamebutton.setenabled(true);controlpad.cancelgamebutton.setenabled(false);chesspad.statustext.settext("请建立游戏或加入游戏");}isclient=isserver=false;}}public void keypressed(keyevent e){textfield inputwords=(textfield)e.getsource();if(e.getkeycode()==keyevent.vk_enter){if(erchoice.getselecteditem().equals("所有人")) {try{out.writeutf(inputwords.gettext());inputwords.settext("");}catch(exception ea){chatpad.chatlinearea.settext("chessclient:keypressed无法连接,建议重新连接\n");erlist.removeall();erchoice.removeall();inputwords.settext("");controlpad.connectbutton.setenabled(true);}}else{try{out.writeutf("/"+erchoice.getselecteditem()+""+inputwords.gettext());inputwords.settext("");}catch(exception ea){chatpad.chatlinearea.settext("chessclient:keypressed无法连接,建议重新连接\n");erlist.removeall();erchoice.removeall();inputwords.settext("");controlpad.connectbutton.setenabled(true);}}}}public void keytyped(keyevent e){}public void keyreleased(keyevent e){}public static void main(string args[]){chessclient chessclient=new chessclient();}}/****************************************************************** ************************下面是:chessinteface.java******************************************************************* ***********************/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;class userpad extends panel{list userlist=new list(10);userpad(){setlayout(new borderlayout());for(int i=0;i<50;i++){userlist.add(i+"."+"没有用户");}add(userlist,borderlayout.center);}}class chatpad extends panel{textarea chatlinearea=newtextarea("",18,30,textarea.scrollbars_vertical_only);chatpad(){setlayout(new borderlayout());add(chatlinearea,borderlayout.center);}}class controlpad extends panel{label iplabel=new label("ip",label.left); textfield inputip=new textfield("localhost",10); button connectbutton=new button("连接主机"); button creatgamebutton=new button("建立游戏"); button joingamebutton=new button("加入游戏"); button cancelgamebutton=new button("放弃游戏"); button exitgamebutton=new button("关闭程式"); controlpad(){setlayout(new flowlayout(flowlayout.left));setbackground(color.pink);add(iplabel);add(inputip);add(connectbutton);add(creatgamebutton);add(joingamebutton);add(cancelgamebutton);add(exitgamebutton);}}class inputpad extends panel{textfield inputwords=new textfield("",40); choice userchoice=new choice();inputpad(){setlayout(new flowlayout(flowlayout.left));for(int i=0;i<50;i++){userchoice.additem(i+"."+"没有用户");}userchoice.setsize(60,24);add(userchoice);add(inputwords);}}/****************************************************************** ****************************下面是:chesspad.java******************************************************************* ***************************/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class chessthread extends thread{chesspad chesspad;chessthread(chesspad chesspad){this.chesspad=chesspad;}public void sendmessage(string sndmessage){try{chesspad.outdata.writeutf(sndmessage);}catch(exception ea){system.out.println("chessthread.sendmessage:"+ea);}}public void acceptmessage(string recmessage){if(recmessage.startswith("/chess ")){stringtokenizer usertoken=new stringtokenizer(recmessage," "); string chesstoken;string[] chessopt={"-1","-1","0"};int chessoptnum=0;while(usertoken.hasmoretokens()){chesstoken=(string)usertoken.nexttoken(" ");if(chessoptnum>=1 && chessoptnum<=3){chessopt[chessoptnum-1]=chesstoken;}chessoptnum++;}chesspaint(integer.parseint(chessopt[0]),integer.parsei nt(chessopt[1]),integer.parseint(chessopt[2]));}else if(recmessage.startswith("/yourname ")){chesspad.chessselfname=recmessage.substring(10);}else if(recmessage.equals("/error")){chesspad.statustext.settext("错误:没有这个用户,请退出程式,重新加入");}else{//system.out.println(recmessage);}}public void run(){string message="";try{while(true){message=chesspad.indata.readutf(); acceptmessage(message);}}catch(ioexception es){}}}class chesspad extends panel implementsmouselistener,actionlistener{int chesspoint_x=-1,chesspoint_y=-1,chesscolor=1;int chessblack_x[]=new int[200];int chessblack_y[]=new int[200];int chesswhite_x[]=new int[200];int chesswhite_y[]=new int[200];int chessblackcount=0,chesswhitecount=0;int chessblackwin=0,chesswhitewin=0;boolean ismouseenabled=false,iswin=false,isingame=false;textfield statustext=new textfield("请先连接服务器");socket chesssocket;datainputstream indata;dataoutputstream outdata;string chessselfname=null;string chesspeername=null;string host=null;int port=4331;chessthread chessthread=new chessthread(this);chesspad(){setsize(440,440);setlayout(null);setbackground(color.pink);addmouselistener(this);add(statustext);statustext.setbounds(40,5,360,24);statustext.seteditable(false);}public boolean connectserver(string serverip,int serverport) throws exception{try{chesssocket=new socket(serverip,serverport);indata=new datainputstream(chesssocket.getinputstream()); outdata=new dataoutputstream(chesssocket.getoutputstream()); chessthread.start();return true;}catch(ioexception ex){statustext.settext("chesspad:connectserver:无法连接\n"); }return false;}public void chessvictory(int chesscolorwin){this.removeall();for(int i=0;i<=chessblackcount;i++) {chessblack_x[i]=0;chessblack_y[i]=0;}for(int i=0;i<=chesswhitecount;i++) {chesswhite_x[i]=0;chesswhite_y[i]=0;}chessblackcount=0;chesswhitecount=0;add(statustext);statustext.setbounds(40,5,360,24); if(chesscolorwin==1){ chessblackwin++;statustext.settext("黑棋胜,黑:白为"+chessblackwin+":"+chesswhitewin+",重新开局,等待白棋下子...");}else if(chesscolorwin==-1){chesswhitewin++;statustext.settext("白棋胜,黑:白为"+chessblackwin+":"+chesswhitewin+",重新开局,等待黑棋下子...");}}public void getlocation(int a,int b,int color){if(color==1){chessblack_x[chessblackcount]=a*20;chessblack_y[chessblackcount]=b*20;chessblackcount++;}else if(color==-1){chesswhite_x[chesswhitecount]=a*20;chesswhite_y[chesswhitecount]=b*20;chesswhitecount++;}}public boolean checkwin(int a,int b,int checkcolor){int step=1,chesslink=1,chesslinktest=1,chesscompare=0;if(checkcolor==1){chesslink=1;for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if(((a+step)*20==chessblack_x[chesscompare]) && ((b*20)==chessblack_y[chesscompare])){chesslink=chesslink+1;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if(((a-step)*20==chessblack_x[chesscompare]) &&(b*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}chesslink=1;chesslinktest=1;for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if((a*20==chessblack_x[chesscompare]) &&((b+step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if((a*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}chesslink=1;chesslinktest=1;for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if(((a-step)*20==chessblack_x[chesscompare]) &&((b+step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if(((a+step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}chesslink=1;chesslinktest=1;for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++){if(((a+step)*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;}for(step=1;step<=4;step++){for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) {if(((a-step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])){chesslink++;if(chesslink==5){return(true);}}}if(chesslink==(chesslinktest+1))chesslinktest++;elsebreak;。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档