第一范文网 - 专业文章范例文档资料分享平台

java五子棋代码实习报告

来源:用户分享 时间:2025/9/5 14:49:47 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

else

UIManager.setLookAndFeel(

\ SwingUtilities.updateComponentTreeUI(this); }catch(Exception ee){} if(arg.equals(\ this.width=20; this.height=15;

cm=new ChessModel(1);

MapSize(this.width,this.height);

SwingUtilities.updateComponentTreeUI(this); }

if(arg.equals(\ this.width=30; this.height=20;

cm=new ChessModel(2);

MapSize(this.width,this.height);

SwingUtilities.updateComponentTreeUI(this); }

if(arg.equals(\ this.width=40; this.height=30;

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(\关于\

第 8 页 共 21 页

JOptionPane.showMessageDialog(this, \五子棋游戏\关于\ if(arg.equals(\退出\ System.exit(0); } }

class ChessModel {

private int width,height,modeChess; private int x=0,y=0;

private int[][] arrMapShow; private boolean isOdd,isExist; public ChessModel() {}

public ChessModel(int modeChess){ this.isOdd=true; 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){ //按照棋盘模式构建棋盘大小 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] = -5; } }}

public boolean getisOdd(){//获取是否交换棋手的标识符 return this.isOdd; }

public void setisOdd(boolean isodd){ //设置交换棋手的标识符

if(isodd)

this.isOdd=true; else

this.isOdd=false; }

public boolean getisExist(){//获取某棋盘方格是否有棋子的标识值 return this.isExist;

第 9 页 共 21 页

}

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; }else

this.isExist=false; if(getisOdd()){ setisOdd(false);

this.arrMapShow[x][y]=1; }else{

setisOdd(true);

第 10 页 共 21 页

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; // right

for(int i=1;i<5;i++){ x_temp1+=1;

if(x_temp1>this.width) break;

第 11 页 共 21 页

搜索更多关于: java五子棋代码实习报告 的文档
java五子棋代码实习报告.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c3bx4392eif79c964hv4m_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top