บทความนี้อธิบายถึงรหัสเกมหมากรุกจีนที่ใช้โดย Java มันถูกแชร์กับคุณสำหรับการอ้างอิงของคุณ รหัสเฉพาะมีดังนี้
1. จุดประสงค์ในทางปฏิบัติ:
1. แอปพลิเคชันและความแตกต่างระหว่างการคลิกเมาส์ลากและกิจกรรมอื่น ๆ
2. การบันทึกและอ่านไฟล์บันทึกหมากรุก
3. ปรับปรุงกฎของหมากรุก
2. เนื้อหาภาคปฏิบัติ:
หมากรุกจีนมีประวัติศาสตร์อันยาวนานและดึงดูดผู้คนนับไม่ถ้วนให้ศึกษา ตอนนี้เราได้ทำการออกแบบและคำอธิบายต่อไปนี้เกี่ยวกับการต่อสู้ของหมากรุกจีนและการผลิตคะแนนหมากรุกสำหรับการอ้างอิงและการเรียนรู้ของคุณ
1. เมื่อเล่นหมากรุกระหว่างเครื่องจักรและเครื่องจักรด้านสีแดงจะเป็นผู้นำ ลากชิ้นหมากรุกไปยังปลายทางหากตรงตามกฎและกระรอกจะลดลง
ทุกคนเล่นหมากรุก
2. ทำบันทึกหมากรุก หลังจากเลือกเมนูบันทึกหมากรุกเกมเริ่มต้นและบันทึกกระบวนการหมากรุก
เลือกเมนู "Make Record"
ด้านสีแดงชนะหลังจากคะแนนหมากรุกเสร็จสิ้น
หลังจากชนะด้านหนึ่งกล่องโต้ตอบข้อความชัยชนะจะปรากฏขึ้น หลังจากคลิกตกลงเลือกเมนู "บันทึกการบันทึกหมากรุก" และกล่องโต้ตอบบันทึกไฟล์จะปรากฏขึ้น
บันทึกกล่องโต้ตอบบันทึกหมากรุก
3. แสดงคะแนนหมากรุก หลังจากเลือกเมนูคะแนนหมากรุกสาธิตกล่องโต้ตอบแบบเปิดจะปรากฏขึ้นเลือกคะแนนหมากรุกที่บันทึกไว้เพื่อเริ่มการสาธิต
สาธิตกล่องโต้ตอบบันทึกหมากรุก
แสดงกระบวนการบันทึกหมากรุก (อัตโนมัติและคู่มือ)
3. รหัสอ้างอิง:
1. หมากรุกไฟล์หลัก Chinesechess.java
แพ็คเกจ cn.edu.ouc.chinesechess; นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.*; นำเข้า Java.io.*; นำเข้า java.util.linkedList; / ** * หมากรุกคลาสหลัก * * @author cnlht */ คลาสสาธารณะ Chinesechess ขยาย jframe ใช้ actionListener {กระดานหมากรุก = null; ปีศาจปีศาจ = null; MakechessManual Record = NULL; คอนเทนเนอร์ con = null; Jmenubar Bar; Jmenu Filemenu; Jmenuitem ทำคะแนนหมากรุกบันทึกคะแนนหมากรุกแสดงให้เห็นถึงคะแนนหมากรุก; JFilechooser filechooser = null; LinkedList Chess score = null; Public Chinesechess () {bar = new JMenubar (); filemenu = ใหม่ jmenu ("หมากรุกจีน"); ทำคะแนนหมากรุก = ใหม่ jmenuitem ("ทำคะแนนหมากรุก"); บันทึกคะแนนหมากรุก = ใหม่ JMenuitem ("บันทึกคะแนนหมากรุก"); บันทึกคะแนนหมากรุก setenabled (เท็จ); การสาธิตหมากรุกคะแนน = JMENUITEM ใหม่ ("คะแนนหมากรุกสาธิต"); filemenu.add (ทำคะแนนหมากรุก); filemenu.add (บันทึกคะแนนหมากรุก); filemenu.add (แสดงคะแนนหมากรุก); bar.add (filemenu); setjmenubar (บาร์); Settitle (ทำ chess score.getText ()); ทำคะแนนหมากรุก AddactionListener (นี้); บันทึกคะแนนหมากรุก AddactionListener (นี้); แสดงให้เห็นถึงคะแนนหมากรุก AddactionListener (นี้); บอร์ด = กระดานหมากรุกใหม่ (45, 45, 9, 10); บันทึก = บอร์ดบันทึก; con = getContentPane (); JSPlitPane Split = ใหม่ JSPlitPane (JSPlitPane.horizontal_split, จริง, บอร์ด, บันทึก); split.setDividerSize (5); split.setDividerLocation (460); con.add (แยก, Borderlayout.center); AddWindowListener (WindowAdapter ใหม่ () {โมฆะสาธารณะ Windowclosing (WindowEvent E) {System.Exit (0);}}); setVisible (จริง); Setbounds (60, 20, 690, 540); filechooser = jfilechooser ใหม่ (); Con.validate (); ตรวจสอบความถูกต้อง(); } โมฆะสาธารณะ ActionPerformed (ActionEvent E) {ถ้า (e.getSource () == ทำคะแนนหมากรุก) {con.removeAll (); บันทึกคะแนนหมากรุก Setenabled (จริง); this.settitle (สร้าง Chess score.getText ()); บอร์ด = กระดานหมากรุกใหม่ (45, 45, 9, 10); บันทึก = บอร์ดบันทึก; JSPlitPane Split = ใหม่ JSPlitPane (JSPlitPane.horizontal_split, จริง, บอร์ด, บันทึก); split.setDividerSize (5); split.setDividerLocation (460); con.add (แยก, Borderlayout.center); ตรวจสอบความถูกต้อง(); } if (e.getSource () == บันทึกคะแนนหมากรุก) {int state = filechooser.showsavedialog (null); ไฟล์ saveFile = fileChooser.getSelectedFile (); if (savefile! = null && state == jfilechooser.approve_option) {ลอง {fileOutputStream outone = ใหม่ fileOutputStream (saveFile); ObjectOutputStream Outtwo = New ObjectOutputStream (Outone); Outtwo.writeObject (record.get Get Chess คะแนน ()); Outone.close (); Outtwo.close (); } catch (เหตุการณ์ ioexception) {}}} ถ้า (e.getSource () == แสดงคะแนนหมากรุก) {con.removeAll (); Con.repaint (); Con.validate (); ตรวจสอบความถูกต้อง(); บันทึกคะแนนหมากรุก setenabled (เท็จ); สถานะ int = fileChooser.ShowOpendialog (null); ไฟล์ openFile = fileChooser.getSelectedFile (); if (openFile! = null && state == jfilechooser.approve_option) {ลอง {fileInputStream inone = new FileInputStream (OpenFile); ObjectInputStream intwo = ใหม่ ObjectInputStream (inone); chess = (linkedList) intwo.readObject (); inone.close (); intwo.close (); กระดานหมากรุก = กระดานหมากรุกใหม่ (45, 45, 9, 10); ปีศาจ = ปีศาจใหม่ (บอร์ด); Demon.Set Record (บันทึกหมากรุก); Con.add (ปีศาจ, Borderlayout.center); Con.validate (); ตรวจสอบความถูกต้อง(); this.settitle (Demo Chess Record.getText () + ":" + OpenFile); } catch (เหตุการณ์ข้อยกเว้น) {jlabel label = new JLabel ("ไม่ใช่ไฟล์บันทึกหมากรุก"); label.setFont (ตัวอักษรใหม่ ("Lishu", Font.Bold, 60)); label.setforeground (color.red); Label.sethorizontalalignment (SwingConstants.Center); con.add (ฉลาก, BorderLayout.Center); Con.validate (); this.settitle ("ไม่มีบันทึกหมากรุก"); ตรวจสอบความถูกต้อง(); }} else {jlabel label = new JLabel ("ไม่มีไฟล์บันทึกหมากรุกไม่เปิด"); label.setFont (ตัวอักษรใหม่ ("Lishu", Font.Bold, 50)); label.setforeground (color.pink); Label.sethorizontalalignment (SwingConstants.Center); con.add (ฉลาก, BorderLayout.Center); Con.validate (); this.settitle ("ไฟล์หมากรุกไม่ได้เปิด"); ตรวจสอบความถูกต้อง(); }}} โมฆะคงที่สาธารณะหลัก (สตริง args []) {chinesechess ใหม่ (); - 2. Fessboard.java ไฟล์
แพ็คเกจ cn.edu.ouc.chinesechess; นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.*; / ** * คลาสบอร์ดบอร์ด * * @author cnlht */ กระดานหมากรุกระดับสาธารณะขยาย JPanel ใช้ Mouselistener, MousemotionListener {จุดหมากรุกสาธารณะ [] []; Public Int Unitwidth, Unitheight; ความยาวแกน x ส่วนตัวความยาวแกน y; ส่วนตัว int x, y; ภาพส่วนตัว IMG; Pieceimg ภาพที่ได้รับการป้องกัน บูลีนส่วนตัวย้าย = เท็จ; สตริงสาธารณะสีแดงสี่เหลี่ยม = "สี่เหลี่ยมจัตุรัสสีแดง", สีดำสี่เหลี่ยม = "สแควร์ดำ"; Chesspiece 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; Chesspiece Black Cart 1, Black Cart 2, Black Horse 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; Public Boolean Red Side หมากรุก = จริงหมากรุกด้านข้างสีดำ = เท็จ กฎกฎ = null; บันทึกสาธารณะ makechessanual = null; กระดานหมากรุกสาธารณะ (int w, int h, int r, int c) {setlayout (null); addmouselistener (นี่); addmousemotionListener (นี้); สี bc = getbackground (); Unitwidth = W; Unitheight = H; ความยาวแกน x = r; ความยาวแกน y = c; point = chesspoint ใหม่ [r + 1] [c + 1]; สำหรับ (int i = 1; i <= r; i ++) {สำหรับ (int j = 1; j <= c; j ++) {point [i] [j] = chesspoint ใหม่ (i * unitwidth, j * Unitheight, false); }} กฎ = กฎใหม่ (จุดนี้); record = new MakechessManual (จุดนี้); img = toolkit.getdefaulttoolkit (). getImage ("board.jpg"); pieceimg = toolkit.getdefaulttoolkit (). getImage ("piece.gif"); รถยนต์สีแดง 1 = หมากรุกใหม่ ("", color.red, bc, w - 4, h - 4, สิ่งนี้); รถแดง 1. ชุดหมากรุกหมวดหมู่ (สีสี่เหลี่ยมสีแดง); รถยนต์สีแดง 2 = หมากรุกใหม่ ("", color.red, bc, w - 4, h - 4, สิ่งนี้); รถสีแดง 2. ชุดหมากรุกหมวดหมู่ (สีสี่เหลี่ยมสีแดง); Red Horse 1 = New Chesspiece ("", Color.Red, BC, W - 4, H - 4, นี่); Red Horse 1 = New Chesspiece ("", Color.Red, BC, W - 4, H - 4, นี่); Red Horse 2.Set หมากรุกหมวดหมู่ (สีสี่เหลี่ยมสีแดง); ปืนใหญ่สีแดง 1 = หมากรุกใหม่ ("火", color.red, bc, w - 4, h - 4, สิ่งนี้); Cannon สีแดง 1.Set หมากรุกหมวดหมู่ (สีสแควร์สีแดง); ปืนใหญ่สีแดง 2 = หมากรุกใหม่ ("火", color.red, bc, w - 4, h - 4, สิ่งนี้); หมวดหมู่หมากรุกสีแดง 2. ชุดหมากรุก (สีสแควร์สีแดง); ปืนใหญ่สีแดง 1 = หมากรุกใหม่ ("火", color.red, bc, w - 4, h - 4, สิ่งนี้); Cannon สีแดง 1.Set หมากรุกหมวดหมู่ (สีสแควร์สีแดง); ปืนใหญ่สีแดง 2 = หมากรุกใหม่ ("火", color.red, bc, w - 4, h - 4, สิ่งนี้); Cannon สีแดง 1.Set หมากรุกหมวดหมู่ (สีสแควร์สีแดง); ปืนใหญ่สีแดง 2 = หมากรุกใหม่ ("火", color.red, bc, w - 4, h - 4, สิ่งนี้); หมวดหมู่หมากรุกสีแดง 2. ชุด (สีสแควร์สีแดง); สีแดง 1 = หมากรุกใหม่ ("Shi", color.red, bc, w - 4, h - 4, สิ่งนี้); หมวดหมู่หมากรุกสีแดง 1. ชุด (สีสแควร์สีแดง); สีแดง 2 = หมากรุกใหม่ ("shi", color.red, bc, w - 4, h - 4, สิ่งนี้); หมวดหมู่หมากรุกสีแดง 2. ชุด (สีสแควร์สีแดง); หมวดหมู่หมากรุกสีแดง 2. ชุด (สีสแควร์สีแดง); สีแดง 2 = หมากรุกใหม่ ("shi", color.red, bc, w - 4, h - 4, สิ่งนี้); สีแดง 1 = หมากรุกใหม่ ("Shi", color.red, bc, w - 4, h - 4, สิ่งนี้); Red Soldier 1.Set หมากรุกหมวดหมู่ (สีสแควร์สีแดง); Red Soldier 2 = New Chesspiece ("Soldier", Color.Red, BC, W - 4, H - 4, นี่); Red Soldier 2.Set หมากรุกหมวดหมู่ (สีสี่เหลี่ยมสีแดง); Red Soldier 3 = New Chesspiece ("Soldier", Color.Red, BC, W - 4, H - 4, นี่); Red Soldier 3 = New Chesspiece ("Soldier", Color.Red, BC, W - 4, H - 4, นี่); Red Soldier 4.Set หมากรุกหมวดหมู่ (สีสี่เหลี่ยมสีแดง); Red Soldier 5 = New Chesspiece ("Soldier", Color.Red, BC, W - 4, H - 4, นี่); Red Soldier 5.Set หมากรุกหมวดหมู่ (สีสแควร์สีแดง); Black Soldier = New Chesspiece ("用", color.black, bc, w - 4, h - 4, สิ่งนี้); Black Soldier 1 = New Chesspiece ("用", color.black, bc, w - 4, h - 4, สิ่งนี้); Black Soldier 1 = New Chesspiece ("用", color.black, bc, w - 4, h - 4, สิ่งนี้); Black Soldier 1 = New Chesspiece ("Car", Color.black, BC, W - 4, H - 4, นี่); รถสีดำ 1.SET หมากรุกหมวดหมู่ (สีดำสี่เหลี่ยม); แบล็กคาร์ 2 = หมากรุกใหม่ ("รถ", color.black, bc, w - 4, h - 4, สิ่งนี้); หมวดหมู่หมากรุกสีดำ 2. ชุดหมากรุก (สีสแควร์สีดำ); Black Cannon 1 = New Chesspiece ("Cannon", Color.black, BC, W - 4, H - 4, นี่); หมวดหมู่หมากรุกสีดำ 1. ชุดหมากรุก (สีสแควร์สีดำ); Black Cannon 2 = New Chesspiece ("Cannon", Color.black, BC, W - 4, H - 4, นี่); Black Cannon 2.Set หมากรุกหมวดหมู่ (สีสแควร์สีดำ); ช้างดำ 1 = หมากรุกใหม่ ("ช้าง", color.black, bc, w - 4, h - 4, นี่); หมวดหมู่หมากรุกสีดำ 1. ชุดหมากรุก (สีดำสีดำ); ช้างดำ 2 = หมากรุกใหม่ ("ช้าง", color.black, bc, w - 4, h - 4, นี่); หมวดหมู่หมากรุกสีดำชุดหมากรุก (สีดำสีดำ); ช้างดำ 1 = หมากรุกใหม่ ("ช้าง", color.black, bc, w - 4, h - 4, นี่); หมวดหมู่หมากรุกสีดำ 1. ชุดหมากรุก (สีดำสีดำ); ช้างดำ 2 = หมากรุกใหม่ ("ช้าง", color.black, bc, w - 4, h - 4, นี่); Black Horse 2.Set หมากรุกหมวดหมู่ (สีดำ); กางเกงสีดำ 1 = หมากรุกใหม่ ("Zhi", color.black, bc, w - 4, h - 4, สิ่งนี้); กางเกงสีดำ 1. ชุดหมากรุกหมากรุก (สีดำ); กางเกงสีดำ 2 = หมากรุกใหม่ ("จี", color.black, bc, w - 4, h - 4, นี่); กางเกงสีดำ 2.Set หมากรุกหมวดหมู่ (สีดำ); กางเกงสีดำ 3 = หมากรุกใหม่ ("Zhi", color.black, bc, w - 4, h - 4, สิ่งนี้); กางเกงสีดำ 3.Set หมากรุกหมวดหมู่ (สีดำ); กางเกงสีดำ 4 = หมากรุกใหม่ ("Zhi", color.black, bc, w - 4, h - 4, สิ่งนี้); Black Phantom 4.Set หมากรุกหมวดหมู่ (สีสแควร์สีดำ); Black Phantom 5 = New Chesspiece ("Zhi", Color.black, BC, W - 4, H - 4, นี่); Black Phantom 5.Set หมากรุกหมวดหมู่ (สีดำสีดำ); จุด [1] [10] .setpiece (carriage สีแดง 1, นี่); จุด [2] [10] .setpiece (Red Horse 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 (Red Soldier 2, นี่); จุด [7] [10] .setpiece (Red Soldier 2, นี่); จุด [8] [10] .setpiece (Red Horse 2, นี่); จุด [9] [10] .setpiece (Red Soldier 2, นี่); จุด [2] [8] .setpiece (Red Soldier 1, นี่); จุด [8] [8] .setpiece (Red Soldier 2, นี่); จุด [1] [7] .setpiece (Red Soldier 2, นี่); จุด [3] [7] .setpiece (Red Soldier 2, นี่); จุด [3] [7] .setpiece (Red Soldier 2, นี่); จุด [3] [7] .setpiece (Red Soldier 2, นี่); จุด [5] [7] .setpiece (Red Soldier 3, นี่); จุด [7] [7] .setpiece (Red Soldier 4, นี่); จุด [9] [7] .setpiece (Red Soldier 5, นี่); จุด [1] [1] .setpiece (รถดำ 1, นี่); จุด [2] [1] .setpiece (Black Horse 1, นี่); จุด [3] [1] .setpiece (ช้างดำ 1, นี่); จุด [4] [1] .setpiece (ช้างดำ 1, นี่); จุด [5] [1] .setpiece (Black General, นี่); จุด [6] [1] .setpiece (ช้างดำ 1, นี่); จุด [4] [1] .setpiece (Black General 1, นี่); จุด [5] [1] .setpiece (Black General 2, นี่); จุด [6] [1] .setpiece (Black General 2, นี่); จุด [7] [1] .setpiece (ช้างดำ 2, นี่); จุด [8] [1] .setpiece (Black Horse 2, นี่); จุด [9] [1] .setpiece (Black Car 2, นี่); จุด [2] [3] .setpiece (Black Cannon 1, นี่); จุด [8] [3] .setpiece (Black Cannon 2, นี่); จุด [1] [4] .setpiece (Black Cannon 1, นี่); จุด [3] [4] .setpiece (Black Cannon 2, นี่); จุด [5] [4] .setpiece (Black Cannon 3, นี่); จุด [7] [4] .setpiece (Black Cannon 4, นี่); จุด [7] [4] .setpiece (Black Cannon 4, นี่); จุด [3] [4] .setpiece (Black Cannon 4, นี่); จุด [5] [4] .setpiece (Black Cannon 3, นี่); จุด [7] [4] .setpiece (Black Cannon 4, นี่); จุด [9] [4] .setpiece (กระเป๋าเงินสีดำ 5, นี่); } โมฆะสาธารณะ 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; j ++) {g.drawline (จุด [1] [j] .x, จุด [1] [j] .y, จุด [ความยาวแกน X] [j] .x, จุด [ความยาว x-axis] [j] .y); } สำหรับ (int i = 1; i <= ความยาว x-axis; i ++) {ถ้า (i! = 1 && i! = ความยาว x-axis) {g.drawline (จุด [i] [1] .x, จุด [i] [1] G.Drawline (จุด [i] [y-axis length-4] .x, point [i] [ความยาวแกน y-4] .y, จุด [i] [ความยาวแกน y] .x, จุด [i] [ความยาวแกน y] .y); } else {g.drawline (จุด [i] [1] .x, จุด [i] [1] .y, จุด [i] [ความยาวแกน y] .x, จุด [i] [ความยาวแกน y] .y); }} g.drawline (จุด [4] [1] .x, จุด [4] [1] .y, จุด [6] [3] .x, จุด [6] [3] .y); G.Drawline (จุด [6] [1] .x, จุด [6] [1] .y, จุด [4] [3] .x, จุด [6] [1] .y, จุด [4] [3] .x, จุด [4] [3] .y); G.Drawline (จุด [4] [8] .x, จุด [4] [8] .y, จุด [6] [ความยาวแกน y] .x, จุด [6] [ความยาวแกน y] .y); G.Drawline (จุด [4] [ความยาวแกน y] .x, จุด [4] [ความยาวแกน y] .y, จุด [6] [8] .x, จุด [6] [8] .y); สำหรับ (int i = 1; i <= ความยาวแกน x; 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 ++; }} / ** เหตุการณ์การกดเมาส์* / โมฆะสาธารณะ mousepressed (MouseEvent E) {ชิ้นส่วน chesspiece = null; สี่เหลี่ยมผืนผ้าสี่เหลี่ยม = null; ถ้า (e.getSource () == สิ่งนี้) ย้าย = false; ถ้า (ย้าย == เท็จ) ถ้า (e.getSource () อินสแตนซ์ของหมากรุก) {piece = (chesspiece) e.getSource (); startx = piece.getBounds (). x; starty = piece.getBounds (). y; rect = piece.getBounds (); สำหรับ (int i = 1; i <= ความยาวแกน x; i ++) {สำหรับ (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; หยุดพัก; }}}}}} โมฆะสาธารณะ mousemoved (MouseEvent E) {} / ** เหตุการณ์การลากเมาส์* / โมฆะสาธารณะ mousedragged (MouseEvent E) {ชิ้นส่วน Chesspiece = null; ถ้า (e.getSource () อินสแตนซ์ของหมากรุก) {piece = (chesspiece) e.getSource (); ย้าย = จริง; e = swingutilities.ConvertMouseVent (ชิ้นส่วน e, this); } if (e.getSource () == สิ่งนี้) {ถ้า (ย้าย && piece! = null) {x = e.getx (); y = e.gety (); if (red square chess && ((piece.chess subcategory ()). เท่ากับ (สีสแควร์สีแดง)) {piece.setLocation (x - piece.getWidth () / 2, y - piece.getheight () / 2); } if (Black Chess Move && (piece.chess class (). เท่ากับ (สีตัวอักษรสีดำ))) {piece.setLocation (x - piece.getWidth () / 2, y - piece.getheight () / 2); }}}} / ** เหตุการณ์การปล่อยเมาส์* / โมฆะสาธารณะ mouserleased (MouseEvent E) {ชิ้นส่วนหม้อชิ้น = null; ย้าย = เท็จ; สี่เหลี่ยมผืนผ้าสี่เหลี่ยม = null; ถ้า (e.getSource () อินสแตนซ์ของหมากรุก) {piece = (chesspiece) e.getSource (); rect = piece.getBounds (); e = swingutilities.ConvertMouseVent (ชิ้นส่วน e, this); } if (e.getSource () == สิ่งนี้) {บูลีน containSchessPoint = false; int x = 0, y = 0; int m = 0, n = 0; if (piece! = null) {สำหรับ (int i = 1; i <= ความยาวแกน x; i ++) {สำหรับ (int j = 1; j <= ความยาวแกน y; j ++) {x = point [i] [j] .getx (); y = จุด [i] [j]. gety (); if (rect.contains (x, y)) {containSchessPoint = true; m = i; n = j; หยุดพัก; }}}}} if (piece! = null && containschessPoint) {color piececolor = piece.get piece.get color (); if (จุด [m] [n] .ispiece ()) {color c = (จุด [m] [n] .getpiece ()) รับสีของหมากรุก (); if (piececolor.getRgb () == c.getRgb ()) {piece.setLocation (startx, starty); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (จริง); } else {Boolean OK = Rule.MovePieCerule (ชิ้นส่วน, starti, startj, m, n); ถ้า (ตกลง) {chesspiece pieceremoved = point [m] [n] .getPiece (); จุด [m] [n]. removepiece (pieceremoved, this); จุด [m] [n] .setpiece (ชิ้นส่วนนี้); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (เท็จ); บันทึกคะแนนหมากรุกบันทึก (ชิ้นส่วน, starti, startj, m, n); บันทึกบันทึกชิ้นหมากรุกที่กิน (ทีละชิ้น); Rule.iswine (pieceremoved); if (piece.paper category (). เท่ากับ (สีสี่เหลี่ยมสีแดง)) {red square move = false; Black Square Move = true; } if (piece.paper category (). Equals (สีดำสีดำ)) {Black Square Move = False; การย้ายสี่เหลี่ยมสีแดง = true; } ตรวจสอบความถูกต้อง(); ทาสีใหม่ (); } else {piece.setLocation (startx, starty); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (จริง); }}} else {boolean ok = กฎ. movepiecerule (ชิ้นส่วน starti, startj, m, n); ถ้า (ตกลง) {จุด [m] [n] .setpiece (ชิ้นส่วนนี้); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (เท็จ); บันทึกคะแนนหมากรุกบันทึก (ชิ้นส่วน, starti, startj, m, n); บันทึกชิ้นหมากรุกบันทึกการกิน ("ไม่มีชิ้นหมากรุก"); if (piece.paper category (). เท่ากับ (สีสี่เหลี่ยมสีแดง)) {red square chess = false; หมากรุกสีดำสแควร์ = จริง; } if (piece.paper category (). เท่ากับ (สีดำสีดำ)) {หมากรุกสีดำสแควร์ = เท็จ; หมากรุกสี่เหลี่ยมจัตุรัสสีแดง = จริง; }} else {piece.setLocation (startx, starty); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (จริง); }}} if (piece! = null &&! containchesspoint) {piece.setLocation (startx, starty); (จุด [starti] [startJ]) ชุดมีชิ้นหมากรุก (จริง); }}} โมฆะสาธารณะ Mouseentered (MouseEvent E) {} โมฆะสาธารณะ mouseexited (MouseEvent E) {} โมฆะสาธารณะ mouseclicked (MouseEvent E) {}}} 3. chesspiece.java ไฟล์
แพ็คเกจ cn.edu.ouc.chinesechess; นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.*; / ** * คลาสหมากรุก * * @author cnlht */ หมากรุกคลาสสาธารณะขยาย jlabel {ชื่อสตริง; // หมากรุกชื่อสี backcolor = null, porteolor; // พื้นหลังและสายสีเบื้องหน้าหมวดหมู่สีสตริงสี = null; กระดานหมากรุก = null; ความกว้าง int, ความสูง; // ขนาดหมากรุกสาธารณะ (ชื่อสตริง, สี FC, สี BC, ความกว้าง int, ความสูง int, กระดานหมากรุก) {// สร้างชิ้นส่วนหมากรุก this.name = name; this.board = บอร์ด; this.width = ความกว้าง; this.height = ความสูง; Porseolor = FC; backcolor = bc; setsize (ความกว้างความสูง); Fetbackground (BC); addmousemotionlistener (บอร์ด); addmouselistener (บอร์ด); } // วาดชิ้นส่วนหมากรุกเป็นโมฆะสาธารณะสี (กราฟิก g) {g.drawimage (board.pieceimg, 2, 2, ความกว้าง -2, ความสูง 2, null); G.SetColor (PORESTOLOR); G.SetFont (ฟอนต์ใหม่ ("Kaiti", Font.Bold, 26)); G.DrawString (ชื่อ, 7, ความสูง - 8); // วาด "ชื่อหมากรุก" บนชิ้นหมากรุก G.SetColor (color.black); //g.drawoval(1, 1, ความกว้าง - 1, ความสูง - 1); float linewidth = 2.3f; ((graphics2d) g) .setstroke (basicstroke ใหม่ (linewidth)); ((graphics2d) g). DDRAWOVAL (2, 2, WIDTH-2, ความสูง 2); } public int getWidth () {ความกว้างกลับ; } public int getheight () {return height; } สตริงสาธารณะ getName () {ชื่อคืน; } สีสาธารณะได้รับสีหมากรุก () {ส่งคืนพยากรณ์; } โมฆะสาธารณะตั้งหมวดหมู่หมากรุก (หมวดหมู่สตริง) {หมวดหมู่สี = หมวดหมู่; } หมวดหมู่หมากรุกสตริงสาธารณะ () {กลับหมวดหมู่สี; - 4. ไฟล์พิกัดชิ้นส่วนหมากรุก
แพ็คเกจ cn.edu.ouc.chinesechess; / *** คลาส Chess Point** @author Cnlht*/ Public Class Chesspoint {/ ** พิกัด Chesspiece*/ int x, y; / ** พิกัดนี้มีชิ้นส่วน*/ บูลีนมีชิ้นส่วนหรือไม่ /** ชิ้นหมากรุก = null; / ** กระดานหมากรุกที่พิกัดเป็น*/ บอร์ดบอร์ดกระดาน = null; Public Chesspoint (int x, int y, บูลีนบู) {this.x = x; this.y = y; มีชิ้นส่วน = boo; } public boolean ispiece () {return มีชิ้นหมากรุก; } ชุดโมฆะสาธารณะมีชิ้นหมากรุก (บูลีนบู) {ชิ้นหมากรุก = boo; } สาธารณะ int getx () {return x; } สาธารณะ int getey () {return y; } // ตั้งค่าการเปลี่ยนคะแนนเป็นชิ้นส่วนของหมากรุกโมฆะสาธารณะ setpiece (ชิ้นหมากรุก, กระดานหมากรุก) {this.board = บอร์ด; this.piece = piece; board.add (ชิ้นส่วน); int w = (board.unitwidth); int h = (board.unitheight); piece.setbounds (x - w / 2, y - h / 2, w, h); // ตำแหน่งหมากรุก, ความกว้าง, ความสูงด้วยชิ้นหมากรุก = จริง; board.validate (); } Public Chesspiece getpiece () {return piece; } โมฆะสาธารณะ removepiece (ชิ้นส่วนหมากรุกกระดานหมากรุก) {this.board = บอร์ด; this.piece = piece; board.Remove (ชิ้นส่วน); board.validate (); หมากรุกชิ้น = เท็จ; - 5. กฎการเล่นเกมกฎคลาส
แพ็คเกจ cn.edu.ouc.chinesechess; นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.*; / ** * คลาสกฎการเล่นหมากรุกคลาส * * @author cnlht */ กฎคลาสสาธารณะ {กระดานหมากรุก = null; ชิ้นส่วนหมากรุก = null; จุดหมากรุก [] []; int starti, startj, endi, endj; กฎสาธารณะ (บอร์ดกระดานหมากรุก, จุดหมากรุก [] []) {this.board = บอร์ด; this.point = จุด; } โมฆะสาธารณะ Iswine (ชิ้นส่วนหมากรุก) {this.piece = piece; if (piece.getName () == "จัดตำแหน่ง" || piece.getName () == "Handy") {if (piece.color หมวดหมู่ == "Red Side") {Joptionpane.showMessagedialog (Null, "Black Side Victory!"); } else {joptionpane.showMessagedialog (null, "ชัยชนะด้านข้างสีแดง!"); }}} บูลีนสาธารณะ 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); บูลีนคุณเล่นหมากรุกได้ไหม = เท็จ; if (piece.getName (). เท่ากับ ("car")) {ถ้า (starti == endi) {int j = 0; สำหรับ (j = minj+1; j <= maxj - 1; j ++) {ถ้า (จุด [starti] [j] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; หยุดพัก; }} ถ้า (j == maxj) {คุณสามารถย้ายหมากรุก = true; }} อื่นถ้า (startj == endj) {int i = 0; สำหรับ (i = mini+1; i <= maxi - 1; i ++) {ถ้า (จุด [i] [startj] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; หยุดพัก; }} ถ้า (i == maxi) {คุณสามารถย้ายหมากรุก = true; }} else {คุณสามารถย้ายหมากรุก = false; }}} อื่นถ้า (piece.getName (). equals ("")) {ถ้า (starti == endi) {int j = 0; สำหรับ (j = minj+1; j <= maxj - 1; j ++) {ถ้า (จุด [starti] [j] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; หยุดพัก; }} ถ้า (j == maxj) {คุณสามารถย้ายหมากรุก = true; }} อื่นถ้า (startj == endj) {int i = 0; สำหรับ (i = mini+1; i <= maxi - 1; i ++) {ถ้า (จุด [i] [startj] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; หยุดพัก; }} ถ้า (i == maxi) {ฉันสามารถย้ายหมากรุก = true; }} else {ฉันสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). equals ("ma")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (xaxle == 2 && yaxle == 1) {ถ้า (endi> starti) {ถ้า (จุด [starti + 1] [startj] .ispiece ()) {ฉันสามารถย้ายหมากรุก = false; } else {ฉันสามารถย้ายหมากรุก = true; }} ถ้า (endi <starti) {ถ้า (จุด [starti - 1] [startj] .ispiece ()) {คุณสามารถย้ายหมากรุก = เท็จได้ไหม; } else {คุณสามารถย้ายหมากรุก = true; }}} อื่นถ้า (xaxle == 1 && yaxle == 2) {if (endj> startj) {ถ้า (จุด [starti] [startj + 1] .ispiece ()) {คุณสามารถย้ายหมากรุก = เท็จ; } else {คุณสามารถย้ายหมากรุก = true; }} if (endj <startj) {ถ้า (จุด [starti] [startj - 1] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; } else {คุณสามารถย้ายหมากรุก = true; }} if (endj <startj) {ถ้า (จุด [starti] [startj - 1] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; } else {คุณสามารถย้ายหมากรุก = true; } จริง; }}} else {ฉันสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). equals ("")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (xaxle == 2 && yaxle == 1) {ถ้า (endi> starti) {ถ้า (จุด [starti + 1] [startj] .ispiece ()) {ฉันสามารถย้ายหมากรุก = false; } else {ฉันสามารถย้ายหมากรุก = true; }} ถ้า (endi <starti) {ถ้า (จุด [starti - 1] [startj] .ispiece ()) {คุณสามารถย้ายหมากรุก = เท็จได้ไหม; } else {คุณสามารถย้ายหมากรุก = true; }}} อื่นถ้า (xaxle == 1 && yaxle == 2) {if (endj> startj) {ถ้า (จุด [starti] [startj + 1] .ispiece ()) {คุณสามารถย้ายหมากรุก = เท็จ; } else {คุณสามารถย้ายหมากรุก = true; }} if (endj <startj) {ถ้า (จุด [starti] [startj - 1] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; } else {คุณสามารถย้ายหมากรุก = true; }}} else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). Equals ("Icon")) {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) {ถ้า (จุด [centeri] [centerj] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; } else {คุณสามารถย้ายหมากรุก = true; }} else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). Equals ("Phase")) {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) {ถ้า (จุด [centeri] [centerj] .ispiece ()) {คุณสามารถย้ายหมากรุก = false; } else {คุณสามารถย้ายหมากรุก = true; }} else {คุณสามารถย้ายหมากรุก = true; }} else {คุณสามารถย้ายหมากรุก = true; }} else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). Equals ("cannon")) {int number = 0; if (starti == endi) {int j = 0; สำหรับ (j = minj+1; j <= maxj - 1; j ++) {ถ้า (จุด [starti] [j] .ispiece ()) {number ++; }} if (number> 1) {คุณสามารถย้ายหมากรุก = false; } อื่นถ้า (number == 1) {ถ้า (จุด [endi] [endj] .ispiece ()) {คุณสามารถย้ายหมากรุก = จริง; }} อื่นถ้า (number == 0 &&! point [endi] [endj] .ispiece ()) {คุณสามารถย้ายหมากรุก = จริง; }} อื่นถ้า (startj == endj) {int i = 0; สำหรับ (i = mini+1; i <= maxi - 1; i ++) {ถ้า (จุด [i] [startj] .ispiece ()) {number ++; }} if (number> 1) {คุณสามารถย้ายหมากรุก = false; } อื่นถ้า (number == 1) {ถ้า (จุด [endi] [endj] .ispiece ()) {คุณสามารถย้ายหมากรุก = จริง; }} อื่นถ้า (number == 0 &&! point [endi] [endj] .ispiece ()) {คุณสามารถย้ายหมากรุก = จริง; }} else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). Equals ("Soldier")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endj> = 6) {if (startj - endj == 1 && xaxle == 0) {คุณสามารถย้ายหมากรุก = true; } else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (endj <= 5) {if ((startj - endj == 1) && (xaxle == 0)) {ฉันสามารถย้ายหมากรุก = true; } else if ((endj - startj == 0) && (xaxle == 1)) {ฉันสามารถย้ายหมากรุก = true; } else {ฉันสามารถย้ายหมากรุก = false; }}} อื่นถ้า (piece.getName (). Equals ("zhi")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endj <= 5) {if (endj - startj == 1 && xaxle == 0) {ฉันสามารถย้ายหมากรุก = true; } else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (endj> = 6) {if ((endj - startj == 1) && (xaxle == 0)) {คุณสามารถย้ายหมากรุก = true; } else if ((endj - startj == 0) && (xaxle == 1)) {คุณสามารถย้ายหมากรุก = จริง; } else {คุณสามารถย้ายหมากรุก = false; }}} อื่นถ้า (piece.getName (). equals ("shi")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {คุณสามารถย้ายหมากรุก = true; } else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า (piece.getName (). equals ("state")) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {คุณสามารถย้ายหมากรุก = true; } else {คุณสามารถย้ายหมากรุก = false; }} อื่นถ้า ((piece.getName (). Equals ("หล่อ")) || (piece.getName (). เท่ากับ ("แทนที่"))) {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4) {ถ้า ((xaxle == 1 && yaxle == 0) || (xaxle == 0 && yaxle == 1)) {คุณสามารถย้ายหมากรุก = true; } else {คุณสามารถย้ายหมากรุก = false; }} else {คุณสามารถย้ายหมากรุก = false; }} return คุณสามารถย้ายหมากรุก; - 6. ไฟล์ movestep.java
แพ็คเกจ cn.edu.ouc.chinesechess; นำเข้า Java.awt.Point; / ** * คลาสการเดิน * * @author Cnlht * */ Movestep คลาสสาธารณะใช้ java.io.serializable {Public Point Pstart, Pend; Public Movestep (จุด P1, Point 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; กระดานหมากรุก = null; chesspoint [] [] จุด; LinkedList Chess note = null; LinkedList Eated Chess ชิ้น = NULL; Jbutton Buttonundo; int i = 0; Public MakechessManual (กระดานหมากรุก, หมากรุก [] [] จุด) {this.board = บอร์ด; this.point = จุด; text = new jtextarea (); Scroll = new JScrollpane (ข้อความ); Chess note = new LinkedList (); Eated Chess Pieces = new LinkedList (); buttonundo = new jbutton ("Chess กลับใจ"); buttonundo.setfont (ฟอนต์ใหม่ ("lishu", font.plain, 18)); SetLayout (New BorderLayout ()); เพิ่ม (Scroll, BorderLayout.Center); เพิ่ม (buttonundo, borderlayout.south); buttonundo.addactionListener (นี่); } 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; } โมฆะสาธารณะบันทึกคะแนนหมากรุก (ชิ้นส่วนหมากรุก, int starti, int startj, int endi, int endj) {point pstart = จุดใหม่ (starti, startj); point pend = จุดใหม่ (endi, endj); movestep step = movestep ใหม่ (pstart, pend); Chess score.add (ขั้นตอน); หมวดหมู่หมากรุกสตริง = piece.chess category (); ชื่อสตริง = piece.getName (); สตริง m = "#" + หมวดหมู่หมากรุก + ชื่อ + ":" + starti + numberToletter (startj) + "ถึง" + endi + numberToletter (endj); text.append (m); if (piece. หมวดหมู่หมากรุก (). เท่ากับ (board.board.black square color)) text.append ("/n"); } โมฆะสาธารณะบันทึกชิ้นส่วนหมากรุกที่กิน (วัตถุวัตถุ) {Eating Chess Piece.Add (วัตถุ); } Public LinkedList รับชิ้นหมากรุก () {return Chess Piece; } โมฆะสาธารณะ ActionPerformed (ActionEvent E) {int position = text.getText (). lastIndexof ("#"); if (ตำแหน่ง! = -1) text.replacerange ("", ตำแหน่ง, text.getText (). length ()); if (chess piece.size ()> 0) {movestep lastStep = (movestep) หมากรุก piece.getLast (); Chess Record.removelast (); Object Qizi = Eated Chess Piece.getLast (); Eated Chess Piece.RemoVelast (); String temp = qizi.toString (); if (temp.equals ("ไม่มีชิ้นส่วนหมากรุก")) {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 หมวดหมู่ชิ้นส่วน (). เท่ากับ (บอร์ดสีสแควร์)) {board.red square move = true; board.black square move = false; } if (piece.chess piece category (). Equals (board.board.black square color)) {board.black square move = true; Board.Red Square Move = FALSE; }} else {chesspiece removedpiece = (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 (ชิ้นส่วนบอร์ด); จุด [endi] [endj] .setpiece (ลบชิ้นส่วน, บอร์ด); (จุด [endi] [endj]) ชุดมีชิ้นหมากรุก (จริง); 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记录棋谱的方法。
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。