المادتان السابقتان: Java تنفذ لعبة Gozi (اثنان) لرسم لوحة شطرنج ؛ Java تنفذ لعبة Gozi (اثنان) لرسم قطعة شطرنج ؛ تنفذ Java لعبة Gozi (أربعة) لإدراك خطوة لعبة Gozi (اثنان) لمشاهدتها.
لقد رسمنا بالفعل ألواح الشطرنج وقطع الشطرنج من قبل ويمكننا القيام بحركة بحرية. تتمثل الوظيفة التالية التي يجب تحقيقها في تحديد ما إذا كانت هناك خمسة حبات متتالية (لا تعتبر لعبة الشطرنج في الوقت الحالي).
نحن نستخدم موضع اجتياز حيث هبطت اللوحة بالفعل وتحقق مما إذا كان هناك خمس قطع شطرنج متتالية في أي اتجاه من اتجاهاتها الأربعة: لأعلى ولأسفل ، اليسار واليمين ، أسفل اليسار واليمين.
الخطوة الأولى هي تحويل فئة قطعة الشطرنج. في السابق ، كان لدى فئة قطعة الشطرنج معلومات الألوان وحالة الإسقاط فقط. الآن نحتاج إلى إضافة بيانات من النوع Int لتسجيل عدد الخرزات المعروفة حاليًا أنها مستمرة أثناء عملية اجتياز.
تشيسمان
حزمة Xchen.test.simplegobang ؛ الطبقة العامة Chessman {private int color ؛ // 1-white ، 0 boolean private placed = false ؛ int matchcount = 1 ؛ Public Chessman (int color ، boolean placed) {this.color = color ؛ this.placed = وضعت ؛ } 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 ؛ }}والخطوة الثانية هي أولاً تحديد ما إذا كانت هناك خمسة حبات متتالية من اتجاه واحد ، واستخدام الاتجاهات اليمنى واليسرى كمحاولة هنا.
تمت إضافة وظيفة 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 mouselistener {Final Static int Black = 0 ؛ الثابت النهائي int الأبيض = 1 ؛ شطرنج int العام = أسود ؛ int chessman_width = 30 ؛ الصورة العامة poardimg ؛ صفوف int الخاصة النهائية = 19 ؛ Chessman [] [] chessstatus = new Chessman [الصفوف+1] [صفوف+1] ؛ Public DrawChessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("res/drawable/chessboard2.png") ؛ if (boardimg == null) system.err.println ("png غير موجود") ؛ addmouselistener (هذا) ؛ } override paintcomponent paintcomponent المحمية (الرسومات G) {// todo method method method super.paintcomponent (g) ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ 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/ows ؛ 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]. 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 [] color_w = new color [] {color.hite ، color.black} ؛ طلاء RadialGradientPaint ؛ if (chessstatus [i] [j] .getColor () == 1) {//system.out.println("draw white chess ") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_w*2 ، fractions ، colors_w) ؛ } آخر {//system.out.println("Draw Black Chess ") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_b*2 ، fractions ، colors_b) ؛ } ((graphics2d) g) .setpaint (paint) ؛ ((Graphics2d) g). }}}} Override // public void mousepressed (mouseevent e) {int point_x = e.getx () ؛ int point_y = e.gety () ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ 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/ows ؛ //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 ") ؛ لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*i *x) // إذا كان ذلك العرض/2 ، فإن قيمتان مطابقتان ستظهر في الأوسط {///system. "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)) ؛ status_x = i ؛ }}} لـ (int i = 0 ؛ i <صفوف+1 ؛ i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)) ؛ status_y = i ؛ }}} Chessman Chessman = 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 (mouseevent e) {// todo method method method ad} override public mousereleded (mouseevent e) {// todo todo method method aduverride mothered (mouseevent e) mouseentered (mouseevent e) {// todo method method mouseexited (mouseevent e) {// todo todo method method method adccount} لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {for (int j = 0 ؛ j <rows+1 ؛ j ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j]. // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if ((i+n> = 0) && (i+n) <= rows) {if (chessstatus [i+n] [j]! = null && chessstatus [i+n] System.out.println ("pos:"+i+""+j+"count right ++:"+(i+n)+""+j+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ }}} // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if (in> = 0) && (in) <= rows) {if (chessstatus [in] [j]! = null && chessstatus [in]. 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}} return false ؛ }}}}الخطوة 3 : تظل الوحدة الرئيسية دون تغيير. تشغيل واختبار ما إذا كانت خوارزميةنا صحيحة.
main.java
حزمة Xchen.test.simplegobang ؛ استيراد java.awt.container ؛ استيراد javax.swing.jframe ؛ استيراد xchen.test.simplegobang.drawChessboard ؛ يمتد الفئة العامة الرئيسية JFRAME {private DrawChessboard DrawChessboard ؛ Public Main () {DrawChessboard = new DrawChessboard () ؛ // عنوان الإطار Settitle ("Goji المستقل") ؛ حاوية حاوية = getContentPane () ؛ ContainerPane.Add (DrawChessboard) ؛ } public static void main (string [] args) {main m = new main () ؛ M.SetSize (800 ، 800) ؛ M.SetVisible (صحيح) ؛ }}الخطوة 4 : الآن بعد أن أصدرنا حكمًا في اتجاه واحد ، سنكمل قانون الحكم في الاتجاهات الثلاثة الأخرى.
استكمال وظيفة ISWIN () في drawchessboard.java
حزمة 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 mouselistener {Final Static int Black = 0 ؛ الثابت النهائي int الأبيض = 1 ؛ شطرنج int العام = أسود ؛ int chessman_width = 30 ؛ الصورة العامة poardimg ؛ صفوف int الخاصة النهائية = 19 ؛ Chessman [] [] chessstatus = new Chessman [الصفوف+1] [صفوف+1] ؛ Public DrawChessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("res/drawable/chessboard2.png") ؛ if (boardimg == null) system.err.println ("png غير موجود") ؛ addmouselistener (هذا) ؛ } override paintcomponent paintcomponent المحمية (الرسومات G) {// todo method method method super.paintcomponent (g) ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ 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/ows ؛ 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]. 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 [] color_w = new color [] {color.hite ، color.black} ؛ طلاء RadialGradientPaint ؛ if (chessstatus [i] [j] .getColor () == 1) {//system.out.println("draw white chess ") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_w*2 ، fractions ، colors_w) ؛ } آخر {//system.out.println("Draw Black Chess ") ؛ Paint = New RadialGradientPaint (pos_x-thessman_width/2f ، pos_y-chessman_width/2f ، radius_b*2 ، fractions ، colors_b) ؛ } ((graphics2d) g) .setpaint (paint) ؛ ((Graphics2d) g). }}}} Override // public void mousepressed (mouseevent e) {int point_x = e.getx () ؛ int point_y = e.gety () ؛ int imgwidth = boardimg.getheight (هذا) ؛ int imgheight = boardimg.getWidth (هذا) ؛ 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/ows ؛ //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 ") ؛ لـ (int i = 0 ؛ i <الصفوف+1 ؛ i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*i *x) // إذا كان ذلك العرض/2 ، فإن قيمتان مطابقتان ستظهر في الأوسط {///system. "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)) ؛ status_x = i ؛ }}} لـ (int i = 0 ؛ i <صفوف+1 ؛ i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)) ؛ status_y = i ؛ }}} Chessman Chessman = 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 (mouseevent e) {// todo method method method ad} override public mousereleded (mouseevent e) {// todo todo method method aduverride mothered (mouseevent e) mouseentered (mouseevent e) {// todo method method mouseexited (mouseevent e) {// todo tominated method method stub} boolean iswin (int point_x ، int point_y ، chessman [] cm) j = 0 ؛ j <rows+1 ؛ j ++) {// live search أفقي إذا (chessstatus [i] [j]! = null && chessstatus [i] [j] if (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j]. System.out.println ("pos:"+i+""+j+"count right ++:"+(i+n)+""+j+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ }}} // ابحث عن (int to the left n = 1 ؛ n <= 4 ؛ n ++) {if ((in> = 0) && (in) <= rows) {if (chessstatus [in] [j]! = null && chessstatus [in]. 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]! = null && chessstatus [i] [j]. 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 ؛ }} آخر {break ؛ }}} // ابحث عن (int n = 1 ؛ n <= 4 ؛ n ++) {if ((jn> = 0) && (jn) <= rows) {if (chessstatus [i] [jn]! = null && chessstatus [i]. System.out.println ("pos:"+i+""+j+""+"left count ++:"+(i)+""+(jn)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {if (chessstatus [i] [jn]! = null) {chessstatus [i] [j] .matchCount = 1 ؛ } استراحة؛ }} ستر إذا (chessstatus [i] [j]! = null && chessstatus [i] [j]. if ((jn> = 0) && (jn) <= rows && (in)> = 0 && (in) <= rows) {if (chessstatus [in] [jn]! = null && chessstatus [in] [jn]. System.out.println ("pos:"+i+""+j+"up count ++:"+(in)+""+(jn)+"count:"+chessstatus [i] [j] .MatchCount) ؛ if (chessstatus [i] [j] .MatchCount == 5) {return true ؛ }} آخر {break ؛ } ستر 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 ؛ }} آخر {if (chessstatus [i+n] [j+n]! = null) {chessstatus [i] [j] .MatchCount = 1 ؛ } استراحة؛ }} ستر إذا (chessstatus [i] [j]! = null && chessstatus [i] [j]. 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 ؛ }} آخر {break ؛ }}} // العلوي اليمين لـ (int n = 1 ؛ n <= 4 ؛ n ++) {if (jn> = 0) && (jn) <= rows && (i+n)> = 0 & && (i+n) <= rows) {if (chessstatus [i+n]! 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 ؛ }} آخر {if (chessstatus [i+n] [jn]! = null) {chessstatus [i] [j] .MatchCount = 1 ؛ } استراحة؛ }}} chessstatus [i] [j] .MatchCount = 1 ؛ // refresh count}}} return false ؛ }}قم بتشغيله مرة أخرى
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.