이전 두 기사 : Java는 체스 보드를 그리는 두 개의 Gozi 게임 (2)을 구현합니다. Java는 2 개의 Gozi 게임 (2)을 구현하여 체스 조각을 그립니다. Java는 2 개의 Gozi 게임 (4)을 구현하여 2 개의 Gozi 게임 (2)의 움직임을 깨닫고 그것을 볼 수 있습니다.
우리는 이미 전에 체스 보드와 체스 조각을 뽑았으며 자유롭게 움직일 수 있습니다. 실현 될 다음 기능은 5 개의 연속 구슬이 있는지 여부를 결정하는 것입니다 (체스 게임은 당분간 고려되지 않음).
우리는 보드가 이미 착륙 한 Traversal 위치를 사용하고 4 방향의 모든 방향으로 5 개의 연속 체스 조각이 있는지 확인합니다 : 위아래, 왼쪽 및 오른쪽, 왼쪽 및 오른쪽 아래.
첫 번째 단계 는 체스 조각 클래스를 변환하는 것입니다. 이전에는 체스 조각 클래스에 색상 정보가 있고 낙하 상태가있었습니다. 이제 우리는 트래버스 프로세스 중에 현재 연속적인 것으로 알려진 비드 수를 기록하기 위해 Int-Type 데이터를 추가해야합니다.
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; }}두 번째 단계 는 먼저 한 방향에서 5 개의 연속 구슬이 있는지 여부를 결정하고 왼쪽과 오른쪽 방향을 여기에서 시도하는 것입니다.
전체 보드에서 효과적인 조각을 가로 질러 승리하는 판단을 내리기 위해 Iswin 기능이 추가되었습니다.
Drawchessboard.java
패키지 xchen.test.simplegobang; java.awt.graphics 가져 오기; import java.awt.graphics2d; import java.awt.radialgradientPaint; import java.awt.image; java.awt.toolkit import; java.awt.event.mouseevent import; import java.awt.event.mouselistener; import java.awt.color; 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; }}} Chessman Chessman = New Chessman (Black, True); Chessstatus [status_x] [status_y] = Chessman; 리 페인트 (); if (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!!"); }}} @override // public void mouseclicked (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mousereLeased (mouseevent e) {// auto auto-auto-void mohouterentered (mouseevent e) {// auto-reged method e) {// public void mouseentered (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mouseexited (mouseevent e) {// todo 자동 생성 메소드 stub} boolean iswin (int point_x, int point_y, chessman [] cm) {// int matchcount = 1; 비드 // (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. "+J); // (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]. 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 [i] [J]. 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; // Comper Count}} return false; }}}}3 단계 : 기본 모듈은 변경되지 않았습니다. 알고리즘이 올바른지 여부를 실행하고 테스트하십시오.
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); }}4 단계 : 이제 우리는 한 방향으로 판단을 내렸으므로 다른 세 방향으로 판단 코드를 완료 할 것입니다.
drawchessboard.java에서 iswin () 함수를 보완합니다
패키지 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; }}} Chessman Chessman = New Chessman (Black, True); Chessstatus [status_x] [status_y] = Chessman; 리 페인트 (); if (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!!"); }}} @override // public void mouseclicked (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mousereLeased (mouseevent e) {// auto auto-auto-void mohouterentered (mouseevent e) {// auto-reged method e) {// public void mouseentered (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mouseexited (mouseevent e) {// todo 자동 생성 메소드 stub} boolean iswin (int point_x, int point_y, chessman [] cm) {int i <row <row+) j = 0; if (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j] .getPlaced () == true) {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 [i] [J]. 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 ++) {// perpendical if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {// 아래쪽으로 검색하면 왼쪽 상단 모서리가 좌표 원점이고 y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if (j+n) <= rows (J+n) <<= rows입니다. 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) {i] [J]. 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) {// 왼쪽 상단 모서리는 좌표 원점이고 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 [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] [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) {// 왼쪽 상단 모서리는 좌표 원점이며, 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 [i] [J]++; 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]] 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을 더 지원하기를 바랍니다.