简单黑白棋.docx

上传人:b****4 文档编号:6667762 上传时间:2023-05-10 格式:DOCX 页数:21 大小:17.64KB
下载 相关 举报
简单黑白棋.docx_第1页
第1页 / 共21页
简单黑白棋.docx_第2页
第2页 / 共21页
简单黑白棋.docx_第3页
第3页 / 共21页
简单黑白棋.docx_第4页
第4页 / 共21页
简单黑白棋.docx_第5页
第5页 / 共21页
简单黑白棋.docx_第6页
第6页 / 共21页
简单黑白棋.docx_第7页
第7页 / 共21页
简单黑白棋.docx_第8页
第8页 / 共21页
简单黑白棋.docx_第9页
第9页 / 共21页
简单黑白棋.docx_第10页
第10页 / 共21页
简单黑白棋.docx_第11页
第11页 / 共21页
简单黑白棋.docx_第12页
第12页 / 共21页
简单黑白棋.docx_第13页
第13页 / 共21页
简单黑白棋.docx_第14页
第14页 / 共21页
简单黑白棋.docx_第15页
第15页 / 共21页
简单黑白棋.docx_第16页
第16页 / 共21页
简单黑白棋.docx_第17页
第17页 / 共21页
简单黑白棋.docx_第18页
第18页 / 共21页
简单黑白棋.docx_第19页
第19页 / 共21页
简单黑白棋.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

简单黑白棋.docx

《简单黑白棋.docx》由会员分享,可在线阅读,更多相关《简单黑白棋.docx(21页珍藏版)》请在冰点文库上搜索。

简单黑白棋.docx

简单黑白棋

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

@SuppressWarnings("serial")

publicclassFirstextendsJFrame{

publicFirst(){

DrawPanedrawPane=newDrawPane(getBackground());

Containerpane=getContentPane();

pane.add(drawPane,BorderLayout.CENTER);

setTitle("简单黑白棋");

setSize(600,580);

setExtendedState(JFrame.MAXIMIZED_BOTH);

//setUndecorated(true);

//setAlwaysOnTop(true);

//setLocationByPlatform(true);

setLocation(208,104);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBackground(Color.red);

setResizable(false);

setVisible(true);

}

publicstaticvoidmain(String[]args){

newFirst();

}

}

@SuppressWarnings("serial")

classDrawPaneextendsJPanel{

privateInsetsinsets;

finalstaticintBLACK=-1;

finalstaticintWHITE=1;

privatePointpoint=null;

privateintcolorStatus;

privateint[][]status=null;

privateintcount;

privateintchessNum;

privateJTextAreainfoText=null;

privateint[][]statusW=null;

privateintlevel=1;

privateintwhiteCount=0;

privateintblackCount=0;

privateColorbgColor=null;

publicDrawPane(Colorc){

initComponent();

initChess();

bgColor=c;

addMouseListener(newMouseAdapter(){

publicvoidmouseClicked(MouseEvente){

response(e.getPoint());

}

});

setFocusable(true);

addKeyListener(newmyKeyListener());

}

publicvoidpaintComponent(Graphicsg){

super.paintComponent(g);

if(insets==null){

insets=getInsets();

}

//super.paintComponents(g);

Graphics2Dg2d=(Graphics2D)g;

g2d.setColor(Color.green);

g2d.fill3DRect(50,50,400,400,true);

g2d.setColor(Color.RED);

for(inti=0;i<8;i++){

for(intj=0;j<8;j++){

g2d.drawString(i+"",50*i+75,45);

g2d.drawString(j+"",40,50*j+75);

g2d.drawRect(50+50*i,50+50*j,50,50);

if(status[i][j]==BLACK){

g2d.setColor(Color.BLACK);

g2d.fillOval(i*50+53,j*50+53,44,44);

}

if(status[i][j]==WHITE){

g2d.setColor(Color.WHITE);

g2d.fillOval(i*50+53,j*50+53,44,44);

}

if(status[i][j]>1){

//g2d.setColor(Color.BLUE);

//g2d.drawString(""+(status[i][j]-1),i*50+75,

//j*50+75);

g2d.drawString("X",i*50+75,j*50+75);

}

g2d.setColor(Color.BLACK);

g2d.fillOval(200,470,30,30);

g2d.setColor(Color.WHITE);

g2d.fillOval(300,470,30,30);

g2d.setColor(bgColor);

g2d.fillRect(240,470,60,30);

g2d.fillRect(340,470,60,30);

g2d.setColor(Color.RED);

g2d.drawString(""+blackCount,240,490);

g2d.drawString(""+whiteCount,340,490);

}

}

//jug(colorStatus);

}

publicvoidinitComponent(){

infoText=newJTextArea();

setLayout(newBorderLayout());

infoText.setEditable(false);

//infoText.setBackground(Color.PINK);

infoText.setForeground(Color.BLACK);

JScrollPanejsp=newJScrollPane(infoText);

JPanelinfoPanel=newJPanel(newBorderLayout());

//infoPanel.setBackground(Color.PINK);

infoPanel.add(jsp,BorderLayout.CENTER);

infoPanel.add(newJLabel(""),BorderLayout.NORTH);

infoPanel.add(newJLabel(""),BorderLayout.SOUTH);

add(infoPanel,BorderLayout.EAST);

}

publicvoidinitChess(){

chessNum=4;

count=0;

colorStatus=BLACK;

status=newint[8][8];

status[3][3]=BLACK;

status[4][4]=BLACK;

status[3][4]=WHITE;

status[4][3]=WHITE;

statusW=newint[8][8];

for(inti=0;i<8;i++){

for(intj=0;j<8;j++){

statusW[i][j]=2;

}

}

for(inti=0;i<4;i++){

statusW[i+2][0]=3;

statusW[i+2][1]=1;

statusW[i+2][7]=3;

statusW[i+2][6]=1;

statusW[0][i+2]=3;

statusW[1][i+2]=1;

statusW[7][i+2]=3;

statusW[6][i+2]=1;

}

for(inti=0;i<2;i++){

for(intj=0;j<2;j++){

statusW[i][j]=0;

statusW[7-i][j]=0;

statusW[i][7-j]=0;

statusW[7-i][7-j]=0;

}

}

statusW[0][0]=4;

statusW[0][7]=4;

statusW[7][0]=4;

statusW[7][7]=4;

jug(colorStatus);

Stringmsg="下棋的不骤:

";

infoText.setText(msg+"\n");

repaint();

}

publicvoidresponse(Pointp){

booleaninterrupt=false;

point=setPoint(p);

if(point!

=null){

intx=point.x/50-1;

inty=point.y/50-1;

if(check(x,y,colorStatus)>0){

manulChess(x,y,BLACK);

if(level==1){

autoChess(WHITE);

}else{

autoChess(WHITE,2);

}

if(blPass()){

if(!

blPass()){

repaint();

}else{

interrupt=true;

}

}

if(blOver(interrupt)){

initChess();

}

}

}

}

publicPointsetPoint(Pointp){

if(p.x<50||p.x>450||p.y<50||p.y>450){

returnnull;

}else{

p.x=(p.x/50)*50;

p.y=(p.y/50)*50;

returnp;

}

}

publicbooleanchange(inti,intj,intsta){

booleanflag=false;

//north

if(j!

=0){

if(sta!

=status[i][j-1]&&1==Math.abs(status[i][j-1])){

intm=j-1;

while(status[i][j-1]==status[i][m]&&m>0){

m--;

//System.out.println(m);

}

if(status[i][m]==sta){

flag=true;

for(intn=j-1;n>m;n--){

status[i][n]=sta;

//System.out.println("["+i+","+n+"]"

//+status[i][n]);

}

}

}

}

//south

if(j!

=7){

if(sta!

=status[i][j+1]&&1==Math.abs(status[i][j+1])){

intm=j+1;

while(status[i][j+1]==status[i][m]&&m<7){

m++;

//System.out.println(m);

}

if(status[i][m]==sta){

flag=true;

for(intn=j+1;n

status[i][n]=sta;

//System.out.println("["+i+","+n+"]"

//+status[i][n]);

}

}

}

}

//west

if(i!

=0){

if(sta!

=status[i-1][j]&&1==Math.abs(status[i-1][j])){

intm=i-1;

while(status[i-1][j]==status[m][j]&&m>0){

m--;

//System.out.println(m);

}

if(status[m][j]==sta){

flag=true;

for(intn=i-1;n>m;n--){

status[n][j]=sta;

//System.out.println("["+n+","+j+"]"

//+status[n][j]);

}

}

}

}

//east

if(i!

=7){

if(sta!

=status[i+1][j]&&1==Math.abs(status[i+1][j])){

intm=i+1;

while(status[i+1][j]==status[m][j]&&m<7){

m++;

//System.out.println(m);

}

if(status[m][j]==sta){

flag=true;

for(intn=i+1;n

status[n][j]=sta;

//System.out.println("["+n+","+j+"]"

//+status[n][j]);

}

}

}

}

//northwest

if(j!

=0&&i!

=0){

if(sta!

=status[i-1][j-1]

&&1==Math.abs(status[i-1][j-1])){

intm=i-1;

intn=j-1;

while(status[i-1][j-1]==status[m][n]&&m>0&&n>0){

m--;

n--;

//System.out.println("m="+m+"n="+n);

}

if(status[m][n]==sta){

flag=true;

for(intx=i-1,y=j-1;x>m;x--,y--){

//for(inty=j-1;y>n;y--){

status[x][y]=sta;

//System.out.println("["+x+","+y+"]"

//+status[x][y]);

//}

}

}

}

}

//southeast

if(j!

=7&&i!

=7){

if(sta!

=status[i+1][j+1]

&&1==Math.abs(status[i+1][j+1])){

intm=i+1;

intn=j+1;

while(status[i+1][j+1]==status[m][n]&&m<7&&n<7){

m++;

n++;

//System.out.println("m="+m+"n="+n);

}

if(status[m][n]==sta){

flag=true;

for(intx=i+1,y=j+1;x

//for(inty=j+1;y

status[x][y]=sta;

//System.out.println("["+x+","+y+"]"

//+status[x][y]);

//}

}

}

}

}

//northeast

if(j!

=0&&i!

=7){

if(sta!

=status[i+1][j-1]

&&1==Math.abs(status[i+1][j-1])){

intm=i+1;

intn=j-1;

while(status[i+1][j-1]==status[m][n]&&m<7&&n>0){

m++;

n--;

//System.out.println("m="+m+"n="+n);

}

if(status[m][n]==sta){

flag=true;

for(intx=i+1,y=j-1;x

//for(inty=j-1;y>n;y--){

status[x][y]=sta;

//System.out.println("["+x+","+y+"]"

//+status[x][y]);

//}

}

}

}

}

//southwest

if(j!

=7&&i!

=0){

if(sta!

=status[i-1][j+1]

&&1==Math.abs(status[i-1][j+1])){

intm=i-1;

intn=j+1;

while(status[i-1][j+1]==status[m][n]&&m>0&&n<7){

m--;

n++;

//System.out.println("m="+m+"n="+n);

}

if(status[m][n]==sta){

flag=true;

for(intx=i-1,y=j+1;x>m;x--,y++){

//for(inty=j+1;y

status[x][y]=sta;

//System.out.println("["+x+","+y+"]"

//+status[x][y]);

//}

}

}

}

}

returnflag;

}

publicintcheck(inti,intj,intsta){

intflag=0;

if(Math.abs(status[i][j])==1){

return0;

}

//north

if(j!

=0){

if(sta!

=status[i][j-1]&&1==Math.abs(status[i][j-1])){

intm=j-1;

while(status[i][j-1]==status[i][m]&&m>0){

m--;

//System.out.println(m);

}

if(status[i][m]==sta){

flag=flag+(j-1-m);

}

}

}

//south

if(j!

=7){

if(sta!

=status[i][j+1]&&1==Math.abs(status[i][j+1])){

intm=j+1;

while(status[i][j+1]==status[i][m]&&m<7){

m++;

//System.out.println(m);

}

if(status[i][m]==sta){

flag=flag+(m-j-1);

}

}

}

//west

if(i!

=0){

if(sta!

=status[i-1][j]&&1==Math.abs(status[i-1][j])){

intm=i-1;

while(status[i-1][j]==status[m][j]&&m>0){

m--;

//System.out.println(m);

}

if(status[m][j]==sta){

flag=flag+(i-1-m);

}

}

}

//east

if(i!

=7){

if(sta!

=status[i+1][j]&&1==Math.abs(status[i+1][j])){

intm=i+1;

while(status[i+1][j]==status[m][j]&&m<7){

m++;

//System.out.println(m);

}

if(status[m][j]==sta){

flag=flag+(m-i-1);

}

}

}

//northwest

if(j!

=0&&i!

=0){

if(sta!

=status[i-1][j-1]

&&1==Math.abs(status[i-1][j-1])){

intm=i-1;

intn=j-1;

while(status[i-1][j-1]==status[m][n]&&m>0&&n>0){

m--;

n--;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2