软件测试文
题目:五子棋开发测试
班 级: 计算机科学技术特色班
组成员:
完成日期: 2015
指导教师:
摘
五子棋游戏仅增强思维力提高记忆力富含哲理助修身养性场概念点连接古哲理结晶开发出款简单实单机版五子棋软件意义
文分析五子棋游戏游戏规设计游戏算法选择eclipse作该软件开发台实现五子棋种战模式战电脑战实现定义棋盘行列数
文开发环境安装五子棋功分析设计功模块实现逐步您展现五子棋实现程测试游戏系统功齐全稳运行具较强价值
关键词:五子棋思考算法战机战
目 录
1引言 1
11编写目 1
12背景 1
2单机版五子棋系统介绍 2
21台简介 2
22游戏概述 2
3基Java五子棋游戏设计 3
31游戏模块 3
311 开局退出棋盘 3
312 模式栏 4
32游戏流程图 4
4软件测试 5
41测试目 5
42测试方法 5
43白盒测试 6
431测试例 7
44黑盒测试 10
45测试游戏存问题 12
46游戏足解决方案 12
461游戏改进代码 12
47回测试 13
5总结 15
参考文献 16
附录 17
1引言
11编写目
根测试计划报告软件进行测试详细记录测试程软件质量进行评价软件设计员提供BUG做产生测试分析报告
12背景
游戏更加完善玩家更游戏体验针游戏出现问题做出修复游戏更加生动符合玩家游戏惯
2单机版五子棋系统介绍
21台简介
Eclipse 开放源代码基Java扩展开发台身言框架组服务通插件组件构建开发环境幸运Eclipse 附带标准插件集包括Java开发工具(Java Development KitJDK)
22游戏概述
该游戏软件采 Java 语言设计 Windows 系统玩家提供五子棋游戏 机战基功玩家通鼠标棋盘相应位置子方白色 方黑色先黑子(场)游戏程中先连成五棋子 横竖斜会屏幕显示谁赢赛局收场 进行场赛赛程中果方需悔棋先通手意
3基Java五子棋游戏设计
项目中五子棋包含两模块图31(a)示第模块模块实现五子棋游戏功第二模块辅助模块实现五子棋相关辅助设置游戏界面图31(b)
五子棋游戏
视图帮助块
游戏模块
图31(a) 模块
图31(b)游戏界面
31游戏模块
该模块实现游戏功包含两模块图32(a)示第模块棋盘栏实现游戏玩家点击棋功第二模块模式栏棋方式进行选择游戏菜单图32(b)示
游戏模块
模式栏
开局退出棋盘栏
图32(a) 游戏模块
图32(b) 游戏菜单
311 开局退出棋盘
实现视化开局退出功棋盘选择战模式
图33 开局退出棋盘
312 模式栏
模式栏实现游戏程中种控制功包括机实例化战模式
图34 模式选择
电脑子
电脑否子
电脑子
电脑否子
否
游戏结束
白子黑子否5连成线
否
游戏者否子
否
游戏者子
模式选择
开局
32游戏流程图
4软件测试
41测试目
软件测试发现错误执行程序程者说软件测试根软件开发阶段规格说明程序部结构精心设计批测试例{输入数预期输出结果}利测试例运行程序发现程序错误程设计测试目标想少时间力系统找出软件中潜种错误缺陷果成功实施测试够发现软件中错误测试附带收获够证明软件功性需求说明相符合外实施测试收集测试结果数性分析提供测试目软件投入生产性运行前发现软件中错误
42测试方法
常见软件测试阶段工作分单元测试集成测试确认测试系统测试四步骤测试方法黑盒测试白盒测试两种方法:黑盒测试(已知道产品应该具功通测试检验否功正常)白盒测试(果知道产品部工作程通测试检验部动作否说明书规定正常进行)
游戏测试方法包括黑盒测试白盒测试组员通亲玩然观察游戏运行程中需实现功没实现测试程中没发现新问题记录案然根测试产生系列问题组负责编码员游戏进行改进然进行测试样重复进行样工作直游戏运行预想时状况基致时算通测试
图41 完整游戏图
43白盒测试
通程序源代码进行测试户界面种类型测试需代码句法发现部代码算法溢出路径条件等等中缺点者错误进加修正方法测试象作开盒子测试员程序部逻辑结构相关信息设计选择测试例程序逻辑路径进行测试通点检查程序状态确定实际状态否预期状态致软件白盒测试产生测试例够:
1保证模块中独立路径少次
2逻辑值均需测试truefalse
3边界操作范围运行循环
4检查部数结构确保效性
431测试例
(1)例1:
类ChessFrame功创建五子棋游戏窗体进行测试构造五子棋游戏窗体方法否正确
class ChessFrame extends JFrame implements ActionListener {
private String[] strsize{20x1530x2040x30}
private String[] strmode{机弈弈}
public static boolean iscomputertruecheckcomputertrue
private int widthheight
private ChessModel cm
private MainPanel mp
public ChessFrame() {
thissetTitle(五子棋游戏)
cmnew ChessModel(1)
mpnew MainPanel(cm)
Container conthisgetContentPane()
conadd(mpCenter)
thissetResizable(false)
thisaddWindowListener(new ChessWindowEvent())
MapSize(2015)
JMenuBar mbar new JMenuBar()
thissetJMenuBar(mbar)
JMenu gameMenu new JMenu(游戏)
mbaradd(makeMenu(gameMenu new Object[] {
开局 棋盘模式 null 退出
} this))
JMenu lookMenu new JMenu(视图)
mbaradd(makeMenu(lookMenunew Object[] {
MetalMotifWindows
}this))
JMenu helpMenu new JMenu(帮助)
mbaradd(makeMenu(helpMenu new Object[] {
关
} this))
}
(2)例2:
play(int xint y)方法实现电脑子先通方法readyplay(int xint y)判断某坐标位置否棋子
public void readyplay(int xint y){
if(badxy(xy))
return
if (chessExist(xy))
return
thisarrMapShow[x][y]3
}
public void play(int xint y){
if(badxy(xy))
return
if(chessExist(xy)){
thisisExisttrue
return
}else
thisisExistfalse
if(getisOdd()){
setisOdd(false)
thisarrMapShow[x][y]1
}else{
setisOdd(true)
thisarrMapShow[x][y]2
}
}
(3)例3:
计算机走棋方法(computerDo)测试
通方法 computerDo实现电脑子思想
public void computerDo(int widthint height){
int max_blackmax_whitemax_tempmax0
setisOdd(true)
Systemoutprintln(计算机走棋 )
for(int i 0 i < width i++){
for(int j 0 j < height j++){
if(chessExist(ij)){
max_whitecheckMax(ij2)
max_blackcheckMax(ij1)
max_tempMathmax(max_whitemax_black)
if(max_temp>max){
maxmax_temp
thisxi
thisyj }}}}
(4)例4:
根提供棋子信息(颜色坐标)画棋子方法( draw)测试
public void draw(Graphics g int i int j int v){
int x 20 * i+20
int y 20 * j+20
画棋盘
if(iwidth && jheight){
gsetColor(Colorwhite)
gdrawRect(xy2020)
}
画黑色棋子
if(v 1 ){
gsetColor(Colorgray)
gdrawOval(x8y81616)
gsetColor(Colorblack)
gfillOval(x8y81616)
}
画白色棋子
if(v 2 ){
gsetColor(Colorgray)
gdrawOval(x8y81616)
gsetColor(Colorwhite)
gfillOval(x8y81616)
}
if(v 3){
gsetColor(Colorcyan)
gdrawOval(x8y81616)
}
}
(5)例5:
响应鼠标点击事件构造方法(mousePressed)实现鼠标点击实现棋动作
public void mousePressed(MouseEvent evt){
int x (evtgetX()10) 20
int y (evtgetY()10) 20
Systemoutprintln(x+ +y)
if (evtgetModifiers()MouseEventBUTTON1_MASK){
cmplay(xy)
Systemoutprintln(cmgetisOdd()+ +cmgetarrMapShow()[x][y])
repaint()
if(cmjudgeSuccess(xycmgetisOdd())){
cmshowSuccess(this)
evtconsume()
ChessFrameiscomputerfalse
}
(6)例6:
构造方法judgeSuccess(int xint yboolean isodd)前棋子位置判断出否胜负
public boolean judgeSuccess(int xint yboolean isodd){
int num1
int arrvalue
int x_tempxy_tempy
if(isodd)
arrvalue2
else
arrvalue1
int x_temp1x_tempy_temp1y_temp
44黑盒测试
软件黑盒测试软件测试方法称功测试数驱动测试基规格说明测试测试者解程序部情况知道程序输入输出系统功户角度针软件界面功外部结构进行测试考虑程序部逻辑结构
软件黑盒测试法注重测试软件功需求试图发现列类错误:
1)功正确遗漏
2)界面错误
3)数库访问错误
4)性错误
5)初始化终止错误等
测试方法分种:等价类划分法边界值分析法错误推测法果图法判定表驱动法正交试验设计法功图法等
理讲软件黑盒测试采穷举输入测试输入作测试情况考虑查出程序中错误实际测试情况穷仅测试合法输入合法输入进行测试样完全测试进行针性测试通制定测试案例指导测试实施保证软件测试组织步骤计划进行软件黑盒测试行必须够加量化真正保证软件质量测试例测试行具体量化方法
五子棋游戏测试例表示:
例
操作
预期结果
实际结果
例1
否版eclipese中运行该游戏
游戏够设计该游戏
版中正常运行
Pass
例2
运行时否正常显示
界面
运行时正常显示
界面
Pass
例3
鼠标单击菜单—开始否运行游戏
运行游戏
Pass
例4
鼠标单击棋盘否会棋子落该位置
棋子落鼠标单击位置
Pass
例5
子出现活三四电脑否会子堵截
电脑会子阻挡
Pass
例6
次游戏测试:(黑棋)没出现活三四情况电脑(白棋)落子否存规律
黑棋没出现活三
四情况电脑
(白棋)机落子存
规律
Pass
例7
进行战时规棋否正常输赢
正常输赢
Pass
例8
判断横线5子否获胜利
Pass
例9
判断竖线5子否获胜利
Pass
例10
判断斜线5子否获胜利
Pass
45测试游戏存问题
1通组外员游戏测试发现棋盘问题导致时棋子满格没分出胜负样难显示博弈者水
2组外员游戏测试建议认设计五子棋游戏实现机博弈实现间博弈样难更展高超棋艺
46游戏足解决方案
1通台代码修改实现游戏棋盘规格控制选择新添规格30*20格40*30格模式博弈者展高超棋艺
2机战基础新增战模式方便间棋艺博弈乐趣更爱五子棋玩家参进
461游戏改进
1 菜单中新加棋盘选项模式中新增规格30*20格40*30格模式
实现代码:
public ChessModel(int modeChess){
thisisOddtrue
if(modeChess 1){
PanelInit(20 15 modeChess) }
if(modeChess 2){
PanelInit(30 20 modeChess) }
if(modeChess 3){
PanelInit(40 30 modeChess)}}
private void PanelInit(int width int height int modeChess){
thiswidth width
thisheight height
thismodeChess 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] 5
}
}
}
2菜单中新加模式选项新增战模式
if(argequals(弈)){
thischeckcomputerfalse
thisiscomputerfalse
cmnew ChessModel(cmgetModeChess())
MapSize(cmgetWidth()cmgetHeight())
SwingUtilitiesupdateComponentTreeUI(this)
}
47回测试
修改游戏版执行回测试
例
操作
预期结果
测试结果
例1
选择规格棋盘否正常运行
正常运行
Pass
例2
选择战否正常运行
正常运行
Pass
例3
选择规格棋盘功正常运行否产生影响
没影响
Pass
例4
选择战模式功正常运行否产生影响
没影响
Pass
5总结
测试阶段系列测试方案破坏已建造软件系统特设计存竭力证明程序中错误预定求正确工作软件测试角度出发会派生出两种测试原户角度出发希通软件测试充分暴露软件中存问题缺陷考虑否接受该产品开发者角度出发希测试表明软件产品存错误已正确实现户需求确立软件质量信心
通次软件测试课程设计深刻学掌握软件测试软件测试程基方法基技术关黑盒白盒测试例设计进行认真学研究进步提高程序编写力前未触问题
总收获颇丰增强动手团队合作力付出会回报实践出真知踏社会做份理实践准备
参考文献
[1] 谢希仁 计算机网络(第五版)[M] 北京:电子工业出版社2008年2月
[2] 胡强 计算机网络[M] 北京:北京邮电学出版社2005年1月
[3] 刘群策 LoadRunner软件项目性测试 [M] 机械工业出版社 2008年
[4] 朱少民 软件测试方法技术 [M] 清华学出版社 2005年
[5] 郑杰 软件测试 [M] 民邮电出版社 2013年
附录
Java编程:五子棋游戏源代码
import javaawt*
import javaawtevent*
import javaapplet*
import javaxswing*
import javaioPrintStream
import javaxswingJComponent
import javaxswingJPanel
*
*main方法创建ChessFrame类实例象(cf)
*启动屏幕显示显示该实例象
**
public class FiveChessAppletDemo {
public static void main(String args[]){
ChessFrame cf new ChessFrame()
cfshow()
}
}
*
*类ChessFrame功创建五子棋游戏窗体菜单
**
class ChessFrame extends JFrame implements ActionListener {
private String[] strsize{20x1530x2040x30}
private String[] strmode{机弈弈}
public static boolean iscomputertruecheckcomputertrue
private int widthheight
private ChessModel cm
private MainPanel mp
构造五子棋游戏窗体
public ChessFrame() {
thissetTitle(五子棋游戏)
cmnew ChessModel(1)
mpnew MainPanel(cm)
Container conthisgetContentPane()
conadd(mpCenter)
thissetResizable(false)
thisaddWindowListener(new ChessWindowEvent())
MapSize(2015)
JMenuBar mbar new JMenuBar()
thissetJMenuBar(mbar)
JMenu gameMenu new JMenu(游戏)
mbaradd(makeMenu(gameMenu new Object[] {
开局 棋盘模式 null 退出
} this))
JMenu lookMenu new JMenu(视图)
mbaradd(makeMenu(lookMenunew Object[] {
MetalMotifWindows
}this))
JMenu helpMenu new JMenu(帮助)
mbaradd(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)
else
return null
for(int i 0 i < itemslength i++)
if(items[i] null)
maddSeparator()
else if(items[i] 棋盘){
JMenu jm new JMenu(棋盘)
ButtonGroup groupnew ButtonGroup()
JRadioButtonMenuItem rmenu
for (int j0j
if (j0)
rmenusetSelected(true)
jmadd(rmenu)
groupadd(rmenu)
}
madd(jm)
}else if(items[i] 模式){
JMenu jm new JMenu(模式)
ButtonGroup groupnew ButtonGroup()
JRadioButtonMenuItem rmenu
for (int h0h
if(h0)
rmenusetSelected(true)
jmadd(rmenu)
groupadd(rmenu)
}
madd(jm)
}else
madd(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
else
return null
if(target instanceof ActionListener)
raddActionListener((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
else
return null
if(target instanceof ActionListener)
raddActionListener((ActionListener)target)
return r
}
public void MapSize(int wint h){
setSize(w * 20+50 h * 20+100 )
if(thischeckcomputer)
thisiscomputertrue
else
thisiscomputerfalse
mpsetModel(cm)
mprepaint()
}
public boolean getiscomputer(){
return thisiscomputer
}
public void restart(){
int modeChess cmgetModeChess()
if(modeChess < 3 && modeChess > 1){
cm new ChessModel(modeChess)
MapSize(cmgetWidth()cmgetHeight())
}else{
Systemoutprintln(\u81EA\u5B9A\u4E49)
}
}
public void actionPerformed(ActionEvent e){
String argegetActionCommand()
try{
if (argequals(Windows))
UIManagersetLookAndFeel(
comsunjavaswingplafwindowsWindowsLookAndFeel)
else if(argequals(Motif))
UIManagersetLookAndFeel(
comsunjavaswingplafmotifMotifLookAndFeel)
else
UIManagersetLookAndFeel(
javaxswingplafmetalMetalLookAndFeel )
SwingUtilitiesupdateComponentTreeUI(this)
}catch(Exception ee){}
if(argequals(20x15)){
thiswidth20
thisheight15
cmnew ChessModel(1)
MapSize(thiswidththisheight)
SwingUtilitiesupdateComponentTreeUI(this)
}
if(argequals(30x20)){
thiswidth30
thisheight20
cmnew ChessModel(2)
MapSize(thiswidththisheight)
SwingUtilitiesupdateComponentTreeUI(this)
}
if(argequals(40x30)){
thiswidth40
thisheight30
cmnew ChessModel(3)
MapSize(thiswidththisheight)
SwingUtilitiesupdateComponentTreeUI(this)
}
if(argequals(机弈)){
thischeckcomputertrue
thisiscomputertrue
cmnew ChessModel(cmgetModeChess())
MapSize(cmgetWidth()cmgetHeight())
SwingUtilitiesupdateComponentTreeUI(this)
}
if(argequals(弈)){
thischeckcomputerfalse
thisiscomputerfalse
cmnew ChessModel(cmgetModeChess())
MapSize(cmgetWidth()cmgetHeight())
SwingUtilitiesupdateComponentTreeUI(this)
}
if(argequals(开局)){
restart()
}
if(argequals(关))
JOptionPaneshowMessageDialog(this 五子棋游戏测试版 关 0)
if(argequals(退出))
Systemexit(0)
}
}
*
*类ChessModel实现整五子棋程序算法核心
*
class ChessModel {
棋盘宽度高度棋盘模式(20×15)
private int widthheightmodeChess
棋盘方格横坐标
private int x0y0
棋盘方格横坐标应棋子颜色
数组arrMapShow3值:1235
中1代表该棋盘方格棋子黑子
2代表该棋盘方格棋子白子
3代表该棋盘方格没棋子
5代表该棋盘方格够棋子
private int[][] arrMapShow
交换棋手标识棋盘方格否棋子标识符
private boolean isOddisExist
public ChessModel() {}
该构造方法根棋盘模式(modeChess)构建应棋盘
public ChessModel(int modeChess){
thisisOddtrue
if(modeChess 1){
PanelInit(20 15 modeChess)
}
if(modeChess 2){
PanelInit(30 20 modeChess)
}
if(modeChess 3){
PanelInit(40 30 modeChess)
}
}
棋盘模式构建棋盘
private void PanelInit(int width int height int modeChess){
thiswidth width
thisheight height
thismodeChess 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] 5
}
}
}
获取否交换棋手标识符
public boolean getisOdd(){
return thisisOdd}
设置交换棋手标识符
public void setisOdd(boolean isodd){
if(isodd)
thisisOddtrue
else
thisisOddfalse
}
获取某棋盘方格否棋子标识值
public boolean getisExist(){
return thisisExist
}
获取棋盘宽度
public int getWidth(){
return thiswidth
}
获取棋盘高度
public int getHeight(){
return thisheight
}
获取棋盘模式
public int getModeChess(){
return thismodeChess
}
获取棋盘方格棋子信息
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 iint j){
if(thisarrMapShow[i][j]1 || thisarrMapShow[i][j]2)
return true
return false
}
判断该坐标位置否棋子
public void readyplay(int xint y){
if(badxy(xy))
return
if (chessExist(xy))
return
thisarrMapShow[x][y]3
}
该坐标位置棋子
public void play(int xint y){
if(badxy(xy))
return
if(chessExist(xy)){
thisisExisttrue
return
}else
thisisExistfalse
if(getisOdd()){
setisOdd(false)
thisarrMapShow[x][y]1
}else{
setisOdd(true)
thisarrMapShow[x][y]2
}
}
计算机走棋
*
*说明:穷举法判断坐标点四方棋子数
*出棋子数值坐标子
**
public void computerDo(int widthint height){
int max_blackmax_whitemax_tempmax0
setisOdd(true)
Systemoutprintln(计算机走棋 )
for(int i 0 i < width i++){
for(int j 0 j < height j++){
if(chessExist(ij)){算法判断否子
max_whitecheckMax(ij2)判断白子值
max_blackcheckMax(ij1)判断黑子值
max_tempMathmax(max_whitemax_black)
if(max_temp>max){
maxmax_temp
thisxi
thisyj
}
}
}
}
setX(thisx)
setY(thisy)
thisarrMapShow[thisx][thisy]2
}
记录电脑子横坐标
public void setX(int x){
thisxx
}
记录电脑子坐标
public void setY(int y){
thisyy
}
获取电脑子横坐标
public int getX(){
return thisx
}
获取电脑子坐标
public int getY(){
return thisy
}
计算棋盘某方格八方棋子值
八方分:左右左左右右
public int checkMax(int x int yint black_or_white){
int num0max_nummax_temp0
int x_tempxy_tempy
int x_temp1x_tempy_temp1y_temp
judge right
for(int i1i<5i++){
x_temp1+1
if(x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
judge left
x_temp1x_temp
for(int i1i<5i++){
x_temp11
if(x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
if(num<5)
max_tempnum
judge up
x_temp1x_temp
y_temp1y_temp
num0
for(int i1i<5i++){
y_temp11
if(y_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
judge down
y_temp1y_temp
for(int i1i<5i++){
y_temp1+1
if(y_temp1>thisheight)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
if(num>max_temp&&num<5)
max_tempnum
judge left_up
x_temp1x_temp
y_temp1y_temp
num0
for(int i1i<5i++){
x_temp11
y_temp11
if(y_temp1<0 || x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
judge right_down
x_temp1x_temp
y_temp1y_temp
for(int i1i<5i++){
x_temp1+1
y_temp1+1
if(y_temp1>thisheight || x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
if(num>max_temp&&num<5)
max_tempnum
judge right_up
x_temp1x_temp
y_temp1y_temp
num0
for(int i1i<5i++){
x_temp1+1
y_temp11
if(y_temp1<0 || x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
judge left_down
x_temp1x_temp
y_temp1y_temp
for(int i1i<5i++){
x_temp11
y_temp1+1
if(y_temp1>thisheight || x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]black_or_white)
num++
else
break
}
if(num>max_temp&&num<5)
max_tempnum
max_nummax_temp
return max_num
}
判断胜负
public boolean judgeSuccess(int xint yboolean isodd){
int num1
int arrvalue
int x_tempxy_tempy
if(isodd)
arrvalue2
else
arrvalue1
int x_temp1x_tempy_temp1y_temp
判断右边
for(int i1i<6i++){
x_temp1+1
if(x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
判断左边
x_temp1x_temp
for(int i1i<6i++){
x_temp11
if(x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
if(num5)
return true
判断方
x_temp1x_temp
y_temp1y_temp
num1
for(int i1i<6i++){
y_temp11
if(y_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
判断方
y_temp1y_temp
for(int i1i<6i++){
y_temp1+1
if(y_temp1>thisheight)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
if(num5)
return true
判断左
x_temp1x_temp
y_temp1y_temp
num1
for(int i1i<6i++){
x_temp11
y_temp11
if(y_temp1<0 || x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
判断右
x_temp1x_temp
y_temp1y_temp
for(int i1i<6i++){
x_temp1+1
y_temp1+1
if(y_temp1>thisheight || x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
if(num5)
return true
判断右
x_temp1x_temp
y_temp1y_temp
num1
for(int i1i<6i++){
x_temp1+1
y_temp11
if(y_temp1<0 || x_temp1>thiswidth)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
判断左
x_temp1x_temp
y_temp1y_temp
for(int i1i<6i++){
x_temp11
y_temp1+1
if(y_temp1>thisheight || x_temp1<0)
break
if(thisarrMapShow[x_temp1][y_temp1]arrvalue)
num++
else
break
}
if(num5)
return true
return false
}
赢棋提示
public void showSuccess(JPanel jp){
JOptionPaneshowMessageDialog(jp赢厉害win
JOptionPaneINFORMATION_MESSAGE)
}
输棋提示
public void showDefeat(JPanel jp){
JOptionPaneshowMessageDialog(jp输请重新开始lost
JOptionPaneINFORMATION_MESSAGE)
}
}
*
*类MainPanel完成功:
*1构建面板该面板画棋盘
*2处理该棋盘鼠标事件(鼠标左键点击鼠标右键点击鼠标拖动等)
**
class MainPanel extends JPanel
implements MouseListenerMouseMotionListener{
private int widthheight棋盘宽度高度
private ChessModel cm
根棋盘模式设定面板
MainPanel(ChessModel mm){
cmmm
widthcmgetWidth()
heightcmgetHeight()
addMouseListener(this)
}
根棋盘模式设定棋盘宽度高度
public void setModel(ChessModel mm){
cm mm
width cmgetWidth()
height cmgetHeight()
}
根坐标计算出棋盘方格棋子信息(白子黑子)
然调draw方法棋盘画出相应棋子
public void paintComponent(Graphics g){
superpaintComponent(g)
for(int j 0 j < height j++){
for(int i 0 i < width i++){
int v cmgetarrMapShow()[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(iwidth && jheight){
gsetColor(Colorwhite)
gdrawRect(xy2020)
}
画黑色棋子
if(v 1 ){
gsetColor(Colorgray)
gdrawOval(x8y81616)
gsetColor(Colorblack)
gfillOval(x8y81616)
}
画白色棋子
if(v 2 ){
gsetColor(Colorgray)
gdrawOval(x8y81616)
gsetColor(Colorwhite)
gfillOval(x8y81616)
}
if(v 3){
gsetColor(Colorcyan)
gdrawOval(x8y81616)
}
}
响应鼠标点击事件根鼠标点击棋
根棋判断胜负等
public void mousePressed(MouseEvent evt){
int x (evtgetX()10) 20
int y (evtgetY()10) 20
Systemoutprintln(x+ +y)
if (evtgetModifiers()MouseEventBUTTON1_MASK){
cmplay(xy)
Systemoutprintln(cmgetisOdd()+ +cmgetarrMapShow()[x][y])
repaint()
if(cmjudgeSuccess(xycmgetisOdd())){
cmshowSuccess(this)
evtconsume()
ChessFrameiscomputerfalse
}
判断否机弈
if(ChessFrameiscomputer&&cmgetisExist()){
cmcomputerDo(cmgetWidth()cmgetHeight())
repaint()
if(cmjudgeSuccess(cmgetX()cmgetY()cmgetisOdd())){
cmshowDefeat(this)
evtconsume()
}
}
}
}
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 (moveevtgetX()10) 20
int y (moveevtgetY()10) 20
cmreadyplay(xy)
repaint()
}
}
class ChessWindowEvent extends WindowAdapter{
public void windowClosing(WindowEvent e){
Systemexit(0)
}
ChessWindowEvent()
{
}
}
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档