تصف هذه المقالة رمز لعبة الشطرنج الصينية التي تنفذها Java. تتم مشاركتها معك للرجوع إليها. الرمز المحدد كما يلي
1. الغرض العملي:
1. التطبيق والفرق بين النقر الفأري ، السحب والأحداث الأخرى
2. حفظ وقراءة ملفات سجل الشطرنج
3. تحسين قواعد الشطرنج.
2. المحتوى العملي:
الشطرنج الصينية لها تاريخ طويل وجذب عدد لا يحصى من الناس للدراسة. الآن قمنا بالتصميم والتفسير التاليين لمعارك الشطرنج الصيني وإنتاج درجات الشطرنج للرجوع إليه وتعلمك.
1. عند لعب الشطرنج بين الآلات والآلات ، يأخذ الجانب الأحمر الصدارة. اسحب قطعة الشطرنج إلى الوجهة إذا كانت تلبي القواعد وإسقاط السنجاب.
الجميع يلعب الشطرنج
2. اجعل سجل الشطرنج. بعد اختيار قائمة سجلات الشطرنج ، تبدأ اللعبة ويتم تسجيل عملية الشطرنج.
حدد قائمة "Make Chess Record"
فاز الجانب الأحمر بعد الانتهاء من درجة الشطرنج
بعد فوز جانب واحد ، يبرز مربع حوار رسالة النصر. بعد النقر فوق "موافق" ، حدد قائمة "حفظ سجل الشطرنج" وملبع حوار حفظ الملف.
حفظ مربع الحوار سجل الشطرنج
3. إظهار درجة الشطرنج. بعد تحديد قائمة نقاط الشطرنج العرضية ، ظهر مربع حوار مفتوح ، حدد درجة الشطرنج المحفوظة لبدء العرض التوضيحي.
إظهار مربع الحوار سجل الشطرنج
إظهار عملية سجل الشطرنج (التلقائي واليدوي)
3. الرمز المرجعي:
1. شطرنج الملف الرئيسي chinesechess.java
حزمة cn.edu.ouc.Chinesechess ؛ استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد java.io.*؛ استيراد java.util.linkedList ؛ / ** * فئة الشطرنج الرئيسية * * Author CnlHt */ Class Class Class Extraphs Jframe يمتد ActionListener {Chessboard Board = null ؛ شيطان الشيطان = فارغ ؛ سجل makechessmanual = فارغ ؛ حاوية con = null ؛ Jmenubar Bar Jmenu Filemenu ؛ Jmenuitem جعل درجة الشطرنج ، حفظ درجة الشطرنج ، إظهار درجة الشطرنج. JfileChooser filechooser = null ؛ LinkedList chess score = null ؛ chinesechess العامة () {bar = new jmenubar () ؛ FileMenu = New Jmenu ("الشطرنج الصيني") ؛ Make Chess Score = New Jmenuitem ("Make Chess Price") ؛ حفظ درجة الشطرنج = new jmenuitem ("حفظ درجة الشطرنج") ؛ حفظ الشطرنج. نقاط الشطرنج المظاهرة = New JMenuitem ("SCORT RECENT TRIGE") ؛ filemenu.add (جعل درجة الشطرنج) ؛ filemenu.add (حفظ درجة الشطرنج) ؛ filemenu.add (إظهار درجة الشطرنج) ؛ Bar.Add (FileMenu) ؛ setjmenubar (بار) ؛ Settitle (Make Chess Score.getText ()) ؛ Make Chess Score.AddActionListener (هذا) ؛ حفظ الشطرنج. إظهار درجة الشطرنج. لوحة = شطرنج جديد (45 ، 45 ، 9 ، 10) ؛ سجل = board.record ؛ con = getContentPane () ؛ JSplitpane split = new jsplitpane (jsplitpane.horizontal_split ، true ، board ، record) ؛ split.setDiversize (5) ؛ split.setDividerLocation (460) ؛ Con.add (Split ، BorderLayout.Center) ؛ AddWindOwListener (new WindowAdapter () {public void windowclosing (windowevent e) {system.exit (0) ؛}}) ؛ setVisible (صحيح) ؛ setBounds (60 ، 20 ، 690 ، 540) ؛ filechooser = new JfileChooser () ؛ con.validate () ؛ التحقق () ؛ } public void actionperformed (ActionEvent e) {if ( حفظ الشطرنج Score.SetEnabled (صواب) ؛ this.settitle (إنشاء chess score.getText ()) ؛ لوحة = شطرنج جديد (45 ، 45 ، 9 ، 10) ؛ سجل = board.record ؛ JSplitpane split = new jsplitpane (jsplitpane.horizontal_split ، true ، board ، record) ؛ split.setDiversize (5) ؛ split.setDividerLocation (460) ؛ Con.add (Split ، BorderLayout.Center) ؛ التحقق () ؛ } if ( ملف saveFile = filechooser.getSelectedFile () ؛ if (saveFile! = null && state == jfileChooser.approve_option) {try {fileOutputStream Outone = new FileOutputStream (SaveFile) ؛ ObjectOutputStream outtwo = new ObjectOutputStream (Outone) ؛ outtwo.writeObject (record.get chess score ()) ؛ Outone.close () ؛ outtwo.close () ؛ } catch (حدث ioException) {}}} if ( con.repaint () ؛ con.validate () ؛ التحقق () ؛ حفظ الشطرنج. int state = filechooser.showopendialog (null) ؛ ملف openFile = filechooser.getSelectedFile () ؛ if (openFile! = null && state == jFileChoOser.approve_option) {try {fileInputStream inOne = new FileInputStream (OpenFile) ؛ ObjectInputStream intwo = new ObjectInputStream (inone) ؛ الشطرنج = (LinkedList) Intwo.ReadObject () ؛ inone.close () ؛ intwo.close () ؛ لوحة الشطرنج = شطرنج جديد (45 ، 45 ، 9 ، 10) ؛ شيطان = شيطان جديد (مجلس) ؛ سجل الشطرنج Demon.Set (سجل الشطرنج) ؛ Con.add (Demon ، BorderLayout.Center) ؛ con.validate () ؛ التحقق () ؛ this.settitle (demo chess record.getText () + ":" + openFile) ؛ } catch (حدث استثناء) {jlabel label = new JLabel ("ملف سجل الشطرنج") ؛ label.setfont (New Font ("Lishu" ، font.bold ، 60)) ؛ label.setforeground (color.red) ؛ label.SethorizontalAlignment (swingconstants.center) ؛ Con.add (label ، borderlayout.center) ؛ con.validate () ؛ this.settitle ("لا سجل الشطرنج") ؛ التحقق () ؛ }} آخر {jlabel label = new JLabel ("لا يتم فتح ملف سجل الشطرنج") ؛ label.setfont (New Font ("Lishu" ، font.bold ، 50)) ؛ label.setForeGround (color.pink) ؛ label.SethorizontalAlignment (swingconstants.center) ؛ Con.add (label ، borderlayout.center) ؛ con.validate () ؛ this.settitle ("ملف الشطرنج غير مفتوح") ؛ التحقق () ؛ }}} public static void main (String args []) {new Chinesechess () ؛ }} 2. ملف Chessboard.java
حزمة cn.edu.ouc.Chinesechess ؛ استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ / ** * فئة الشطرنج * * Author Cnlht */ تمديد الشطرنج من الفئة العامة jpanel الأدوات mouselistener ، mousemotionListener {public Chesspoint Point [] [] ؛ intwidth العام ، unitheight ؛ طول المحور X الخاص ، طول المحور ص ؛ private int x ، y ؛ صورة خاصة IMG ؛ محمي الصورة قطعة. خطوة منطقية خاصة = خطأ ؛ السلسلة العامة اللون الأحمر المربع = "Red Square" ، اللون الأسود المربع = "Black Square" ؛ CART RED RED CART 1 ، Red CART 2 ، Red Horse 1 ، Red Horse 2 ، Red Horse 1 ، Red Horse 2 ، Red Horse 3 ، Red Soldiers 4 ، Red Soldiers 5 ، Red Cannon 1 ، Red Cannon 2 ؛ Cartpiece Black Cart 1 ، العربة السوداء 2 ، حصان أسود 1 ، Black Horse 2 ، Black General ، Black Soldiers 1 ، Black Soldiers 2 ، Black Soldiers 3 ، Black Soldiers 4 ، Black Soldiers 5 ، Black Elephant 1 ، Black Elephant 2 ، Black Gun 1 ، Black Cannon 2 ؛ int startx ، Starty ؛ int starti ، startJ ؛ شطرنج الجانب الأحمر المنطقي العام = صحيح ، شطرنج سوداء سوداء = خطأ ؛ قاعدة القاعدة = فارغة ؛ سجل makechessmanual العام = فارغ ؛ Public Chessboard (int w ، int h ، int r ، int c) {setLayout (null) ؛ addmouselistener (هذا) ؛ addMouseMotionListener (هذا) ؛ اللون bc = getBackground () ؛ unitwidth = w ؛ unitheight = h ؛ طول المحور x = r ؛ طول المحور y = c ؛ point = new ChessPoint [r + 1] [c + 1] ؛ لـ (int i = 1 ؛ i <= r ؛ i ++) {for (int j = 1 ؛ j <= c ؛ j ++) {point [i] [j] = new chesspoint (i * unitwidth ، j * unitheight ، false) ؛ }} القاعدة = قاعدة جديدة (هذه ، نقطة) ؛ سجل = جديد makechessmanual (هذا ، نقطة) ؛ img = toolkit.getDefaultToolkit (). getImage ("board.jpg") ؛ piecimg = toolkit.getDefaultToolkit (). getImage ("piece.gif") ؛ Red Car 1 = New Chesspiece ("" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Car 1.Set Chess Piece Category (Red Square Color) ؛ Red Car 2 = New Chesspiece ("" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Car 2.Set Chess Piece Category (Red Square Color) ؛ Red Horse 1 = New Chesspiece ("" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Horse 1 = New Chesspiece ("" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Horse 2.Set Chess Piece Category (Red Square Color) ؛ Red Cannon 1 = New Chesspiece ("火" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Cannon 1.Set Chess Piece Category (Red Square Color) ؛ Red Cannon 2 = New Chesspiece ("火" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Cannon 2.Set Chess Piece Category (Red Square Color) ؛ Red Cannon 1 = New Chesspiece ("火" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Cannon 1.Set Chess Piece Category (Red Square Color) ؛ Red Cannon 2 = New Chesspiece ("火" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Cannon 1.Set Chess Piece Category (Red Square Color) ؛ Red Cannon 2 = New Chesspiece ("火" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red 2.Set Chess Piece Category (Red Square Color) ؛ Red 1 = New Chesspiece ("shi" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red 1.Set Chess Piece Category (Red Square Color) ؛ Red 2 = New Chesspiece ("shi" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red 2.Set Chess Piece Category (Red Square Color) ؛ Red 2.Set Chess Piece Category (Red Square Color) ؛ Red 2 = New Chesspiece ("shi" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red 1 = New Chesspiece ("shi" ، color.red ، bc ، w - 4 ، h - 4 ، this) ؛ Red Soldier 1.Set Chess Piece Category (Red Square Color) ؛ Red Soldier 2 = New Chesspiece ("Soldier" ، Color.red ، BC ، W - 4 ، H - 4 ، this) ؛ Red Soldier 2.Set Chess Piece Category (Red Square Color) ؛ Red Soldier 3 = New Chesspiece ("Soldier" ، Color.red ، BC ، W - 4 ، H - 4 ، this) ؛ Red Soldier 3 = New Chesspiece ("Soldier" ، Color.red ، BC ، W - 4 ، H - 4 ، this) ؛ Red Soldier 4. Sett Chess Piece Category (Red Square Color) ؛ Red Soldier 5 = New Chesspiece ("Soldier" ، Color.red ، BC ، W - 4 ، H - 4 ، this) ؛ Red Soldier 5.Set Chess Piece Category (Red Square Color) ؛ Black Soldier = New Chesspiece ("Sold" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Soldier 1 = New Chesspiece ("Soldier" ، Color.black ، BC ، W - 4 ، H - 4 ، this) ؛ Black Soldier 1 = New Chesspiece ("Soldier" ، Color.black ، BC ، W - 4 ، H - 4 ، this) ؛ Black Soldier 1 = New Chesspiece ("Car" ، Color.black ، BC ، W - 4 ، H - 4 ، this) ؛ سيارة سوداء 1. فئة قطعة الشطرنج (لون المربع الأسود) ؛ Black Car 2 = New Chesspiece ("Car" ، Color.black ، BC ، W - 4 ، H - 4 ، this) ؛ سيارة سوداء 2. فئة قطعة الشطرنج (لون سكان أسود) ؛ المدفع الأسود 1 = شطرنج جديد ("cannon" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Cannon 1.Set Chess Piece Category (Black Square Color) ؛ Black Cannon 2 = New Chesspiece ("Cannon" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ أسود مدفع 2. فئة قطعة الشطرنج (لون المربع الأسود) ؛ الفيل الأسود 1 = شطرنج جديد ("Elephant" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Elephant 1.Set Chess Piece Category (Black Square Color) ؛ Black Elephant 2 = New Chesspiece ("Elephant" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ الأسود الفيل 2. فئة قطعة الشطرنج (لون المربع الأسود) ؛ الفيل الأسود 1 = شطرنج جديد ("Elephant" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Elephant 1.Set Chess Piece Category (Black Square Color) ؛ Black Elephant 2 = New Chesspiece ("Elephant" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Horse 2.Set Chess Piece Category (اللون الأسود) ؛ السراويل السوداء 1 = شطرنج جديد ("Zhi" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ سراويل سوداء 1. فئة قطعة الشطرنج (اللون الأسود) ؛ السراويل السوداء 2 = شطرنج جديد ("zhi" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ سراويل سوداء 2. فئة قطعة الشطرنج (اللون الأسود) ؛ السراويل السوداء 3 = شطرنج جديد ("zhi" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ السراويل السوداء 3. فئة قطعة الشطرنج (اللون الأسود) ؛ السراويل السوداء 4 = شطرنج جديد ("Zhi" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Phantom 4. Sett Chess Piece Category (Black Square Color) ؛ Black Phantom 5 = New Chesspiece ("Zhi" ، color.black ، bc ، w - 4 ، h - 4 ، this) ؛ Black Phantom 5. Sett Chess Piece Category (Black Square Color) ؛ النقطة [1] [10] .SetPiece (النقل الأحمر 1 ، هذا) ؛ النقطة [2] [10] .SetPiece (الحصان الأحمر 1 ، هذا) ؛ النقطة [3] [10] .SetPiece (المرحلة الحمراء 1 ، هذا) ؛ النقطة [4] [10] .SetPiece (Red Sage 1 ، هذا) ؛ النقطة [4] [10] .SetPiece (Red Sage 1 ، هذا) ؛ النقطة [4] [10] .SetPiece (Red Sage 1 ، هذا) ؛ النقطة [5] [10] .SetPiece (جندي أحمر ، هذا) ؛ النقطة [6] [10] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [7] [10] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [8] [10] .SetPiece (Red Horse 2 ، this) ؛ النقطة [9] [10] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [2] [8] .SetPiece (جندي أحمر 1 ، هذا) ؛ النقطة [8] [8] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [1] [7] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [3] [7] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [3] [7] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [3] [7] .SetPiece (جندي أحمر 2 ، هذا) ؛ النقطة [5] [7] .SetPiece (جندي أحمر 3 ، هذا) ؛ النقطة [7] [7] .SetPiece (جندي أحمر 4 ، هذا) ؛ النقطة [9] [7] .SetPiece (جندي أحمر 5 ، هذا) ؛ النقطة [1] [1] .SetPiece (السيارة السوداء 1 ، هذا) ؛ النقطة [2] [1] .SetPiece (حصان أسود 1 ، هذا) ؛ النقطة [3] [1] .SetPiece (الفيل الأسود 1 ، هذا) ؛ النقطة [4] [1] .SetPiece (الفيل الأسود 1 ، هذا) ؛ النقطة [5] [1] .SetPiece (الأسود العام ، هذا) ؛ النقطة [6] [1] .SetPiece (الفيل الأسود 1 ، هذا) ؛ النقطة [4] [1] .SetPiece (الأسود العام 1 ، هذا) ؛ النقطة [5] [1] .SetPiece (الأسود العام 2 ، هذا) ؛ النقطة [6] [1] .SetPiece (الأسود العام 2 ، هذا) ؛ النقطة [7] [1] .SetPiece (الفيل الأسود 2 ، هذا) ؛ النقطة [8] [1] .SetPiece (حصان أسود 2 ، هذا) ؛ النقطة [9] [1] .SetPiece (السيارة السوداء 2 ، هذا) ؛ النقطة [2] [3] .SetPiece (المدفع الأسود 1 ، هذا) ؛ النقطة [8] [3] .setpiece (المدفع الأسود 2 ، هذا) ؛ النقطة [1] [4] .SetPiece (المدفع الأسود 1 ، هذا) ؛ النقطة [3] [4] .SetPiece (المدفع الأسود 2 ، هذا) ؛ النقطة [5] [4] .SetPiece (المدفع الأسود 3 ، هذا) ؛ النقطة [7] [4] .SetPiece (المدفع الأسود 4 ، هذا) ؛ النقطة [7] [4] .SetPiece (المدفع الأسود 4 ، هذا) ؛ النقطة [3] [4] .SetPiece (المدفع الأسود 4 ، هذا) ؛ النقطة [5] [4] .SetPiece (المدفع الأسود 3 ، هذا) ؛ النقطة [7] [4] .SetPiece (المدفع الأسود 4 ، هذا) ؛ النقطة [9] [4] .SetPiece (محفظة أسود 5 ، هذا) ؛ } paintComponent paintcomponent (الرسومات G) {super.paintcomponent (g) ؛ int imgwidth = img.getWidth (هذا) ؛ int imgheight = img.getheight (هذا) ؛ // احصل على عرض وارتفاع الصورة int fwidth = getWidth () ؛ int fheight = getheight () ؛ // احصل على عرض وارتفاع النافذة int x = (fwidth - imgwidth)/ 2 ؛ int y = (fheight - imgheight) / 2 ؛ G.DrawImage (IMG ، X ، Y ، Null) ؛ لـ (int j = 1 ؛ j <= y-axis length ؛ j ++) {g.drawline (point [1] [j] .x ، point [1] [j] } لـ (int i = 1 ؛ i <= طول المحور x ؛ i ++) {if (i! = 1 && i! = طول المحور x) {g.drawline (point [i] [1] .x ، point [i] [1] g.drawline (نقطة [i] [y-axis length-4] .x ، point [i] [طول المحور y-4] .y ، نقطة [i] [طول المحور y] .x ، النقطة [i] [طول المحور y] .y) ؛ } آخر {g.drawline (نقطة [i] [1] .x ، النقطة [i] [1] .y ، نقطة [i] [طول المحور y] .x ، نقطة [i] }} g.drawline (نقطة [4] [1] .x ، النقطة [4] [1] .y ، نقطة [6] [3]. G.Drawline (نقطة [6] [1] .x ، النقطة [6] [1] G.Drawline (Point [4] [8] .x ، Point [4] [8] G.Drawline (نقطة [4] [طول المحور ص] .x ، النقطة [4] [طول المحور ص] .y ، النقطة [6] [8]. من أجل (int i = 1 ؛ i <= length moxis ؛ i ++) {g.drawString (""+i ، i * unitwidth ، unitheight / 2) ؛ } int j = 1 ؛ لـ (char c = 'a' ؛ c <= 'j' ؛ c ++) {g.drawString (""+c ، unitwidth / 4 ، j * unitheight) ؛ J ++ ؛ }} / ** mouse press event* / public void mousepressed (mouseevent e) {chesspiece piece = null ؛ المستطيل المستقيم = فارغ ؛ if (e.getSource () == this) move = false ؛ if (move == false) if (e.GetSource () extryof chesspiece) {piece = (chesspiece) e.GetSource () ؛ startx = piece.getBounds (). x ؛ starty = piece.getBounds (). y ؛ rect = piece.getBounds () ؛ لـ (int i = 1 ؛ i <= طول المحور x ؛ i ++) {for (int j = 1 ؛ j <= y-axis length ؛ j ++) {int x = point [i] [j] .getx () ؛ int y = point [i] [j] .gety () ؛ if (rect.contains (x ، y)) {starti = i ؛ startJ = j ؛ استراحة؛ }}}}}} public void mouseMoved (mouseevent e) {} / ** mouse regh event* / public void mousedragged (mouseevent e) {chesspiece piece = null ؛ if (e.GetSource () مثيل Chesspiece) {piece = (chesspiece) e.GetSource () ؛ نقل = صحيح ؛ e = swingutability.convertmousevent (قطعة ، e ، هذا) ؛ } if ( y = e.gety () ؛ if (Red Square Chess && ((piece.chess subcategory ()). يساوي (لون مربع أحمر))) {piece.setLocation (x - piece.getWidth () / 2 ، y - piece.getheight () / 2) ؛ } if (Move Black Chess Move && (piece.chess class (). يساوي (لون الخط الأسود))) {piece.setLocation (x - piece.getWidth () / 2 ، y - piece.getheight () / 2) ؛ }}}} / ** حدث ماوس إطلاق* / public void mousereleSeped (mouseevent e) {chesspiece piece = null ؛ نقل = خطأ ؛ المستطيل المستقيم = فارغ ؛ if (e.GetSource () extryof chesspiece) {piece = (chesspiece) e.GetSource () ؛ rect = piece.getBounds () ؛ e = swingutability.convertmousevent (قطعة ، e ، هذا) ؛ } if ( int x = 0 ، y = 0 ؛ int m = 0 ، n = 0 ؛ if (piece! = null) {for (int i = 1 ؛ i <= length-axis length ؛ i ++) {for (int j = 1 ؛ j <= y-axis length ؛ j ++) {x = point [i] [j] .getx () ؛ y = point [i] [j] .gety () ؛ if (rect.contains (x ، y)) {continChessPoint = true ؛ م = أنا ؛ ن = ي ؛ استراحة؛ }}} ستر if (point [m] [n] .ispiece ()) {color c = (point [m] [n] .getPiece ()). الحصول على لون الشطرنج () ؛ if (pitcecolor.getrgb () == c.getrgb ()) {piece.setLocation (startx ، starty) ؛ (النقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (صواب) ؛ } آخر {boolean ok = rule.movepiecerule (قطعة ، starti ، startJ ، m ، n) ؛ if (ok) {chesspiece piceremoved = point [m] [n] .getPiece () ؛ النقطة [m] [n] .removepiece (pitceremoved ، هذا) ؛ النقطة [m] [n] .setpiece (قطعة ، هذا) ؛ (نقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (خطأ) ؛ Record.Record Chess Score (Piece ، Starti ، StartJ ، M ، N) ؛ record.record قطع الشطرنج المصابة (piceremoved) ؛ rule.iswine (pitceremoved) ؛ if (piece.Paper Category (). يساوي (لون مربع أحمر)) {Red Square Move = false ؛ تحرك المربع الأسود = صحيح ؛ } if (piece.paper category (). يساوي (لون مربع أسود)) {أسود مربع الحركة = false ؛ نقل مربع أحمر = صحيح ؛ } التحقق () ؛ REPAINT () ؛ } آخر {piece.setLocation (startx ، starty) ؛ (النقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (صواب) ؛ }}} else {boolean ok = rule .movepiecerule (piece ، starti ، startJ ، m ، n) ؛ if (ok) {point [m] [n] .setpiece (قطعة ، هذا) ؛ (نقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (خطأ) ؛ Record.Record Chess Score (Piece ، Starti ، StartJ ، M ، N) ؛ سجلات الشطرنج. if (piece.Paper Category (). يساوي (لون مربع أحمر)) {Red Square Chess = false ؛ الشطرنج المربع الأسود = صحيح ؛ } if (piece.paper category (). يساوي (لون مربع أسود)) {Black Square Chess = false ؛ شطرنج مربع أحمر = صحيح ؛ }} آخر {piece.setLocation (startx ، starty) ؛ (النقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (صواب) ؛ }}} if (piece! = null &&! conteChessPoint) {piece.setLocation (startx ، starty) ؛ (النقطة [starti] [startJ]). مجموعة تحتوي على قطع شطرنج (صواب) ؛ }}} public void mouseentered (mouseevent e) {} public void mouseexited (mouseevent e) {} public void mouseclicked (mouseevent e) {}} 3. ملف Chesspiece.java
حزمة cn.edu.ouc.Chinesechess ؛ استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ / ** * فئة الشطرنج * * author cnlht */ class public chesspiece يمتد Jlabel {string name ؛ // شطرنج اسم اللون backcolor = null ، preportoLor ؛ // الخلفية وفئة ألوان سلسلة الألوان المقدمة = null ؛ لوحة الشطرنج = فارغة ؛ عرض int ، الارتفاع ؛ // size public chesspiece (اسم السلسلة ، اللون FC ، اللون قبل الميلاد ، عرض int ، ارتفاع int ، لوحة الشطرنج) {// إنشاء قطعة الشطرنج this.name = name ؛ this.board = لوحة ؛ this.width = العرض ؛ this.height = الارتفاع ؛ التنبؤ = fc ؛ backcolor = bc ؛ setSize (العرض ، الارتفاع) ؛ انتكاسة (BC) ؛ AddMouseMotionListener (المجلس) ؛ addmouselistener (المجلس) ؛ }. G.SetColor (PreportOlor) ؛ G.SetFont (خط جديد ("Kaiti" ، font.bold ، 26)) ؛ G.DrawString (الاسم ، 7 ، الارتفاع - 8) ؛ // ارسم "اسم الشطرنج" على قطعة الشطرنج G.SetColor (color.black) ؛ //g.drawoval(1 ، 1 ، العرض - 1 ، الارتفاع - 1) ؛ عرض الطرف العائم = 2.3F ؛ ((Graphics2d) g) .SetStroke (BasicStroke (LineWidth)) ؛ ((graphics2d) g) .drawoval (2 ، 2 ، العرض -2 ، الارتفاع -2) ؛ } public int getWidth () {return width ؛ } public int getheight () {return height ؛ } السلسلة العامة getName () {return name ؛ } اللون العام Get the Chess Color () {return preportoLor ؛ } فئة شطرنج مجموعة الفراغ العامة (فئة السلسلة) {Color Category = Category ؛ } فئة الشطرنج العامة () }} 4. ملف تنسيق نقطة الشطرنج
حزمة cn.edu.ouc.Chinesechess ؛ / *** فئة نقطة الشطرنج** Author cnlht*/ الفئة العامة ChessPoint {/ ** chesspiece coordinates*/ int x ، y ؛ / ** هل يحتوي هذا الإحداثي على قطعة*/ منطقية لها قطعة ؛ /** قطعة الشطرنج = فارغة ؛ / ** الشطرنج الذي ينتمي إليه الإحداثي*/ board chessboard = null ؛ Public ChessPoint (int x ، int y ، boolean boo) {this.x = x ؛ this.y = y ؛ لديك قطعة = بوو ؛ } ispiece boolean العامة () {return هناك قطع شطرنج ؛ } مجموعة الفراغ العامة تحتوي على قطع شطرنج (Boolean Boo) {شطرنج القطع = boo ؛ } public int getx () {return x ؛ } public int gety () {return y ؛ }. this.piece = قطعة ؛ board.add (قطعة) ؛ int w = (board.unitwidth) ؛ int h = (board.unitheight) ؛ piece.setBounds (x - w / 2 ، y - h / 2 ، w ، h) ؛ // موقف قطعة الشطرنج ، عرض ، ارتفاع مع قطع الشطرنج = صواب ؛ المجلس. } public chesspiece getPiece () {return piece ؛ } public void removePiece (قطعة الشطرنج ، لوحة الشطرنج) {this.board = board ؛ this.piece = قطعة ؛ board.remove (قطعة) ؛ المجلس. قطع الشطرنج = خطأ ؛ }} 5.
حزمة cn.edu.ouc.Chinesechess ؛ استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ / ** * فئة قواعد لعب الشطرنج * * Author Cnlht */ قاعدة الفئة العامة {Chessboard Board = null ؛ قطعة الشطرنج = فارغة ؛ نقطة الشطرنج [] [] ؛ int starti ، startJ ، endi ، endj ؛ القاعدة العامة (لوحة الشطرنج ، نقطة الشطرنج [] []) {this.board = board ؛ this.point = point ؛ } public void iswine (piece chesspiece) {this.piece = piece ؛ if (piece.getName () == "محاذاة" || piece.getName () == "Handy") {if (piece.color category == "Red Side") {joptionpane.showmessagedialog (null ، "Black Side Vicory!") ؛ } آخر {joptionpane.showmessagedialog (NULL ، "Red Side Victory!") ؛ }}} boolean movepiecerule (قطعة الشريان ، int starti ، int startJ ، int endi ، int endj) {this.piece = piece ؛ this.starti = starti ؛ this.startj = startJ ؛ this.endi = endi ؛ this.endj = endj ؛ int mini = math.min (starti ، endi) ؛ int maxi = math.max (starti ، endi) ؛ int minj = math.min (startJ ، endj) ؛ int maxj = math.max (startJ ، endj) ؛ Boolean هل يمكنك لعب الشطرنج = خطأ ؛ if (piece.getName (). equals ("Car")) {if (starti == endi) {int j = 0 ؛ لـ (j = minj+1 ؛ j <= maxj - 1 ؛ j ++) {if (point [starti] [j] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ استراحة؛ }} if (j == maxj) {هل يمكنك تحريك chess = true ؛ }} آخر إذا (startJ == endj) {int i = 0 ؛ لـ (i = mini+1 ؛ i <= maxi - 1 ؛ i ++) {if (point [i] [startJ] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ استراحة؛ }} if (i == maxi) {هل يمكنك تحريك chess = true ؛ }} آخر {هل يمكنك تحريك الشطرنج = false ؛ }}} آخر إذا (piece.getName (). يساوي ("")) {if (starti == endi) {int j = 0 ؛ لـ (j = minj+1 ؛ j <= maxj - 1 ؛ j ++) {if (point [starti] [j] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ استراحة؛ }} if (j == maxj) {هل يمكنك تحريك chess = true ؛ }} آخر إذا (startJ == endj) {int i = 0 ؛ لـ (i = mini+1 ؛ i <= maxi - 1 ؛ i ++) {if (point [i] [startJ] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ استراحة؛ }} if (i == maxi) {هل يمكنني تحريك الشطرنج = صحيح ؛ }} آخر {هل يمكنني تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("ma")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (xaxle == 2 && yaxle == 1) {if (endi> starti) {if (point [starti + 1] [startJ] .ispiece ()) {هل يمكنني تحريك الشطرنج = false ؛ } آخر {هل يمكنني تحريك الشطرنج = صحيح ؛ }} if (endi <starti) {if (point [starti - 1] [startJ] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }}} آخر إذا (xaxle == 1 && yaxle == 2) {if (endj> startJ) {if (point [starti] [startJ + 1] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} if (endj <startJ) {if (point [starti] [startJ - 1] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} if (endj <startJ) {if (point [starti] [startJ - 1] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ } حقيقي؛ }}} آخر {هل يمكنني تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (xaxle == 2 && yaxle == 1) {if (endi> starti) {if (point [starti + 1] [startJ] .ispiece ()) {هل يمكنني تحريك الشطرنج = false ؛ } آخر {هل يمكنني تحريك الشطرنج = صحيح ؛ }} if (endi <starti) {if (point [starti - 1] [startJ] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }}} آخر إذا (xaxle == 1 && yaxle == 2) {if (endj> startJ) {if (point [starti] [startJ + 1] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} if (endj <startJ) {if (point [starti] [startJ - 1] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }}} آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("أيقونة")) {int centeri = (starti + endi) / 2 ؛ int centerJ = (startJ + endj) / 2 ؛ int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (xaxle == 2 && yaxle == 2 && endj <= 5) {if (point [centeri] [centerj] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("المرحلة")) {int centeri = (starti + endi) / 2 ؛ int centerJ = (startJ + endj) / 2 ؛ int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (xaxle == 2 && yaxle == 2 && endj> = 6) {if (point [centeri] [centerj] .ispiece ()) {هل يمكنك تحريك الشطرنج = false ؛ } آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("Cannon")) {int number = 0 ؛ if (starti == endi) {int j = 0 ؛ لـ (j = minj+1 ؛ j <= maxj - 1 ؛ j ++) {if (point [starti] [j] .ispiece ()) {number ++ ؛ }} if (number> 1) {هل يمكنك تحريك الشطرنج = false ؛ } if if (number == 1) {if (point [endi] [endj] .ispiece ()) {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر إذا (number == 0 &&! point [endi] [endj] .ispiece ()) {هل يمكنك تحريك الشطرنج = true ؛ }} آخر إذا (startJ == endj) {int i = 0 ؛ لـ (i = mini+1 ؛ i <= maxi - 1 ؛ i ++) {if (point [i] [startJ] .ispiece ()) {number ++ ؛ }} if (number> 1) {هل يمكنك تحريك الشطرنج = false ؛ } if if (number == 1) {if (point [endi] [endj] .ispiece ()) {هل يمكنك تحريك الشطرنج = صحيح ؛ }} آخر إذا (number == 0 &&! point [endi] [endj] .ispiece ()) {هل يمكنك تحريك الشطرنج = true ؛ }} آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("soldier")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (endj> = 6) {if (startJ - endj == 1 && xaxle == 0) {هل يمكنك تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر إذا (endj <= 5) {if ((startJ - endj == 1) && (xaxle == 0)) {هل يمكنني تحريك الشطرنج = true ؛ } آخر إذا ((endj - startJ == 0) && (xaxle == 1)) {هل يمكنني تحريك الشطرنج = true ؛ } آخر {هل يمكنني تحريك الشطرنج = false ؛ }}} آخر if (piece.getName (). يساوي ("zhi")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (endj <= 5) {if (endj - startJ == 1 && xaxle == 0) {هل يمكنني تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر إذا (endj> = 6) {if ((endj - startJ == 1) && (xaxle == 0)) {هل يمكنك تحريك الشطرنج = true ؛ } آخر إذا ((endj - startJ == 0) && (xaxle == 1)) {هل يمكنك تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }}} آخر if (piece.getName (). يساوي ("shi")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {هل يمكنك تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if (piece.getName (). يساوي ("الحالة")) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {هل يمكنك تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر if ((piece.getName (). يساوي ("وسيم")) || (piece.getName (). equals ("reply"))) {int xaxle = math.abs (starti - endi) ؛ int yaxle = math.abs (startJ - endj) ؛ if (endi <= 6 && endi> = 4) {if ((xaxle == 1 && yaxle == 0) || (xaxle == 0 && yaxle == 1)) {هل يمكنك تحريك الشطرنج = true ؛ } آخر {هل يمكنك تحريك الشطرنج = false ؛ }} آخر {هل يمكنك تحريك الشطرنج = false ؛ }} الإرجاع هل يمكنك تحريك الشطرنج ؛ }} 6. ملف movestep.java
حزمة cn.edu.ouc.Chinesechess ؛ استيراد java.awt.point ؛ / ** * فئة المشي * * Author cnlht * */ الطبقة العامة movestep تنفذ java.io.serializable {public point pstart ، pend ؛ MOVESTEP العام (النقطة P1 ، النقطة P2) {pStart = p1 ؛ Pend = p2 ؛ }} 7. جعل Chessmanual.java
حزمة cn.edu.ouc.Chinesechess ؛ استيراد javax.swing.*؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد java.util.linkedList ؛ / ** * جعل سجل الشطرنج فئة * * Author Cnlht */ الفئة العامة makechessmanual يمتد jpanel actionlistener {jtextarea text = null ؛ JScrollpane Scroll = null ؛ لوحة الشطرنج = فارغة ؛ chesspoint [] [] point ؛ LinkedList chess note = null ؛ LinkedList يأكل الشطرنج القطع = فارغة ؛ Jbutton Buttonundo ؛ int i = 0 ؛ MakeChessManual العامة (Board Chessboard ، ChessPoint [] [] point) {this.board = board ؛ this.point = point ؛ النص = new JTextAREA () ؛ Scroll = New JScrollPane (text) ؛ ملاحظة الشطرنج = new LinkedList () ؛ قطع الشطرنج المأكل = New LinkedList () ؛ Buttonundo = New JButton ("توب شطرنج") ؛ Buttonundo.setfont (خط جديد ("Lishu" ، font.plain ، 18)) ؛ setLayout (New BorderLayout ()) ؛ إضافة (Scroll ، BorderLayout.Center) ؛ إضافة (buttonundo ، borderlayout.south) ؛ buttonundo.addActionListener (this) ؛ } char numbertoletter (int n) {char c = '/0' ؛ التبديل (n) {الحالة 1: c = 'a' ؛ استراحة؛ الحالة 2: C = 'B' ؛ استراحة؛ الحالة 3: C = 'c' ؛ استراحة؛ الحالة 4: C = 'D' ؛ استراحة؛ الحالة 5: C = 'e' ؛ استراحة؛ الحالة 6: c = 'f' ؛ استراحة؛ الحالة 7: C = 'g' ؛ استراحة؛ الحالة 8: C = 'H' ؛ استراحة؛ الحالة 9: c = 'i' ؛ استراحة؛ الحالة 10: c = 'j' ؛ استراحة؛ } return c ؛ } نقاط الشطرنج السجلية الفراغية العامة (PIET PIET ، int starti ، int startJ ، int endi ، int endj) {point pstart = new point (starti ، startJ) ؛ Point Pend = نقطة جديدة (Endi ، endj) ؛ خطوة movestep = new movestep (pstart ، pend) ؛ درجة الشطرنج. add (الخطوة) ؛ سلسلة الشطرنج = Piece.Chess Category () ؛ اسم السلسلة = piece.getName () ؛ String m = "#" + فئة الشطرنج + الاسم + ": text.append (m) ؛ إذا (قطعة. الشطرنج فئة () } public void سجل قطعة الشطرنج التي تم تناولها (كائن كائن) {قطعة شطرنج. } LinkedList Public Get the Chess Piece () {Return Chess Piece ؛ } public void actionperformed (ActionEvent e) {int position = text.getText (). lastIndExof ("#") ؛ إذا (الموضع! = -1) text.replacerange ("" ، الموضع ، text.getText (). length ()) ؛ if (chess piece.size ()> 0) {movestep lastStep = (movestep) chess.getlast () ؛ chess record.removelast () ؛ كائن qizi = قطعة الشطرنج. getLast () ؛ قطعة الشطرنج المأكل. removelast () ؛ سلسلة temp = qizi.toString () ؛ if (temp.equals ("no chess piece")) {int starti = lastStep.pstart.x ؛ int startJ = lastStep.pstart.y ؛ int endi = lastStep.Pend.x ؛ int endj = lastStep.Pend.y ؛ قطعة الشطرنج = نقطة [endi] [endj] .getPiece () ؛ النقطة [starti] [startJ] .setpiece (قطعة ، لوحة) ؛ (نقطة [endi] [endj]). مجموعة تحتوي على قطع شطرنج (خطأ) ؛ if (piece.Chess Piece Category (). equals (board.red square color)) {board.red square move = true ؛ Board.Black Square Move = false ؛ } if (piece.Chess Piece Category (). equals (board.black square color)) {board.black square move = true ؛ Board.red Square Move = false ؛ }} else {chesspiece removePpiece = (chesspiece) qizi ؛ int starti = lastStep.pstart.x ؛ int startJ = lastStep.pstart.y ؛ int endi = lastStep.Pend.x ؛ int endj = lastStep.Pend.y ؛ قطعة الشطرنج = نقطة [endi] [endj] .getPiece () ؛ النقطة [starti] [startJ] .setpiece (قطعة ، لوحة) ؛ point[endI][endJ].setPiece(removedPiece, board); (point[endI][endJ]).set has chess pieces(true); if (piece.Paper category().equals(board.Red square color)) { board.Red square move = true; board.Black square move = false; } if (piece.Paper Category().equals(board.Black Square Color)) { board.Black Square Chess = true; board.Red Square Chess = false; } } } } } } 8.演示棋谱类文件Demon.java
package cn.edu.ouc.chineseChess; import javax.swing.*; import java.awt.*; import java.awt.event.*; استيراد java.util.*؛ /** * Demo chess class* * @author cnlht */ public class Demon extends JPanel implements ActionListener, Runnable { public JButton replay = null, next = null, auto = null, stop = null; LinkedList chess = null; Thread Automatic demonstration = null; int index = -1; ChessBoard board = null; JTextArea text; JTextField Time interval= null; int time = 1000; String Demo process = ""; JSplitPane splitH = null, splitV = null; public Demon(ChessBoard board) { this.board = board; replay = new JButton("Redemo"); next = new JButton("Next"); auto = new JButton("Automatic Demo"); stop = new JButton("Pause Demo"); Automatic Demo= new Thread(this); replay.addActionListener(this); next.addActionListener(this); auto.addActionListener(this); stop.addActionListener(this); text = new JTextArea(); Time interval = new JTextField("1"); setLayout(new BorderLayout()); JScrollPane pane = new JScrollPane(text); JPanel p = new JPanel(new GridLayout(3, 2)); p.add(next); p.add(replay); p.add(auto); p.add(stop); p.add(new JLabel("Time interval (seconds), SwingConstants.CENTER)); p.add(time interval); splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, p); splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, board, splitV); splitV.setDividerSize(5); splitV.setDividerLocation(400); splitH.setDividerSize(5); splitH.setDividerLocation(460); add(splitH, BorderLayout.CENTER); validate(); } public void set chess score(LinkedList chess score) { this. chess score= chess score; } public char numberToLetter(int n) { char c = '/0'; switch (n) { case 1: c = 'A'; break; case 2: c = 'B'; break; case 3: c = 'C'; break; case 4: c = 'D'; break; case 5: c = 'E'; break; case 6: c = 'F'; break; case 7: c = 'G'; break; case 8: c = 'H'; break; case 9: c = 'I'; break; case 10: c = 'J'; break; } return c; } public void actionPerformed(ActionEvent e) { if (e.getSource() == next) { index++; if (index < chess score.size()) { demonstration step(index); } else { demonstration end ("chess score demonstration completed"); } } if (e.getSource() == replay) { board = new ChessBoard(45, 45, 9, 10); splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); splitH.setLeftComponent(board); splitH.validate(); index = -1; text.setText(null); } if (e.getSource() == auto) { next.setEnabled(false); replay.setEnabled(false); try { time = 1000 * Integer.parseInt(Time interval.getText().trim()); } catch (NumberFormatException ee) { time = 1000; } if (!(automatic demonstration.isAlive())) {automatic demonstration = new Thread(this); board = new ChessBoard(45, 45, 9, 10); splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); splitH.setLeftComponent(board); splitH.validate(); text.setText(null); automatic demonstration.start(); } } if (e.getSource() == stop) { if (e.getActionCommand().equals("Pause Demo")) { Demo process = "Pause Demo"; stop.setText("Continue Demo"); stop.repaint(); } if (e.getActionCommand().equals("Continue Demo")) { Demo process = "Continue Demo"; Automatic demonstration.interrupt(); stop.setText("Pause Demo"); stop.repaint(); } } } public synchronized void run() { for (index = 0; index < chess score.size(); index++) { try { Thread.sleep(time); } catch (InterruptedException e) { } while (Demo process.equals("Pause demo")) { try { wait(); } catch (InterruptedException e) { notifyAll(); } } Demo step(index); } if (index >= Chess Note.size()) { Demo end("Chess Note demonstration completed"); next.setEnabled(true); replay.setEnabled(true); } } public void Demo step(int index) { MoveStep step = (MoveStep) Chess Note.get(index); Point pStart = step.pStart; Point pEnd = step.pEnd; int startI = pStart.x; int startJ = pStart.y; int endI = pEnd.x; int endJ = pEnd.y; ChessPiece piece = (board.point)[startI][startJ].getPiece(); if ((board.point)[endI][endJ].isPiece() == true) { ChessPiece pieceRemoved = (board.point)[endI][endJ].getPiece(); (board.point)[endI][endJ].reMovePiece(pieceRemoved, board); board.repaint(); (board.point)[endI][endJ].setPiece(piece, board); (board.point)[startI][startJ].set has chess pieces (false); board.repaint(); } else { (board.point)[endI][endJ].setPiece(piece, board); (board.point)[startI][startJ].set has chess pieces (false); } String chess piece category = piece.chess piece category(); String name = piece.getName(); String m = "#" + chess class + name + ": " + startI + numberToLetter(startJ) + " to " + endI + numberToLetter(endJ); text.append(m); if (piece. chess class().equals(board.black square color)) text.append("/n"); } public void demonstration end(String message) { splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); JLabel label = new JLabel(message); label.setFont(new Font("Lishu", Font.BOLD, 40)); label.setForeground(Color.blue); label.setHorizontalAlignment(SwingConstants.CENTER); splitH.setLeftComponent(label); splitH.validate(); } }四、总结与要求
1.理解8个文件,没有太复杂的代码。
2.理解鼠标的MouseListener,MouseMotionListener两个接口的区别,五子棋的实现不需要MouseMotionListener。
3.使用LinkedList记录棋谱的方法。
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。