이전 두 기사 : Java는 체스 보드를 그리는 두 개의 Gozi 게임 (2)을 구현합니다. Java는 2 개의 Gozi 게임 (2)을 구현하여 체스 조각을 그립니다. Java는 2 개의 Gozi 게임 (4)을 구현하여 2 개의 Gozi 게임 (2)의 움직임을 깨닫고 그것을 볼 수 있습니다.
우리는 전에 체스 보드와 체스 조각을 뽑았으며, 체스 판에 5 개의 연속 구슬이 있는지 자유롭게 움직여서 판단 할 수 있습니다. 다음 작업은 각각의 움직임 후 체스 쪽을 교환하고 (즉, 다음 조각의 색상은 흰색-> 검은 색 또는 검은 색-> 흰색) 승리 판단에 색상 판단 조건을 추가하는 것입니다 (체스 판에 5 개의 연속 구슬이 있습니다.> 체스 보드에 5 개의 연속 구슬이 있습니다).
메인 모듈 Main.java와 Chess Class Chessman.java는 변경되지 않았습니다. DrawChessboard.java의 Mousepressed 기능과 5 개의 베드러운 판단 기능 Iswin을 조정해야합니다.
Main.java
패키지 xchen.test.simplegobang; import java.awt.container; import javax.swing.jframe; xchen.test.simplegobang.drawchessboard 가져 오기; Public Class Main은 Jframe {Private DrawChessboard DrawChessboard; public main () {drawchessboard = New DrawChessboard (); // 프레임 제목 settitle ( "독립형 고지"); 컨테이너 containerpane = getContentPane (); ContainerPane.add (DrawChessboard); } public static void main (String [] args) {main m = new Main (); M.SetSize (800, 800); M.setvisible (true); }} Chessman.java
패키지 xchen.test.simplegobang; 공개 클래스 Chessman {Private Int Color; // 1- 백인, 0- 블랙 개인 부울 = 거짓; int matchcount = 1; Public Chessman (int color, 부울 배치) {this.color = color; this.placed = 배치; } public boolean getPlaced () {반환 배치; } public void setplaced (부울 배치) {this.placed = 배치; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }}첫 번째 단계는 매번 움직이면 매번 이동 후 체스 게임이 상대방에게 제공되도록하는 것입니다 (즉, 이사 할 때마다 체스 조각의 색상이 변경됩니다).
Chesscolor의 값이 매번 수정되는 한 Drawchessboard.java의 Mousepressed 기능 부분.
@override // public void mousepressed (mouseevent e) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (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/2-1+i*span_x) // 두 개의 일치 값은 중간 지점에서 나타납니다. "+point_x+" "+(x-chessman_width/2+i*span_x)+"+(x+chessman_width/2+i*span_x); 상태 _x = i; }}} for (int i = 0; i <rows+1; i ++) {if (point_y> = ychessman_width/2+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println y "+i+"+i+""+i+" "+(y-chessman_width/2+1+i*span_y)+"+(y+chessman_width/2-1+i*span_y); 상태 _y = i; }}} Chessman Chessman = New Chessman (Chesscolor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ( "체스 컬러 :"+chesscolor); if (chesscolor == black) {chesscolor = 흰색; } else {chesscolor = 검은 색; } Repaint (); if (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!"); }}}실행하십시오
두 번째 단계는 우승 판단에 색상 판단 조건을 추가하는 것입니다 (체스 판에는 5 개의 연속 구슬이 있습니다 -> 체스 보드에는 같은 색상의 5 개의 연속 구슬이 있습니다).
Iswin 기능의 판단에 Chessman.getColor의 색상 판단을 추가하십시오.
ISWIN 기능 코드
부울 iswin (int point_x, int point_y, chessman [] [] cm) {for (int i = 0; i <rows+1; i ++) {for (int j = 0; j <rows+1; matchcolor = Chessstatus [i] [j] .getColor (); // (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) <= 행) {if (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j] .getplaced () == true && chessstatus [i+n] [J]. {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+"오른쪽 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]. Chessstatus [i] [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; // Crofresh Count}} for (int i = 0; i <rows+1; i ++) {for (int j = 0; j <rows+1; j ++) {// longitudinal if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 아래쪽으로 내려다 보면, 왼쪽 상단 코너는 좌표 원점이고, y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows) { if (chessstatus [i] [j+n]! = null && chessstatus [i] [j+n] .getPlaced () == true && chessstatus [i] [j+n] .getColor () == matchcolor) {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 && chessstatus [i] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}}} // 방향 : (int i = 0; i <rows+1; i ++) {(int j = 0; j <rows+1; j ++) {// 왼쪽 {// if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 모서리는 좌표 원점이며, y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [jn]! = null && chessstatus [in] [jn] .getPlaced () == true && chessstatus [in] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; 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; }}} // (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows && (i+n)> = 0 && (i+n) <= rows) if (chessstatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n] .getPlaced () == true && chessstatus [i+n] [j+n] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}}} // 방향 : (int i = 0; i <Rows+1; i ++) {e = 0; j <rows+1; if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 모서리는 좌표 원점이고 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]. 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]! = null && chessstatus [i+n] [jn] .getplaced () == true && chessstatus [i+n] [jn] .getColor () == mutcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}} retud false; }실행해라!
완전한 drawchessboard.java
패키지 xchen.test.simplegobang; import java.awt.color; java.awt.graphics 가져 오기; import java.awt.graphics2d; import java.awt.image; import java.awt.radialgradientPaint; java.awt.toolkit import; java.awt.event.mouseevent import; import java.awt.event.mouselistener; import javax.swing.jpanel; Public Class DrawChessboard는 JPanel 구현을 확장합니다. MousElistener {Final STATIC INT BLACK = 0; 최종 정적 int 화이트 = 1; 공개 int chesscolor = 검은 색; int chessman_width = 30; 공개 이미지 Boardimg; 최종 개인 int 행 = 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 (Graphics G) {// TODO 자동 생성 메소드 스터브 SUPER.PAINTCOMPONTE (g); int imgwidth = boardimg.getheight (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 ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getplaced () == true) {//system.out.println * "+j"+j "+j"+j "+j"+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}; 방사형 그레이드 페인트 페인트; 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, 분수, 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, 분수, colors_b); } ((Graphics2d) g) .setPaint (페인트); ((Graphics2d) g) .filloval (pos_x-chessman_width/2, pos_y-chessman_width/2, chessman_width, chessman_width); }}}} @override // public void mousepressed (mouseevent e) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (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/2-1+i*span_x) // 두 개의 일치 값은 중간 지점에서 나타납니다. "+point_x+" "+(x-chessman_width/2+i*span_x)+"+(x+chessman_width/2+i*span_x); 상태 _x = i; }}} for (int i = 0; i <rows+1; i ++) {if (point_y> = ychessman_width/2+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println y "+i+"+i+""+i+" "+(y-chessman_width/2+1+i*span_y)+"+(y+chessman_width/2-1+i*span_y); 상태 _y = i; }}} if (chessstatus [status_x] [status_y] == null || chessstatus [status_x] [status_y] .getPlaced () == false) {Chessman Chessman = New Chessman (Chesscolor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ( "체스 컬러 :"+chesscolor); if (chesscolor == black) {chesscolor = 흰색; } else {chesscolor = 검은 색; } Repaint (); if (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!"); }}}} @override // public void mouseclicked (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mousereleded (mouseevent e) {// to auto-auto-void mouternered (mouseev e) {mouseev e) {// to-greenced @override public void mouseexited (mouseevent e) {// todo 자동 생성 메소드 스텁} 부울 iswin (int point_x, int point_y, chessman [] [] cm) {(int i = 0; i <rows+1; i ++) {(int j = 0; if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) <= 행) {if (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j] .getplaced () == true && chessstatus [i+n] [J]. {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+"오른쪽 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]. Chessstatus [i] [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; // Crofresh Count}} for (int i = 0; i <rows+1; i ++) {for (int j = 0; j <rows+1; j ++) {// longitudinal if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 아래쪽으로 내려다 보면, 왼쪽 상단 코너는 좌표 원점이고, y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows) { if (chessstatus [i] [j+n]! = null && chessstatus [i] [j+n] .getPlaced () == true && chessstatus [i] [j+n] .getColor () == matchcolor) {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 && chessstatus [i] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}}} // 방향 : (int i = 0; i <rows+1; i ++) {(int j = 0; j <rows+1; j ++) {// 왼쪽 {// if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 모서리는 좌표 원점이며, y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [jn]! = null && chessstatus [in] [jn] .getPlaced () == true && chessstatus [in] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; 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; }}} // (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows && (i+n)> = 0 && (i+n) <= rows) if (chessstatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n] .getPlaced () == true && chessstatus [i+n] [j+n] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}}} // 방향 : (int i = 0; i <Rows+1; i ++) {e = 0; j <rows+1; if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 모서리는 좌표 원점이고 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]. 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]! = null && chessstatus [i+n] [jn] .getplaced () == true && chessstatus [i+n] [jn] .getColor () == mutcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 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; // Comper Count}}} return false; }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.