java實(shí)現(xiàn)五子棋大戰(zhàn)
本文實(shí)例為大家分享了java實(shí)現(xiàn)五子棋大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
這是我接近一年前的項(xiàng)目了,以前沒有養(yǎng)成寫博客的習(xí)慣,打算陸續(xù)把以前做過的項(xiàng)目補(bǔ)上來。
一、介紹
主要實(shí)現(xiàn)的功能有棋子顏色選擇,悔棋,重新開始,玩家對(duì)戰(zhàn)和人機(jī)對(duì)戰(zhàn),效果圖如圖所是:
模式選擇:

棋子選擇:

人機(jī)對(duì)戰(zhàn):

玩家對(duì)戰(zhàn):

二、具體實(shí)現(xiàn)
五子棋的開發(fā)首先需要在界面上繪制一個(gè)表格,因?yàn)槠甙嗍遣蛔兊?,棋子大小是不變的,所以我們首先可以自定義一個(gè)接口來設(shè)置項(xiàng)目中的常量,這樣改變這些參數(shù)時(shí)也比較方便,CS.java代碼如下:
public interface CS {
?? ?public static final int x0=60;//棋盤開始位置
?? ?public static final int y0=70;
?? ?public static final int line=15;//棋盤有多少條線
?? ?public static final int size=40;//棋子大小
?? ?
}和上一篇博客中的畫圖板類似,首先需要一個(gè)界面,這里可以定義一個(gè)Chess類繼承Jframe,然后再重寫paint(Graphics g)方法,來繪制棋盤,Chess.java代碼如下:
Chess.java:
public class Chess extends JFrame implements CS{
?? ?int i=2;
?? ?private qizi qizilarry2[][]=new qizi[line][line];//用qizi類型的二維數(shù)組來存儲(chǔ)棋子
?? ?private JButton b = new JButton("悔棋");
?? ?private JButton b2=new JButton("重新開始");
?? ?private JLabel jLabel=new JLabel("請(qǐng)選擇對(duì)戰(zhàn)模式");
?? ?private JLabel jLabel2=new JLabel("請(qǐng)選擇棋子顏色");
?? ?private int value;//提示框選項(xiàng)的值
?? ?private int value2;
?? ?Dimension dimension=new Dimension(100, 30);
?? ?String color[]= {"白棋","黑棋"};?
?? ?String moshi[]= {"玩家對(duì)戰(zhàn)","人機(jī)對(duì)戰(zhàn)"};?
?? ?public void chessUI() {
?? ??? ?b.setPreferredSize(dimension);
?? ??? ?b2.setPreferredSize(dimension);
?? ??? ?this.setSize(700, 700);
?? ??? ?this.setLayout(new FlowLayout());
?? ??? ?this.setTitle("五子棋");
?? ??? ?this.setLocationRelativeTo(null);
?? ??? ?this.setDefaultCloseOperation(3);
?? ??? ?this.setVisible(true);
?? ??? ?this.add(b);
?? ??? ?this.add(b2);
?? ??? ?value=JOptionPane.showOptionDialog(this, jLabel, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,?
?? ??? ??? ??? ?null, moshi, null);
?? ??? ?value2=JOptionPane.showOptionDialog(this, jLabel2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,?
?? ??? ??? ??? ?null, color, null);
? ? ? ? //關(guān)閉提示框則退出程序?? ??? ?
?? ??? ?if(value==JOptionPane.CLOSED_OPTION||value2==JOptionPane.CLOSED_OPTION) {
?? ??? ??? ?System.exit(1);
?? ??? ?}
?? ??? ?
?? ??? ?Graphics g=this.getGraphics();?
?? ??? ?mouslistener mouslistener =new mouslistener(g,qizilarry2,this,value,value2);
?? ??? ?this.addMouseListener(mouslistener);//窗口添加監(jiān)聽
?? ??? ?b.addActionListener(mouslistener);//按鈕添加監(jiān)聽
?? ??? ?b2.addActionListener(mouslistener);
?? ??? ?
?? ?}
?? ?
?? ?public void paint(Graphics g) {//重繪棋盤和棋子
?? ??? ?super.paint(g);
?? ??? ?drawChess(g);
?? ??? ?drawQZ();
?? ??? ?
?? ?}
?? ?//畫一個(gè)棋盤
?? ?public void drawChess(Graphics g) {
?? ??? ?g.setColor(Color.black);
?? ??? ?for(int i=0;i<line;i++) {
?? ??? ??? ?g.drawLine(x0, y0+size*i, x0+(line-1)*size, y0+size*i);
?? ??? ?}
?? ??? ?for(int j=0;j<line;j++) {
?? ??? ??? ?g.drawLine(x0+size*j, y0, x0+size*j, y0+(line-1)*size);
?? ??? ?}
?? ?}
?? ?//重構(gòu)畫棋子
?? ?public void drawQZ() {
?? ??? ?for(int ix=0;ix<line;ix++) {
?? ??? ??? ?for(int iy=0;iy<line;iy++) {
?? ??? ??? ??? ?if(qizilarry2[ix][iy]!=null) {
?? ??? ??? ??? ??? ?qizi qizi21=qizilarry2[ix][iy];
?? ??? ??? ??? ??? ?qizi21.drawq();
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?public static void main(String[] args) {
?? ??? ?new Chess().chessUI();
?? ?}
}用qizi類型的二維數(shù)組來存儲(chǔ)棋子,在重繪時(shí)重繪整個(gè)棋盤和二維數(shù)組上的棋子,如果二維數(shù)組為null則不用重繪。
接下來該創(chuàng)建監(jiān)聽類了,在鼠標(biāo)點(diǎn)擊棋盤時(shí),要使得棋子在棋盤的正中央,代碼如下:
//x軸坐標(biāo)?? ?
for(ix=0;x1>0;ix++) {
?? ?x1-=size;
}
x1+=size;
x1-=size/2;
ix--;
if(x1<=0) {
?? ?x=x0+ix*size;
}else
?? ?x=x0+(++ix)*size;
//y軸坐標(biāo)
for(iy=0;y1>0;iy++) {
?? ?y1-=size;
}
y1+=size;
y1-=size/2;
iy--;
if(y1<=0) {
?? ?y=y0+iy*size;
}else
?? ?y=y0+(++iy)*size;判贏的方法非常簡(jiǎn)單,只要計(jì)算棋子在它八個(gè)方向的相鄰的且顏色相同的棋子個(gè)數(shù)即可,這里只展現(xiàn)向左查找棋子的代碼(后續(xù)會(huì)附上整個(gè)監(jiān)聽類的代碼):
public int zuo(int x,int y,Color c) {//向左找
?? ??? ?int a=x;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a--;
?? ??? ??? ?if(a<0||qizilarry[a][y]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][y].getColor()==c)
?? ??? ??? ??? ?count1++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return count1;
?? ?}當(dāng)模式為玩家和玩家模式時(shí),需要每下一個(gè)棋子顏色改變,實(shí)現(xiàn)的代碼如下:
if(a) {
?? ??? ??? ??color =c;
?? ??? ??? ??? ??? ?a=false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?color=c2;
?? ??? ??? ??? ??? ?a=true;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?g.setColor(color);
?? ??? ??? ??? ?g.fillOval(x-size/2, y-size/2, size, size);
?? ??? ??? ??? ?prex=ix;
?? ??? ??? ??? ?prey=iy;
?? ??? ??? ??? ?qizi qizi=new qizi(g, color,ix,iy);
?? ??? ??? ??? ?qizilarry[ix][iy]=qizi;
?? ??? ??? ??? ?inte++;玩家VS玩家和玩家VS電腦的判贏方法基本類似,這里只展現(xiàn)了玩家與玩家的判贏方法,即每下一個(gè)新的棋子,就計(jì)算這個(gè)棋子八個(gè)方向上相同且相鄰棋子的個(gè)數(shù),當(dāng)同一直線兩個(gè)方向的棋子個(gè)數(shù)之和為5時(shí),則獲取棋子顏色,判定為獲勝,具體代碼實(shí)現(xiàn)如下:
//判斷輸贏
if(zuo(ix,iy,color)+you(ix,iy,color)>=4||shang(ix,iy,color)+xia(ix,iy,color)>=4
?? ??? ??? ??? ??? ??? ?||zuoshang(ix, iy,color)+youxia(ix, iy,color)>=4||zuoxia(ix, iy,color)+youshang(ix, iy,color)>=4) {
?? ??? ??? ??? ??? ?JLabel jLabel =new JLabel("白棋獲勝!");
?? ??? ??? ??? ??? ?JLabel jlabel2 =new JLabel("黑棋獲勝!");
?? ??? ??? ??? ??? ?if(color==Color.white)
?? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jlabel2, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?count1=0;//如果沒有贏重新置0重新計(jì)算
?? ??? ??? ??? ??? ?count2=0;
?? ??? ??? ??? ??? ?countS=0;
?? ??? ??? ??? ??? ?countX=0;
?? ??? ??? ??? ??? ?countZS=0;
?? ??? ??? ??? ??? ?countZX=0;
?? ??? ??? ??? ??? ?countYS=0;
?? ??? ??? ??? ??? ?countYX=0;
?? ??? ??? ??? ?}
這樣玩家與玩家模式的大體功能就基本實(shí)現(xiàn)了,接下類就要實(shí)現(xiàn)五子棋的AI功能了
五子棋AI
這里我們主要針權(quán)值法討論下,大致思路如下:
1.我們繪制好一個(gè)棋盤后,大小為 15*15;
2.下棋之前,對(duì)于棋盤中的每個(gè)空位,我們每都替電腦人“掂一掂”下在哪里合算;(估權(quán)過程)
3.對(duì)每個(gè)空位按照規(guī)則都計(jì)算完權(quán)重,我們找出權(quán)重最大的位置,此位置就是npc落子位置。
空子位置我們用 “0” 表示,白子用“2”表示,黑子用“1”表示;
我們主要分為以下幾種情況:
定義 棋子相連情況 權(quán)值
活一連 010、020 40
活二連 0110、0220 400
活三連 01110、02220 3000
活四連 011110、022220 10000
眠一連 012、021 20
眠二連 0112、0221 200
眠三連 01112、02221 500
眠四連 011112、022221 3000
用hash表存儲(chǔ)所有可能的情況并賦予一定的權(quán)值,每下一個(gè)棋子便更新棋盤上所有空位置的權(quán)值,電腦再尋找棋盤上權(quán)值最大的點(diǎn)下棋,computerChess()函數(shù)代碼如下:
public void computerChess() {
?? ??? ?hashMap.put("10000", 15);//眠1連
?? ??? ?hashMap.put("20000", 10);//眠1連
?? ??? ?
?? ??? ?hashMap.put("20100",17);//眠1連,15
?? ??? ?hashMap.put("10200",12);//眠1連,10
?? ??? ?hashMap.put("21000",15);//眠1連,15
?? ??? ?hashMap.put("12000",10);//眠1連,10
?? ??? ?hashMap.put("20010",19);//眠1連,15
?? ??? ?hashMap.put("10020",14);//眠1連,10
?? ??? ?hashMap.put("20100",17);//眠1連,15
?? ??? ?hashMap.put("10200",12);//眠1連,10
//?
//?? ??? ?hashMap.put("00010",21);//活1連,15
//?? ??? ?hashMap.put("00020",16);//活1連,10
//?? ??? ?hashMap.put("00100",19);//活1連,15
//?? ??? ?hashMap.put("00200",14);//活1連,10
//?? ??? ?hashMap.put("01000",17);//活1連,15
//?? ??? ?hashMap.put("02000",12);//活1連,10
//?
?? ??? ?//被堵住
?? ??? ?hashMap.put("10100",65);//眠2連,40
?? ??? ?hashMap.put("20200",60);//眠2連,30
?? ??? ?hashMap.put("01100",65);//眠2連,40
?? ??? ?hashMap.put("02200",60);//眠2連,30
?? ??? ?hashMap.put("11000",65);//眠2連,40
?? ??? ?hashMap.put("22000",60);//眠2連,30
?? ??? ?
?? ??? ?hashMap.put("21010",65);//眠2連,40
?? ??? ?hashMap.put("12020",60);//眠2連,30
?? ??? ?hashMap.put("20110",65);//眠2連,40
?? ??? ?hashMap.put("10220",60);//眠2連,30
?? ??? ?hashMap.put("21100",65);//眠2連,40
?? ??? ?hashMap.put("12200",60);//眠2連,30
?
//?? ??? ?hashMap.put("01010",75);//活2連,40
//?? ??? ?hashMap.put("02020",70);//活2連,30
//?? ??? ?hashMap.put("00110",75);//活2連,40
//?? ??? ?hashMap.put("00220",70);//活2連,30
//?? ??? ?hashMap.put("01100",75);//活2連,40
//?? ??? ?hashMap.put("02200",70);//活2連,30
//?? ??? ?hashMap.put("11000",75);//活2連,40
//?? ??? ?hashMap.put("00022",70);//活2連,30
//?? ??? ?
//?? ??? ?//被堵住
?? ??? ?hashMap.put("11100",150);//眠3連,100
?? ??? ?hashMap.put("22200",140);//眠3連,80
?? ??? ?hashMap.put("21110",150);//眠3連,100
?? ??? ?hashMap.put("12220",140);//眠3連,80
//?? ??? ?
//?? ??? ?hashMap.put("10110",1000);//活3連,130
//?? ??? ?hashMap.put("20220",800);//活3連,110
//?? ??? ?hashMap.put("11010",1000);//活3連,130
//?? ??? ?hashMap.put("22020",800);//活3連,110
//?? ??? ?hashMap.put("01110", 1000);//活3連
//?? ??? ?hashMap.put("02220", 800);//活3連
?? ??? ?
?? ??? ?hashMap.put("11110",3000);//4連,300
?? ??? ?hashMap.put("11112",3000);//4連,300
?? ??? ?hashMap.put("22220",3500);//4連,280
?? ??? ?hashMap.put("22221",3500);//4連,280
?? ??? ?int a;
?? ??? ?int b;
?? ??? ?for(int y=0;y<line;y++) {
?? ??? ??? ?for(int x=0;x<line;x++) {
?? ??? ??? ??? ?if(qizilarry[x][y]==null) {
?? ??? ??? ??? ??? ?//向左
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?if(a<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][y]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][y].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuo+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuo+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuo+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?Integer integer=hashMap.get(zuo);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?//向右
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?if(a==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][y]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][y].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?you+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?you+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?you+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(you);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向上
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[x][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[x][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?shang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?shang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?shang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(shang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向下
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[x][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[x][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?xia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?xia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?xia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(xia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向左上
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(a<0||b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuoshang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuoshang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuoshang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(zuoshang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向右下
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(a==line||b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?youxia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?youxia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?youxia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(youxia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向左下
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(a<0||b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuoxia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuoxia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuoxia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(zuoxia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向右上
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(a==line||b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?youshang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?youshang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?youshang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(youshang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?zuo="";
?? ??? ??? ??? ??? ?you="";
?? ??? ??? ??? ??? ?shang="";
?? ??? ??? ??? ??? ?xia="";
?? ??? ??? ??? ??? ?zuoshang="";
?? ??? ??? ??? ??? ?zuoxia="";
?? ??? ??? ??? ??? ?youshang="";
?? ??? ??? ??? ??? ?youxia="";
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}完整代碼
mouslistener.java:
public class mouslistener extends MouseAdapter implements CS,ActionListener{
?? ?private int x;//棋子像素坐標(biāo)
?? ?private int y;
?? ?private int ix;//用戶棋子坐標(biāo)
?? ?private int iy;
?? ?private int cx;//電腦棋子坐標(biāo)
?? ?private int cy;
?? ?private int n=1;//下黑棋或白棋
?? ?public int inte=0;//棋子數(shù)目
?? ?private int prex;//上一個(gè)棋子的坐標(biāo)
?? ?private int prey;
?? ?private int count1=0;
?? ?private int count2=0;
?? ?private int countS=0;
?? ?private int countX=0;
?? ?private int countZS=0;
?? ?private int countYX=0;
?? ?private int countZX=0;
?? ?private int countYS=0;
?? ?private int value;
?? ?private int value2;
?? ?String zuo="";
?? ?String you="";
?? ?String shang="";
?? ?String xia="";
?? ?String zuoshang="";
?? ?String zuoxia="";
?? ?String youshang="";
?? ?String youxia="";
?? ?private boolean a=true;//玩家對(duì)戰(zhàn)下棋順序
?? ?private boolean b=true;//是否在同一個(gè)位置
//?? ?private boolean end=true;//是否電腦勝利
?? ?private Color c;//用戶棋子顏色
?? ?private Color c2;//電腦棋子顏色
?? ?private Color color;//玩家對(duì)戰(zhàn)顏色交替下棋
?? ?public qizi qizilarry[][]=new qizi[line][line];
?? ?private int chessValue[][]=new int[line][line];//權(quán)值表
?? ?private JLabel j=new JLabel("請(qǐng)選擇對(duì)戰(zhàn)模式");
?? ?private JLabel j2=new JLabel("請(qǐng)選擇棋子顏色");
?? ?String moshi[]= {"玩家對(duì)戰(zhàn)","人機(jī)對(duì)戰(zhàn)"};?
?? ?String co[]= {"白棋","黑棋"};?
?? ?JFrame jFrame;//悔棋重繪傳參?
?? ?Graphics g;
?? ?HashMap<String, Integer> hashMap=new HashMap<String, Integer>();
?? ?
?? ?
?? ?public mouslistener(Graphics g,qizi qizi[][],JFrame jFrame,int value,int value2) {
?? ??? ?this.g=g;
?? ??? ?qizilarry=qizi;
?? ??? ?this.jFrame=jFrame;
?? ??? ?this.value=value;
?? ??? ?this.value2=value2;
?? ??? ?if(value2==1) {
?? ??? ??? ?c=Color.black;
?? ??? ??? ?c2=Color.white;
?? ??? ?}else {
?? ??? ??? ?c=Color.white;
?? ??? ??? ?c2=Color.black;
?? ??? ?}
?? ??? ??? ?
?? ?}
?? ?
?? ?
?? ?
?? ?public void mouseClicked(MouseEvent e) {
?? ??? ?x=e.getX();
?? ??? ?y=e.getY();
?? ??? ?int x1=x-x0;
?? ??? ?int y1=y-y0;
?? ? ? ?//x軸坐標(biāo)?? ?
?? ??? ?for(ix=0;x1>0;ix++) {
?? ??? ??? ?x1-=size;
?? ??? ?}
?? ??? ?x1+=size;
?? ??? ?x1-=size/2;
?? ??? ?ix--;
?? ??? ?if(x1<=0) {
?? ??? ??? ?x=x0+ix*size;
?? ??? ?}else
?? ??? ??? ?x=x0+(++ix)*size;
?? ? ? ?//y軸坐標(biāo)
?? ??? ?for(iy=0;y1>0;iy++) {
?? ??? ??? ?y1-=size;
?? ??? ?}
?? ??? ?y1+=size;
?? ??? ?y1-=size/2;
?? ??? ?iy--;
?? ??? ?if(y1<=0) {
?? ??? ??? ?y=y0+iy*size;
?? ??? ?}else
?? ??? ??? ?y=y0+(++iy)*size;
?? ??? ?//判斷是否在同一個(gè)地方?? ?
?? ??? ?b=true;
?? ??? ?if(qizilarry[ix][iy]!=null) {
?? ??? ??? ?JLabel jLabel =new JLabel("不能下在同一個(gè)地方!");
?? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "警告", JOptionPane.WARNING_MESSAGE);
?? ??? ??? ?b=false;
?? ??? ?}
?? ??? ?//--------------------
?? ??? ?//畫圓及重構(gòu),一個(gè)黑棋一個(gè)白棋
?? ??? ?//--------------------
?? ??? ?if(b) {
?? ??? ??? ?if(value==1) {
?? ??? ??? ??? ?g.setColor(c);
?? ??? ??? ??? ?g.fillOval(x-size/2, y-size/2, size, size);
?? ??? ??? ??? ?prex=ix;
?? ??? ??? ??? ?prey=iy;
?? ??? ??? ??? ?qizi qizi=new qizi(g, c,ix,iy);
?? ??? ??? ??? ?qizilarry[ix][iy]=qizi;
?? ??? ??? ??? ?inte++;
?? ??? ??? ??? ?System.out.println("用戶下棋");
?? ??? ??? ??? ?
?? ??? ??? ??? ?computerChess();
?? ??? ??? ??? ?int qzmax=0;
?? ??? ??? ??? ?for(int b=0;b<line;b++) {
?? ??? ??? ??? ??? ?for(int a =0;a<line;a++) {
?? ??? ??? ??? ??? ??? ?if(chessValue[a][b]>qzmax) {
?? ??? ??? ??? ??? ??? ??? ?qzmax=chessValue[a][b];
?? ??? ??? ??? ??? ??? ??? ?cx=a;
?? ??? ??? ??? ??? ??? ??? ?cy=b;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?g.setColor(c2);
?? ??? ??? ??? ?g.fillOval(x0+cx*size-size/2, y0+cy*size-size/2, size, size);
?? ??? ??? ??? ?qizi qizi2=new qizi(g, c2,cx,cy);
?? ??? ??? ??? ?qizilarry[cx][cy]=qizi2;
?? ??? ??? ??? ?inte++;
?? ??? ??? ??? ?System.out.println("電腦下棋");
?? ??? ??? ??? ?
?? ??? ??? ??? ?for(int b=0;b<line;b++) {
?? ??? ??? ??? ??? ?for(int a =0;a<line;a++) {
?? ??? ??? ??? ??? ??? ?chessValue[a][b]=0;
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?//判斷輸贏
?? ??? ??? ??? ?if(zuo(ix,iy,c)+you(ix,iy,c)>=4||shang(ix,iy,c)+xia(ix,iy,c)>=4
?? ??? ??? ??? ??? ??? ?||zuoshang(ix, iy,c)+youxia(ix, iy,c)>=4||zuoxia(ix, iy,c)+youshang(ix, iy,c)>=4) {
?? ??? ??? ??? ??? ?JLabel jLabel =new JLabel("玩家獲勝!");
?? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?count1=0;
?? ??? ??? ??? ??? ?count2=0;
?? ??? ??? ??? ??? ?countS=0;
?? ??? ??? ??? ??? ?countX=0;
?? ??? ??? ??? ??? ?countZS=0;
?? ??? ??? ??? ??? ?countZX=0;
?? ??? ??? ??? ??? ?countYS=0;
?? ??? ??? ??? ??? ?countYX=0;
?? ??? ??? ??? ??? ?if((zuo(cx,cy,c2)+you(cx,cy,c2)>=4||shang(cx,cy,c2)+xia(cx,cy,c2)>=4
?? ??? ??? ??? ??? ??? ??? ?||zuoshang(cx, cy,c2)+youxia(cx, cy,c2)>=4||zuoxia(cx, cy,c2)+youshang(cx, cy,c2)>=4) ){
?? ??? ??? ??? ??? ??? ?JLabel jLabel =new JLabel("電腦獲勝!");
?? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ??? ?count1=0;
?? ??? ??? ??? ??? ??? ?count2=0;
?? ??? ??? ??? ??? ??? ?countS=0;
?? ??? ??? ??? ??? ??? ?countX=0;
?? ??? ??? ??? ??? ??? ?countZS=0;
?? ??? ??? ??? ??? ??? ?countZX=0;
?? ??? ??? ??? ??? ??? ?countYS=0;
?? ??? ??? ??? ??? ??? ?countYX=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?if(a) {
?? ??? ??? ??? ??? ?color =c;
?? ??? ??? ??? ??? ?a=false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?color=c2;
?? ??? ??? ??? ??? ?a=true;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?g.setColor(color);
?? ??? ??? ??? ?g.fillOval(x-size/2, y-size/2, size, size);
?? ??? ??? ??? ?prex=ix;
?? ??? ??? ??? ?prey=iy;
?? ??? ??? ??? ?qizi qizi=new qizi(g, color,ix,iy);
?? ??? ??? ??? ?qizilarry[ix][iy]=qizi;
?? ??? ??? ??? ?inte++;
?? ??? ??? ??? ?
?? ??? ??? ??? ?//判斷輸贏
?? ??? ??? ??? ?if(zuo(ix,iy,color)+you(ix,iy,color)>=4||shang(ix,iy,color)+xia(ix,iy,color)>=4
?? ??? ??? ??? ??? ??? ?||zuoshang(ix, iy,color)+youxia(ix, iy,color)>=4||zuoxia(ix, iy,color)+youshang(ix, iy,color)>=4) {
?? ??? ??? ??? ??? ?JLabel jLabel =new JLabel("白棋獲勝!");
?? ??? ??? ??? ??? ?JLabel jlabel2 =new JLabel("黑棋獲勝!");
?? ??? ??? ??? ??? ?if(color==Color.white)
?? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jlabel2, "游戲結(jié)束", JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?count1=0;//如果沒有贏重新置0重新計(jì)算
?? ??? ??? ??? ??? ?count2=0;
?? ??? ??? ??? ??? ?countS=0;
?? ??? ??? ??? ??? ?countX=0;
?? ??? ??? ??? ??? ?countZS=0;
?? ??? ??? ??? ??? ?countZX=0;
?? ??? ??? ??? ??? ?countYS=0;
?? ??? ??? ??? ??? ?countYX=0;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ??? ?
?? ?}
?? ?public int zuo(int x,int y,Color c) {//向左找
?? ??? ?int a=x;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a--;
?? ??? ??? ?if(a<0||qizilarry[a][y]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][y].getColor()==c)
?? ??? ??? ??? ?count1++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return count1;
?? ?}
?? ?
?? ?public int you(int x,int y,Color c){//向右找
?? ??? ?int a =x;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a++;
?? ??? ??? ?if(a==15||qizilarry[a][y]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][y].getColor()==c)
?? ??? ??? ??? ?count2++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return count2;
?? ?}
?? ?
?? ?public int xia(int x,int y,Color c) {//向下找
?? ??? ?int a=y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a++;
?? ??? ??? ?if(a==15||qizilarry[x][a]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[x][a].getColor()==c)
?? ??? ??? ??? ?countX++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countX;
?? ?}
?? ?
?? ?public int shang(int x,int y,Color c){//向上找
?? ??? ?int a =y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a--;
?? ??? ??? ?if(a<0||qizilarry[x][a]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[x][a].getColor()==c)
?? ??? ??? ??? ?countS++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countS;
?? ?}
?? ?
?? ?public int zuoshang(int x,int y,Color c) {//向左上找
?? ??? ?int a=x;
?? ??? ?int b=y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a--;
?? ??? ??? ?b++;
?? ??? ??? ?if(a<0||b==15||qizilarry[a][b]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][b].getColor()==c)
?? ??? ??? ??? ?countZS++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countZS;
?? ?}
?? ?public int youxia(int x,int y,Color c) {//向右下找
?? ??? ?int a=x;
?? ??? ?int b=y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a++;
?? ??? ??? ?b--;
?? ??? ??? ?if(b<0||a==15||qizilarry[a][b]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][b].getColor()==c)
?? ??? ??? ??? ?countYX++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countYX;
?? ?}
?? ?
?? ?public int zuoxia(int x,int y,Color c) {//向左下找
?? ??? ?int a=x;
?? ??? ?int b=y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a--;
?? ??? ??? ?b--;
?? ??? ??? ?if(a<0||b<0||qizilarry[a][b]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][b].getColor()==c)
?? ??? ??? ??? ?countZX++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countZX;
?? ?}
?? ?
?? ?public int youshang(int x,int y,Color c) {//向右上找
?? ??? ?int a=x;
?? ??? ?int b=y;
?? ??? ?for(int i=1;i<5;i++) {
?? ??? ??? ?a++;
?? ??? ??? ?b++;
?? ??? ??? ?if(a==15||b==15||qizilarry[a][b]==null) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}else if(qizilarry[a][b].getColor()==c)
?? ??? ??? ??? ?countYS++;
?? ??? ??? ?else?
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?return countYS;
?? ?}
?? ?
?? ?public void computerChess() {
?? ??? ?hashMap.put("10000", 15);//眠1連
?? ??? ?hashMap.put("20000", 10);//眠1連
?? ??? ?
?? ??? ?hashMap.put("20100",17);//眠1連,15
?? ??? ?hashMap.put("10200",12);//眠1連,10
?? ??? ?hashMap.put("21000",15);//眠1連,15
?? ??? ?hashMap.put("12000",10);//眠1連,10
?? ??? ?hashMap.put("20010",19);//眠1連,15
?? ??? ?hashMap.put("10020",14);//眠1連,10
?? ??? ?hashMap.put("20100",17);//眠1連,15
?? ??? ?hashMap.put("10200",12);//眠1連,10
//?
//?? ??? ?hashMap.put("00010",21);//活1連,15
//?? ??? ?hashMap.put("00020",16);//活1連,10
//?? ??? ?hashMap.put("00100",19);//活1連,15
//?? ??? ?hashMap.put("00200",14);//活1連,10
//?? ??? ?hashMap.put("01000",17);//活1連,15
//?? ??? ?hashMap.put("02000",12);//活1連,10
//?
?? ??? ?//被堵住
?? ??? ?hashMap.put("10100",65);//眠2連,40
?? ??? ?hashMap.put("20200",60);//眠2連,30
?? ??? ?hashMap.put("01100",65);//眠2連,40
?? ??? ?hashMap.put("02200",60);//眠2連,30
?? ??? ?hashMap.put("11000",65);//眠2連,40
?? ??? ?hashMap.put("22000",60);//眠2連,30
?? ??? ?
?? ??? ?hashMap.put("21010",65);//眠2連,40
?? ??? ?hashMap.put("12020",60);//眠2連,30
?? ??? ?hashMap.put("20110",65);//眠2連,40
?? ??? ?hashMap.put("10220",60);//眠2連,30
?? ??? ?hashMap.put("21100",65);//眠2連,40
?? ??? ?hashMap.put("12200",60);//眠2連,30
?
//?? ??? ?hashMap.put("01010",75);//活2連,40
//?? ??? ?hashMap.put("02020",70);//活2連,30
//?? ??? ?hashMap.put("00110",75);//活2連,40
//?? ??? ?hashMap.put("00220",70);//活2連,30
//?? ??? ?hashMap.put("01100",75);//活2連,40
//?? ??? ?hashMap.put("02200",70);//活2連,30
//?? ??? ?hashMap.put("11000",75);//活2連,40
//?? ??? ?hashMap.put("00022",70);//活2連,30
//?? ??? ?
//?? ??? ?//被堵住
?? ??? ?hashMap.put("11100",150);//眠3連,100
?? ??? ?hashMap.put("22200",140);//眠3連,80
?? ??? ?hashMap.put("21110",150);//眠3連,100
?? ??? ?hashMap.put("12220",140);//眠3連,80
//?? ??? ?
//?? ??? ?hashMap.put("10110",1000);//活3連,130
//?? ??? ?hashMap.put("20220",800);//活3連,110
//?? ??? ?hashMap.put("11010",1000);//活3連,130
//?? ??? ?hashMap.put("22020",800);//活3連,110
//?? ??? ?hashMap.put("01110", 1000);//活3連
//?? ??? ?hashMap.put("02220", 800);//活3連
?? ??? ?
?? ??? ?hashMap.put("11110",3000);//4連,300
?? ??? ?hashMap.put("11112",3000);//4連,300
?? ??? ?hashMap.put("22220",3500);//4連,280
?? ??? ?hashMap.put("22221",3500);//4連,280
?? ??? ?int a;
?? ??? ?int b;
?? ??? ?for(int y=0;y<line;y++) {
?? ??? ??? ?for(int x=0;x<line;x++) {
?? ??? ??? ??? ?if(qizilarry[x][y]==null) {
?? ??? ??? ??? ??? ?//向左
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?if(a<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][y]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][y].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuo+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuo+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuo+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?Integer integer=hashMap.get(zuo);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?//向右
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?if(a==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][y]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][y].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?you+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?you+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?you+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(you);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向上
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[x][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[x][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?shang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?shang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?shang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(shang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向下
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[x][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[x][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?xia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?xia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?xia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(xia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向左上
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(a<0||b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuoshang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuoshang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuoshang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(zuoshang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向右下
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(a==line||b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?youxia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?youxia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?youxia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(youxia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向左下
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a--;
?? ??? ??? ??? ??? ??? ?b++;
?? ??? ??? ??? ??? ??? ?if(a<0||b==line)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?zuoxia+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?zuoxia+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?zuoxia+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(zuoxia);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?//向右上
?? ??? ??? ??? ??? ?a=x;
?? ??? ??? ??? ??? ?b=y;
?? ??? ??? ??? ??? ?for(int i=1;i<6;i++) {
?? ??? ??? ??? ??? ??? ?a++;
?? ??? ??? ??? ??? ??? ?b--;
?? ??? ??? ??? ??? ??? ?if(a==line||b<0)
?? ??? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ??? ?if(qizilarry[a][b]!=null) {
?? ??? ??? ??? ??? ??? ??? ?if(qizilarry[a][b].getColor()==c) {
?? ??? ??? ??? ??? ??? ??? ??? ?youshang+='2';
?? ??? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ??? ?youshang+='1';
?? ??? ??? ??? ??? ??? ?}else
?? ??? ??? ??? ??? ??? ??? ?youshang+=0;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?integer=hashMap.get(youshang);
?? ??? ??? ??? ??? ?if(integer!=null)?
?? ??? ??? ??? ??? ??? ?chessValue[x][y]+=integer;
?? ??? ??? ??? ??? ?zuo="";
?? ??? ??? ??? ??? ?you="";
?? ??? ??? ??? ??? ?shang="";
?? ??? ??? ??? ??? ?xia="";
?? ??? ??? ??? ??? ?zuoshang="";
?? ??? ??? ??? ??? ?zuoxia="";
?? ??? ??? ??? ??? ?youshang="";
?? ??? ??? ??? ??? ?youxia="";
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?
?? ??? ?count1=0;
?? ??? ?count2=0;
?? ??? ?countS=0;
?? ??? ?countX=0;
?? ??? ?countZS=0;
?? ??? ?countZX=0;
?? ??? ?countYS=0;
?? ??? ?countYX=0;
?? ??? ?if("重新開始".equals(e.getActionCommand())) {
?? ??? ??? ?inte=0;
?? ??? ??? ?for(int ix=0;ix<line;ix++) {//設(shè)置所有棋子為null
?? ??? ??? ??? ?for(int iy=0;iy<line;iy++) {
?? ??? ??? ??? ??? ?if(qizilarry[ix][iy]!=null) {
?? ??? ??? ??? ??? ??? ?qizilarry[ix][iy]=null;
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?value=JOptionPane.showOptionDialog(jFrame, j, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,?
?? ??? ??? ??? ??? ?null, moshi, null);
?? ??? ??? ?value2=JOptionPane.showOptionDialog(jFrame, j2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,?
?? ??? ??? ??? ??? ?null, co, null);
?? ??? ??? ?if(value==JOptionPane.CLOSED_OPTION||value2==JOptionPane.CLOSED_OPTION) {
?? ??? ??? ??? ?System.exit(1);
?? ??? ??? ?}
?? ??? ??? ?if(value2==1) {
?? ??? ??? ??? ?c=Color.black;
?? ??? ??? ??? ?c2=Color.white;
?? ??? ??? ?}else {
?? ??? ??? ??? ?c=Color.white;
?? ??? ??? ??? ?c2=Color.black;
?? ??? ??? ?}
?? ??? ??? ?a=true;
?? ??? ??? ?jFrame.repaint();
?? ??? ?}else {
?? ??? ??? ?if(qizilarry[prex][prey]==null) {//如果上一步棋子為null了
?? ??? ??? ??? ?JLabel jLabel1 =new JLabel("只能悔一步棋!");
?? ??? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel1, "提示!", JOptionPane.WARNING_MESSAGE);
?? ??? ??? ?}else {
?? ??? ??? ??? ?qizilarry[prex][prey]=null;
?? ??? ??? ??? ?inte--;
?? ??? ??? ??? ?if(value==1) {
?? ??? ??? ??? ??? ?qizilarry[cx][cy]=null;
?? ??? ??? ??? ??? ?inte--;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?if(color ==c)?
?? ??? ??? ??? ??? ??? ?a=true;
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ??? ?a=false;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?jFrame.repaint();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
?? ?
}qizi.java:
package com.lxr.wzq1230;
import java.awt.Color;
import java.awt.Graphics;
import jicheng.boss;
public class qizi implements CS{
?? ?Graphics g;
?? ?Color color;
?? ?int x;
?? ?int y;
?? ?
?? ?public qizi(Graphics g,Color color,int x,int y) {
?? ??? ?this.g=g;
?? ??? ?this.color=color;
?? ??? ?this.x=x;
?? ??? ?this.y=y;
?? ?}
?? ?
?? ?//獲取x坐標(biāo)
?? ?public int getX() {
?? ??? ?return x;
?? ?}
?? ?
?? ?//獲取y坐標(biāo)
?? ?public int getY() {
?? ??? ?return y;
?? ?}
?? ?
?? ?//獲取顏色
?? ?public Color getColor() {
?? ??? ?return color;
?? ?}
?? ?
?? ?public void drawq(){
?? ??? ?g.setColor(color);
?? ??? ?g.fillOval(x0+x*size-size/2, y0+y*size-size/2, size, size);
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java微信開發(fā)API第三步 微信獲取以及保存接口調(diào)用憑證
這篇文章主要為大家詳細(xì)介紹了java微信開發(fā)API第二步,微信獲取以及保存接口調(diào)用憑證,感興趣的小伙伴們可以參考一下2016-06-06
使用Spring?Boot如何限制在一分鐘內(nèi)某個(gè)IP只能訪問10次
有些時(shí)候,為了防止我們上線的網(wǎng)站被攻擊,或者被刷取流量,我們會(huì)對(duì)某一個(gè)ip進(jìn)行限制處理,這篇文章,我們將通過Spring?Boot編寫一個(gè)小案例,來實(shí)現(xiàn)在一分鐘內(nèi)同一個(gè)IP只能訪問10次,感興趣的朋友一起看看吧2023-10-10
java?啟動(dòng)參數(shù)?springboot?idea詳解
這篇文章主要介紹了java?啟動(dòng)參數(shù)?springboot?idea的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
java利用udp實(shí)現(xiàn)發(fā)送數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了java利用udp實(shí)現(xiàn)發(fā)送數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Springboot整合Dozer實(shí)現(xiàn)深度復(fù)制的方法
Dozer是一種Java?Bean到Java?Bean的映射器,遞歸地將數(shù)據(jù)從一個(gè)對(duì)象復(fù)制到另一個(gè)對(duì)象,它是一個(gè)強(qiáng)大的,通用的,靈活的,可重用的和可配置的開源映射框架,本文給大家介紹Springboot整合Dozer的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2022-03-03
Java后端限制頻繁請(qǐng)求和重復(fù)提交的實(shí)現(xiàn)
很多用戶會(huì)請(qǐng)求過于頻繁或者是多次重復(fù)提交數(shù)據(jù),本文主要介紹了Java后端限制頻繁請(qǐng)求和重復(fù)提交的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java后端Tomcat實(shí)現(xiàn)WebSocket實(shí)例教程
WebSocket protocol 是HTML5一種新的協(xié)議。它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工通信(full-duplex)。一開始的握手需要借助HTTP請(qǐng)求完成握手。本文給大家介紹Java后端Tomcat實(shí)現(xiàn)WebSocket實(shí)例教程,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法
這篇文章主要介紹了Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Java線程的調(diào)度與優(yōu)先級(jí)詳解
這篇文章主要為大家詳細(xì)介紹了Java線程的調(diào)度與優(yōu)先級(jí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

