java實現(xiàn)五子棋大戰(zhàn)
本文實例為大家分享了java實現(xiàn)五子棋大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
這是我接近一年前的項目了,以前沒有養(yǎng)成寫博客的習(xí)慣,打算陸續(xù)把以前做過的項目補上來。
一、介紹
主要實現(xiàn)的功能有棋子顏色選擇,悔棋,重新開始,玩家對戰(zhàn)和人機對戰(zhàn),效果圖如圖所是:
模式選擇:
棋子選擇:
人機對戰(zhàn):
玩家對戰(zhàn):
二、具體實現(xiàn)
五子棋的開發(fā)首先需要在界面上繪制一個表格,因為七班是不變的,棋子大小是不變的,所以我們首先可以自定義一個接口來設(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;//棋子大小 ?? ? }
和上一篇博客中的畫圖板類似,首先需要一個界面,這里可以定義一個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ù)組來存儲棋子 ?? ?private JButton b = new JButton("悔棋"); ?? ?private JButton b2=new JButton("重新開始"); ?? ?private JLabel jLabel=new JLabel("請選擇對戰(zhàn)模式"); ?? ?private JLabel jLabel2=new JLabel("請選擇棋子顏色"); ?? ?private int value;//提示框選項的值 ?? ?private int value2; ?? ?Dimension dimension=new Dimension(100, 30); ?? ?String color[]= {"白棋","黑棋"};? ?? ?String moshi[]= {"玩家對戰(zhàn)","人機對戰(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(); ?? ??? ? ?? ?} ?? ?//畫一個棋盤 ?? ?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ù)組來存儲棋子,在重繪時重繪整個棋盤和二維數(shù)組上的棋子,如果二維數(shù)組為null則不用重繪。
接下來該創(chuàng)建監(jiān)聽類了,在鼠標(biāo)點擊棋盤時,要使得棋子在棋盤的正中央,代碼如下:
//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;
判贏的方法非常簡單,只要計算棋子在它八個方向的相鄰的且顏色相同的棋子個數(shù)即可,這里只展現(xiàn)向左查找棋子的代碼(后續(xù)會附上整個監(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)模式為玩家和玩家模式時,需要每下一個棋子顏色改變,實現(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)了玩家與玩家的判贏方法,即每下一個新的棋子,就計算這個棋子八個方向上相同且相鄰棋子的個數(shù),當(dāng)同一直線兩個方向的棋子個數(shù)之和為5時,則獲取棋子顏色,判定為獲勝,具體代碼實現(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重新計算 ?? ??? ??? ??? ??? ?count2=0; ?? ??? ??? ??? ??? ?countS=0; ?? ??? ??? ??? ??? ?countX=0; ?? ??? ??? ??? ??? ?countZS=0; ?? ??? ??? ??? ??? ?countZX=0; ?? ??? ??? ??? ??? ?countYS=0; ?? ??? ??? ??? ??? ?countYX=0; ?? ??? ??? ??? ?}
這樣玩家與玩家模式的大體功能就基本實現(xiàn)了,接下類就要實現(xiàn)五子棋的AI功能了
五子棋AI
這里我們主要針權(quán)值法討論下,大致思路如下:
1.我們繪制好一個棋盤后,大小為 15*15;
2.下棋之前,對于棋盤中的每個空位,我們每都替電腦人“掂一掂”下在哪里合算;(估權(quán)過程)
3.對每個空位按照規(guī)則都計算完權(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表存儲所有可能的情況并賦予一定的權(quán)值,每下一個棋子便更新棋盤上所有空位置的權(quán)值,電腦再尋找棋盤上權(quá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;//上一個棋子的坐標(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;//玩家對戰(zhàn)下棋順序 ?? ?private boolean b=true;//是否在同一個位置 //?? ?private boolean end=true;//是否電腦勝利 ?? ?private Color c;//用戶棋子顏色 ?? ?private Color c2;//電腦棋子顏色 ?? ?private Color color;//玩家對戰(zhàn)顏色交替下棋 ?? ?public qizi qizilarry[][]=new qizi[line][line]; ?? ?private int chessValue[][]=new int[line][line];//權(quán)值表 ?? ?private JLabel j=new JLabel("請選擇對戰(zhàn)模式"); ?? ?private JLabel j2=new JLabel("請選擇棋子顏色"); ?? ?String moshi[]= {"玩家對戰(zhàn)","人機對戰(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; ?? ??? ?//判斷是否在同一個地方?? ? ?? ??? ?b=true; ?? ??? ?if(qizilarry[ix][iy]!=null) { ?? ??? ??? ?JLabel jLabel =new JLabel("不能下在同一個地方!"); ?? ??? ??? ?JOptionPane.showMessageDialog(jFrame, jLabel, "警告", JOptionPane.WARNING_MESSAGE); ?? ??? ??? ?b=false; ?? ??? ?} ?? ??? ?//-------------------- ?? ??? ?//畫圓及重構(gòu),一個黑棋一個白棋 ?? ??? ?//-------------------- ?? ??? ?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重新計算 ?? ??? ??? ??? ??? ?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); ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot 注冊服務(wù)注冊中心(zk)的兩種方式詳解
本文通過一個demo講述一下這兩種注冊方式,使用的是傳統(tǒng)的向zk注冊的方案。對springboot 注冊zk的相關(guān)知識感興趣的朋友一起看看吧2018-01-01Spring?Boot?利用?XML?方式整合?MyBatis
這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關(guān)資料展開詳細的內(nèi)容介紹,具有一定的參考價值,組要的小伙伴可以參考一下2022-05-05- 猜數(shù)字是興起于英國的益智類小游戲,起源于20世紀(jì)中期,一般由兩個人或多人玩,也可以由一個人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來用Java把這個小游戲?qū)懗鰜砭毦毷?/div> 2021-10-10
解決idea使用maven編譯正常但是運行項目時卻提示很多jar包找不到的問題
這篇文章主要介紹了解決idea使用maven編譯正常但是運行項目時卻提示很多jar包找不到的問題,本文分多種情形給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07最新評論