package chess;
/**
* <p>Title:5chess </p>
* <p>Description: </p>
* <p>Copyright: seaboy.ping@263.net Copyright (c) 2002</p>
* <p>Company: </p>
* @author chenxiaoping
* @version 1.0
*/
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChessModel {
private int width,height,degree;
private int x=0,y=0;
private int[][] arrMap,arrMapShow;
private boolean isOdd,isExist;
private ChessFrame cf;
public ChessModel() {
}
public ChessModel(int degree)
{
this.isOdd=true;
//this.isExist=false;
if(degree == 1)
{
PanelInit(20, 15, degree);
}
if(degree == 2)
{
PanelInit(30, 20, degree);
}
if(degree == 3)
{
PanelInit(40, 30, degree);
}
}
private void PanelInit(int width, int height, int degree)
{
this.width = width;
this.height = height;
this.degree = degree;
//arrMap = new int[width+1][height+1];
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;
//arrMap[i][j] = 0;
}
}
}
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;
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public int getDegree(){
return this.degree;
}
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 flash(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y))
return;
this.arrMapShow[x][y]=-1;
}
*/
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);
this.arrMapShow[x][y]=2;
}
}
//计算机走棋
/*
说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后
得出棋子数最大值的坐标,下子
*/
public void computerdo(int width,int height){
int max_black,max_red,max_temp,max=0;
//int x=0,y=0;
setisOdd(true);
System.out.println("computer is doing ...");
for(int i = 0; i <= width; i++)
{
for(int j = 0; j <= height; j++)
{
if(!chessExist(i,j))
{ //算法判断是否下子
max_red=checkMax(i,j,2);//判断红子的最大值
max_black=checkMax(i,j,1);//判断黑子的最大值
//max_temp=max_red>=max_black?max_red:max_black;
max_temp=Math.max(max_red,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_red){
int num=0,max_num,max_temp=0;
int x_temp=x,y_temp=y;
int x_temp1=x_temp,y_temp1=y_temp;
//judge right
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_red)
num++;
else
break;
}
//judge left
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_red)
num++;
else
break;
}
if(num<5)
max_temp=num;
//judge up
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_red)
num++;
else
break;
}
//judge down
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_red)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
//judge left_up
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_red)
num++;
else
break;
}
//judge right_down
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_red)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
//judge right_up
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_red)
num++;
else
break;
}
//judge left_down
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<0)
break;
if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)
num++;
else
break;
}
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;
else
arrvalue=1;
int x_temp1=x_temp,y_temp1=y_temp;
//judge right
for(int i=1;i<6;i++){
x_temp1+=1;
if(x_temp1>this.width)
break;
if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
wuziqi.rar_java 五子棋_wuziqi java_五子棋 java_五子棋 人机 java_五子棋 棋盘
版权申诉
46 浏览量
2022-09-19
13:45:28
上传
评论
收藏 18KB RAR 举报
alvarocfc
- 粉丝: 105
- 资源: 1万+
最新资源
- 四数之和(java代码).docx
- 701837906919458TapScanner v3.0.10 (Pro).apk
- 青岛大学人工智能实验二 利用α-β搜索的博弈树算法编写一字棋游戏
- ### 1、项目介绍 本项目Scrapy进行数据爬取,并使用Django框架+PyEcharts实现可视化大屏 效果如下:
- # 微信小程序-健康菜谱 基于微信小程序的一个查找检索菜谱的应用 ### 效果 !动态图(./res/gif/demo
- zabbix-get命令包资源
- 289ssm-mysql-jsp 计算机课程实验管理系统.zip(可运行源码+数据库文件+文档)
- 毕业设计,基于PyQt5实现的可视化界面的Python车牌自动识别系统源码
- 20-天天果园项目.rar
- 26-朴素贝叶斯分类.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈