前の2つの記事:Javaは2つのGoziゲーム(2)を実装してチェスボードを描きます。 Javaは、チェスのピースを描くために2つのGoziゲーム(2)を実装しています。 Javaは2つのGoziゲーム(4)を実装して、2つのGoziゲーム(2)の動きを実現して表示します。
私たちはすでにチェスボードとチェスの部分を以前に描いており、自由に動きをすることができます。次に実現する機能は、5つの連続したビーズがあるかどうかを判断することです(当面はチェスゲームは考慮されていません)。
私たちは、ボードがすでに着陸しているトラバーサル位置を使用し、4つの方向の任意の方向に5つの連続したチェスピースがあるかどうかを確認します。
最初のステップは、チェスピースクラスを変換することです。以前は、チェスピースのクラスには色情報とドロップステータスのみがありました。次に、int-typeデータを追加して、移動プロセス中に連続していることが現在知られているビーズの数を記録する必要があります。
Chessman.java
パッケージxchen.test.simplegobang;パブリッククラスのチェスマン{private int color; // 1-ホワイト、0ブラックプライベートブーリアン配置= false; int matchcount = 1; Public Chessman(int color、boolean placed){this.color = color; this.placed = placed; } public boolean getPlaced(){return placed; } public void setPlaced(boolean placed){this.placed = placed; } public int getColor(){return color; } public void setcolor(int color){this.color = color; }}2番目のステップは、最初に一方向から5つの連続したビーズがあるかどうかを判断し、ここでの試みとして左方向と右方向を使用することです。
ISWIN機能が追加され、ボード全体で効果的なピースを横断することにより、勝利の判断を下します。
drawchessboard.java
パッケージxchen.test.simplegobang; java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.radialgradientPaintをインポートします。 java.awt.imageをインポートします。 java.awt.toolkitをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mouselistenerをインポートします。 java.awt.colorをインポートします。 javax.swing.jpanelをインポートします。パブリッククラスのdrawchessboard拡張jpanel実装museListener {final static int black = 0;最終的な静的intホワイト= 1; public int chesscolor = black; int chessman_width = 30;パブリックイメージボード。最終的なプライベートインクロウ= 19; Chessman [] [] Chessstatus = new Chessman [rows+1] [rows+1]; public DrawChessboard(){boardimg = toolkit.getDefaultToolkit()。getImage( "res/drawable/chessboard2.png"); if(boardimg == null)system.err.println( "pngは存在しません"); addmouseListener(this); } @Override Protected void paintComponent(グラフィックスG){// todo auto-enerated method stub super.paintcomponent(g); int imgwidth = boardimg.getheigh(this); int imgheight = boardimg.getWidth(this); int fwidth = getWidth(); int fheight = getheight(); int x =(fwidth-imgwidth)/2; int y =(fheight-imgheight)/2; int span_x = imgwidth/rows; int span_y = imgheight/rows; G.drawimage(boardimg、x、y、null); //(int i = 0; i <rows; i ++){g.drawline(x、y+i*span_y、fwidth-x、y+i*span_y); } //(int i = 0; i <rows; i ++){g.drawline(x+i*span_x、y、x+i*span_x、fheight-y)の垂直線を描画します。 } //(int i = 0; i <rows+1; i ++)for(int j = 0; j <rows+1; j ++){for(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){ / /system.out.print.print.print.print.print。 "+j); int pos_x = x+i*span_x; int pos_y = y+j*span_y; float radius_b = 40; float radius_w = 80; float [] fractions = new float [] {0f、1f}; java.awt.color [] colors_b = new Java.awt.Color [] {color.black、color.white}; color [] colors_w = new color [] {color.white、color.black}; radialgradientPaintペイント。 if(chessstatus [i] [j] .getcolor()== 1){//system.out.println("draw white chess "); Paint = new RadialGradientPaint(pos_x-chessman_width/2f、pos_y-chessman_width/2f、radius_w*2、fractions、colors_w); } else {//system.out.println("draw black chess "); Paint = new RadialGradientPaint(pos_x-chessman_width/2f、pos_y-chessman_width/2f、radius_b*2、fractions、colors_b); }((graphics2d)g).setpaint(paint); ((Graphics2d)g).filloval(pos_x-chessman_width/2、pos_y-chessman_width/2、chessman_width、chessman_width); }}}} @override // public void mousepressed(mousevent e){int point_x = e.getx(); int point_y = e.gety(); int imgwidth = boardimg.getheigh(this); int imgheight = boardimg.getWidth(this); int fwidth = getWidth(); int fheight = getheight(); int x =(fwidth-imgwidth)/2; int y =(fheight-imgheight)/2; int span_x = imgwidth/rows; int span_y = imgheight/rows; //system.out.println( "press"); int status_x = 0; int status_y = 0; if(point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight){//system.out.println("legal "); for(int i = 0; i <rows+1; i ++){if(point_x> = x-chessman_width/2+1+i*span_x){if(point_x <= x+chessman_width/+i*span_x)// "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }}} for(int i = 0; i <rows+1; i ++){if(point_y> = y-chessman_width/2+1+i*span_y){if(point_y <= y+chessman_width/2-1+i*span_y){///system.out.out.out.out.Out.Out "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; }}}チェスマンチェスマン= new Chessman(Black、True); chessstatus [status_x] [status_y] = chessman; Repaint(); if(iswin(status_x、status_y、chessstatus)){system.out.println( "win !!!!"); }}} @Override // public void mouseclicked(mousevent e){// todo auto-fide method stub} @override public void mouseReeded(mousevent e){// todo auto-generated method stub} public void mouseentered(mousevent e){// todo auto-enerated method stub} @override public void mouseexited(mouseevent e){// todo auto-denerated method stub} boolean iswin(int point_x、int point_y、chessman [] cm {// intcount = ake ount = 1// (int i = 0; i <rows+1; i ++){for(int j = 0; j <rows+1; j ++){if(chessstatus [i]!= null && chessstatus [i] [j] .getplaced()== true){//system.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out. //(int n = 1; n <= 4; n ++){if((i+n> = 0)&&(i+n)<= rows){if(chessstatus [i+n] [j]!= null && chessstatus [i+n] [j] .getplaced()= true){chessstatus [i] [j]。[i]。[j] .matchcount++ System.out.println( "pos:"+i+""+j+"right count ++:"+(i+n)+""+j+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {break; }}} //(int n = 1; n <= 4; n ++){if(in> = 0)&&(in)<rows){if(chessstatus [in] [j]!= null && chessstatus [in] [j] .getplaced()== true){chessstatus [j] [j] .matchcount+++; System.out.println( "pos:"+i+""+j+""+"left count ++:"+(in)+""+j+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {if(chessstatus [in] [j]!= null){chessstatus [i] [j] .matchcount = 1; } 壊す; }}} chessStatus [i] [j] .matchcount = 1; // refresh count}} false; }}}}ステップ3 :メインモジュールは変更されていません。アルゴリズムが正しいかどうかを実行してテストします。
main.java
パッケージxchen.test.simplegobang; Import Java.awt.Container; javax.swing.jframeをインポートします。 xchen.test.simplegobang.drawchessboardをインポートします。 Public Class Mainはjframe {private Drawchessboard Drawchessboardを拡張します。 public main(){drawchessboard = new DrawChessBoard(); //フレームタイトルSettitle( "Stand-Alone Goji"); container containerpane = getContentPane(); containerpane.add(drawchessboard); } public static void main(string [] args){main m = new main(); M.Setsize(800、800); M.SetVisible(true); }}ステップ4 :一方の方向に判断を下したので、他の3つの方向に判断コードを完了します。
drawchessboard.javaのiswin()関数を補完します
パッケージxchen.test.simplegobang; java.awt.colorをインポートします。 java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.imageをインポートします。 java.awt.radialgradientPaintをインポートします。 java.awt.toolkitをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mouselistenerをインポートします。 javax.swing.jpanelをインポートします。パブリッククラスのdrawchessboard拡張jpanel実装museListener {final static int black = 0;最終的な静的intホワイト= 1; public int chesscolor = black; int chessman_width = 30;パブリックイメージボード。最終的なプライベートインクロウ= 19; Chessman [] [] Chessstatus = new Chessman [rows+1] [rows+1]; public DrawChessboard(){boardimg = toolkit.getDefaultToolkit()。getImage( "res/drawable/chessboard2.png"); if(boardimg == null)system.err.println( "pngは存在しません"); addmouseListener(this); } @Override Protected void paintComponent(グラフィックスG){// todo auto-enerated method stub super.paintcomponent(g); int imgwidth = boardimg.getheigh(this); int imgheight = boardimg.getWidth(this); int fwidth = getWidth(); int fheight = getheight(); int x =(fwidth-imgwidth)/2; int y =(fheight-imgheight)/2; int span_x = imgwidth/rows; int span_y = imgheight/rows; G.drawimage(boardimg、x、y、null); //(int i = 0; i <rows; i ++){g.drawline(x、y+i*span_y、fwidth-x、y+i*span_y); } //(int i = 0; i <rows; i ++){g.drawline(x+i*span_x、y、x+i*span_x、fheight-y)の垂直線を描画します。 } //(int i = 0; i <rows+1; i ++)for(int j = 0; j <rows+1; j ++){for(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){ / /system.out.print.print.print.print.print。 "+j); int pos_x = x+i*span_x; int pos_y = y+j*span_y; float radius_b = 40; float radius_w = 80; float [] fractions = new float [] {0f、1f}; java.awt.color [] colors_b = new Java.awt.Color [] {color.black、color.white}; color [] colors_w = new color [] {color.white、color.black}; radialgradientPaintペイント。 if(chessstatus [i] [j] .getcolor()== 1){//system.out.println("draw white chess "); Paint = new RadialGradientPaint(pos_x-chessman_width/2f、pos_y-chessman_width/2f、radius_w*2、fractions、colors_w); } else {//system.out.println("draw black chess "); Paint = new RadialGradientPaint(pos_x-chessman_width/2f、pos_y-chessman_width/2f、radius_b*2、fractions、colors_b); }((graphics2d)g).setpaint(paint); ((Graphics2d)g).filloval(pos_x-chessman_width/2、pos_y-chessman_width/2、chessman_width、chessman_width); }}}} @override // public void mousepressed(mousevent e){int point_x = e.getx(); int point_y = e.gety(); int imgwidth = boardimg.getheigh(this); int imgheight = boardimg.getWidth(this); int fwidth = getWidth(); int fheight = getheight(); int x =(fwidth-imgwidth)/2; int y =(fheight-imgheight)/2; int span_x = imgwidth/rows; int span_y = imgheight/rows; //system.out.println( "press"); int status_x = 0; int status_y = 0; if(point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight){//system.out.println("legal "); for(int i = 0; i <rows+1; i ++){if(point_x> = x-chessman_width/2+1+i*span_x){if(point_x <= x+chessman_width/+i*span_x)// "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }}} for(int i = 0; i <rows+1; i ++){if(point_y> = y-chessman_width/2+1+i*span_y){if(point_y <= y+chessman_width/2-1+i*span_y){///system.out.out.out.out.Out.Out "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; }}}チェスマンチェスマン= new Chessman(Black、True); chessstatus [status_x] [status_y] = chessman; Repaint(); if(iswin(status_x、status_y、chessstatus)){system.out.println( "win !!!!"); }}} @Override // public void mouseclicked(mousevent e){// todo auto-fide method stub} @override public void mouseReeded(mousevent e){// todo auto-generated method stub} public void mouseentered(mousevent e){// todo auto-enerated method stub} @override public void mouseexited(mouseevent e){// todo auto-fid method stub} boolean iswin(int point_x、int point_y、chessman [] cm) j = 0; j <rows+1; j ++){//ライブ水平検索if(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){//(int n = 1; n <= 4; n ++){(i+n = 0)<= 0)<= 0) if(chessstatus [i+n] [j]!= null && chessstatus [i+n] [j] .getplaced()== true){chessstatus [i] [j] .matchcount ++; System.out.println( "pos:"+i+""+j+"right count ++:"+(i+n)+""+j+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {break; }}} //(左から左n = 1; n <= 4; n ++){if((in> = 0)&&(in)<= rows){if(chessstatus [in] [in]!= null && chessstatus [in] [in] [j] .getplaced()= true){chessstatus [j]。[j]。[i] .gedtatus [i] .getplaced++ System.out.println( "pos:"+i+""+j+""+"left count ++:"+(in)+""+j+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {if(chessstatus [in] [j]!= null){chessstatus [i] [j] .matchcount = 1; } 壊す; }}}} chessStatus [i] [j] .matchcount = 1; // reffresse count}}}}} for(int i = 0; i <rows+1; i ++){for(int j = 0; j <rows+1; j ++){//垂直if(chessStatus [i] [j]!= null && chessstatus [i] [j] .getPlaced()== true){//下向きに検索すると、左上隅が座標起点であり、y軸の正の方向は(int n = 1; n <= 4; n ++){(j+n> = 0)< if(chessstatus [i] [j+n]!= null && chessstatus [i] [j+n] .getPlaced()== true){chessstatus [i] [j] .matchcount ++; System.out.println( "pos:"+i+""+j+"up count ++:"+(i)+""+(j+n)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {break; }}} //(int n = 1; n <= 4; n ++){if((jn> = 0)&&(jn)<= rows){if(chessstatus [i] [jn]!= null && chessstatus [i] [jn] .getplaced()= = true+++++ System.out.println( "pos:"+i+""+j+""+"left count ++:"+(i)+""+(jn)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {if(chessstatus [i] [jn]!= null){chessstatus [i] [j] .matchcount = 1; } 壊す; }}}} chessStatus [i] [j] .matchcount = 1; // reffresse count}}}} //方向:左上と右下(int i = 0; i <rows+1; i ++){for(int j = 0; j <rows+1; j ++){//左上左上if(chessstatus [i] [j]!= null && chessstatus [i] [j] .getPlaced()== true){//左上隅は座標の原点であり、y軸の正の方向は(int n = 1; n <= 4; n ++){ if((jn> = 0)&&(jn)<= rows &&(in)> = 0 &&(in)<= rows){if(chessstatus [in] [in]!= null && chessstatus [in] [jn] .getplaced()== true){chessstatus [i] [j] .matchount++; System.out.println( "pos:"+i+""+j+"up count ++:"+(in)+""+(jn)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {break; }}}} //右下for(int n = 1; n <= 4; n ++){if((j+n> = 0)&&(j+n)<= rows &&(i+n)> = 0 &&(i+n)<= rows){rows){ if(chessstatus [i+n] [j+n]!= null && chessstatus [i+n] [j+n] .getPlaced()== true){chessstatus [i] [j] .matchcount ++; System.out.println( "pos:"+i+""+j+""+"left count ++:"+(i+n)+""+(j+n)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {if(chessstatus [i+n] [j+n]!= null){chessstatus [i] [j] .matchcount = 1; } 壊す; }}}} chessStatus [i] [j] .matchcount = 1; // reffresse count}}}} //方向:(int i = 0; i <rows+1; i ++){for(int j = 0; j <rows+1; j ++){//左下左if(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){//左上隅は座標起点であり、y軸の正の方向は(int n = 1; n <= 4; n ++){{ if((j+n> = 0)&&(j+n)<= rows &&(in)> = 0 &&(in)<= rows){if(chessstatus [in] [j+n]!= null && chessstatus [in] [j+n] .getplaced()== true){chessstatus [j] [j] .matchcount+; System.out.println( "pos:"+i+""+j+"up count ++:"+(in)+""+(j+n)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {break; }}}} //(int n = 1; n <= 4; n ++){if(jn> = 0)&&(jn)<= rows &&(i+n)> = 0 &&(i+n)<= rows){if(chessstatus [i+n] [jn] {chessstatus [i] [j] .matchcount ++; System.out.println( "pos:"+i+""+j+""+"left count ++:"+(i+n)+""+(jn)+"count:"+chessstatus [i] [j] .matchcount); if(chessstatus [i] [j] .matchcount == 5){return true; }} else {if(chessstatus [i+n] [jn]!= null){chessstatus [i] [j] .matchcount = 1; } 壊す; }}} chessStatus [i] [j] .matchcount = 1; // reffresh count}}} return false; }}もう一度実行します
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。