Artikel ini menjelaskan kode permainan catur Cina yang diimplementasikan oleh Java. Ini dibagikan kepada Anda untuk referensi Anda. Kode spesifiknya adalah sebagai berikut
1. Tujuan Praktis:
1. Aplikasi dan perbedaan antara klik mouse, seret dan acara lainnya
2. Menyimpan dan membaca file catatan catur
3. Tingkatkan aturan catur.
2. Konten Praktis:
Catur Cina memiliki sejarah panjang dan telah menarik banyak orang untuk dipelajari. Sekarang kami telah membuat desain dan penjelasan berikut tentang pertempuran catur Cina dan produksi skor catur untuk referensi dan pembelajaran Anda.
1. Saat bermain catur antara mesin dan mesin, sisi merah memimpin. Seret potongan catur ke tujuan jika memenuhi aturan dan tupai dijatuhkan.
Semua orang bermain catur
2. Buat catatan catur. Setelah memilih menu rekaman catur, permainan dimulai dan proses catur direkam.
Pilih menu "Make Chess Record"
Sisi merah menang setelah skor catur selesai
Setelah satu sisi menang, kotak dialog Victory Message muncul. Setelah mengklik OK, pilih menu "Simpan Catur Rekam" dan kotak dialog Simpan File muncul.
Simpan Kotak Dialog Catatan Catur
3. Demonstrasi skor catur. Setelah memilih menu skor catur demonstrasi, kotak dialog terbuka muncul, pilih skor catur yang disimpan untuk memulai demonstrasi.
Demonstrasi kotak dialog rekaman catur
Demonstrasi proses catatan catur (otomatis dan manual)
3. Kode Referensi:
1. File utama catur chinesechess.java
paket cn.edu.ouc.chinesechess; impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; impor java.io.*; impor java.util.linkedlist; / ** * Kelas utama catur * * @author cnlht */ kelas publik Chinesechess memperluas JFrame mengimplementasikan ActionListener {CHessboard Board = null; Demon Demon = null; MakechessManual Record = NULL; Container con = null; JMenubar Bar; JMenu Filemenu; JMenuitem membuat skor catur, menyimpan skor catur, menunjukkan skor catur; Jfilechooser filechooser = null; LinkedList Catur Skor = NULL; public chinesechess () {bar = new jmenubar (); filemenu = jmenu baru ("catur Cina"); Buat skor catur = jMenuitem baru ("buat skor catur"); Simpan skor catur = JMenuitem baru ("Simpan Skor Catur"); Simpan skor catur.setenabled (false); Skor catur demonstrasi = JMenuitem baru ("Skor Catur Demonstrasi"); filemenu.add (buat skor catur); filemenu.add (simpan skor catur); filemenu.add (menunjukkan skor catur); Bar.Add (Filemenu); setjmenubar (bar); settitle (buat catur skor.getText ()); membuat skor catur.addactionListener (ini); Simpan skor catur.addactionListener (ini); menunjukkan skor catur.addactionListener (ini); papan = papan catur baru (45, 45, 9, 10); Record = Board.Record; con = getContentPane (); Jsplitpane split = jsplitpane baru (jsplitpane.horizontal_split, true, board, record); split.setDividerSize (5); split.setDividerLocation (460); con.add (split, borderlayout.center); addWindowlistener (windowAdapter baru () {public void windowclosing (windowevent e) {System.exit (0);}}); setVisible (true); setbounds (60, 20, 690, 540); filechooser = jFilechooser baru (); con.validate (); mengesahkan(); } public void actionPerformed (ActionEvent e) {if (e.getSource () == membuat skor catur) {con.removeall (); Simpan skor catur.setenabled (true); this.settitle (buat catur skor.getText ()); papan = papan catur baru (45, 45, 9, 10); Record = Board.Record; Jsplitpane split = jsplitpane baru (jsplitpane.horizontal_split, true, board, record); split.setDividerSize (5); split.setDividerLocation (460); con.add (split, borderlayout.center); mengesahkan(); } if (e.getSource () == Simpan skor catur) {int state = filechooser.showsaveDialog (null); File saveFile = filechooser.getSelectedFile (); if (saveFile! = null && state == jfilechooser.approve_option) {coba {fileoutputStream outOne = new fileoutputStream (saveFile); ObjectOutputStream outtwo = ObjectOutputStream baru (outOne); outtwo.writeObject (Record.get chess skor ()); outone.close (); outtwo.close (); } catch (event ioException) {}}} if (e.getSource () == Demonstrasi skor catur) {con.removeall (); con.repaint (); con.validate (); mengesahkan(); Simpan skor catur.setenabled (false); int state = filechooser.showopendialog (null); File openFile = filechooser.getSelectedFile (); if (openfile! = null && state == jfilechooser.approve_option) {coba {fileInputStream inone = new fileInputStream (openFile); ObjectInputStream intwo = ObjectInputStream baru (inOne); Catur = (LinkedList) intwo.readObject (); inone.close (); intwo.close (); Papan catur = papan catur baru (45, 45, 9, 10); Demon = Demon baru (papan); Demon.set Record Catur (Catur Cat); con.add (Demon, borderlayout.center); con.validate (); mengesahkan(); this.settitle (Demo Chess Record.getText () + ":" + OpenFile); } catch (Exception Event) {jlabel label = new jlabel ("not catur file rekaman"); label.setfont (font baru ("lishu", font.bold, 60)); label.setForeground (color.red); label.sethorizontalAlignment (swingconstants.center); con.add (label, borderlayout.center); con.validate (); this.settitle ("No Chess Record"); mengesahkan(); }} else {jlabel label = new jlabel ("No Chess Record File tidak dibuka"); label.setfont (font baru ("Lishu", font.bold, 50)); label.setForeground (color.pink); label.sethorizontalAlignment (swingconstants.center); con.add (label, borderlayout.center); con.validate (); this.settitle ("File catur tidak dibuka"); mengesahkan(); }}} public static void main (string args []) {new chinesechess (); }} 2. File Chessboard.java
paket cn.edu.ouc.chinesechess; impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; / ** * Kelas papan catur * * @author cnlht */ Kelas publik CHESSBOARD memperluas Jpanel mengimplementasikan MouseListener, MousemotionListener {public cyspoint point [] []; unitwidth int publik, unitheight; Panjang privat int x-sumbu, panjang sumbu y; Private int x, y; gambar pribadi img; gambar gambar yang dilindungi; Private Boolean Move = False; string publik warna merah square = "square merah", warna persegi hitam = "black square"; 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; Catur sisi merah boolean publik = true, catur sisi hitam = false; Aturan aturan = null; Catatan MANCECHESSMANUAL PUBLIK = NULL; papan catur publik (int w, int h, int r, int c) {setLayout (null); AddMouseListener (ini); addMouseMotionListener (ini); Warna bc = getBackground (); unitwidth = w; unitheight = h; Panjang sumbu x = r; Panjang sumbu y = C; point = CHESSPOint baru [R + 1] [C + 1]; untuk (int i = 1; i <= r; i ++) {for (int j = 1; j <= c; j ++) {point [i] [j] = new ChessPoint (i * unitwidth, j * unitheight, false); }} aturan = aturan baru (ini, point); Rekaman = MakeChessManual baru (ini, poin); img = toolkit.getDefaultToolKit (). getImage ("board.jpg"); PICEIMG = toolkit.getDefaultToolKit (). getImage ("piece.gif"); mobil merah 1 = catur baru ("", color.red, bc, w - 4, h - 4, this); Mobil Merah 1. Kategori Potongan Catur (Warna Persegi Merah); Mobil Merah 2 = Chesspiece Baru ("", Color.Red, BC, W - 4, H - 4, ini); Mobil Merah 2. Kategori Sepotong Catur (Warna Persegi Merah); Red Horse 1 = Chesspiece baru ("", Color.Red, BC, W - 4, H - 4, This); Red Horse 1 = Chesspiece baru ("", Color.Red, BC, W - 4, H - 4, This); Kuda Merah 2. Kategori Potongan Catur (Warna Persegi Merah); Red Cannon 1 = Chesspiece baru ("火", color.red, bc, w - 4, h - 4, this); Red Cannon 1. Kategori Potongan Catur (Warna Persegi Merah); Red Cannon 2 = baru catur ("火", color.red, bc, w - 4, h - 4, this); Meriam merah 2. Kategori potongan catur (warna persegi merah); Red Cannon 1 = Chesspiece baru ("火", color.red, bc, w - 4, h - 4, this); Red Cannon 1. Kategori Potongan Catur (Warna Persegi Merah); Red Cannon 2 = baru catur ("火", color.red, bc, w - 4, h - 4, this); Red Cannon 1. Kategori Potongan Catur (Warna Persegi Merah); Red Cannon 2 = baru catur ("火", color.red, bc, w - 4, h - 4, this); Kategori Piece Catur Red 2.Set (Warna Persegi Merah); Merah 1 = catur baru ("shi", color.red, bc, w - 4, h - 4, this); Kategori Potongan Catur Merah 1.Set (warna persegi merah); Merah 2 = catur baru ("shi", color.red, bc, w - 4, h - 4, this); Kategori Piece Catur Red 2.Set (Warna Persegi Merah); Kategori Piece Catur Red 2.Set (Warna Persegi Merah); Merah 2 = catur baru ("shi", color.red, bc, w - 4, h - 4, this); Merah 1 = catur baru ("shi", color.red, bc, w - 4, h - 4, this); Prajurit Merah 1. Kategori Potongan Catur (Warna Persegi Merah); Red Soldier 2 = Chesspiece baru ("Soldier", Color.Red, BC, W - 4, H - 4, ini); Soldier Merah 2. Kategori Potongan Catur (Warna Persegi Merah); Prajurit merah 3 = catur baru ("prajurit", color.red, bc, w - 4, h - 4, ini); Prajurit merah 3 = catur baru ("prajurit", color.red, bc, w - 4, h - 4, ini); Soldier Merah 4. Kategori Potongan Catur (Warna Persegi Merah); Prajurit merah 5 = catur baru ("prajurit", color.red, bc, w - 4, h - 4, ini); Tentara Merah 5. Kategori Potongan Catur (Warna Persegi Merah); Black Soldier = CHESSPIECE baru ("Sold", Color.Black, BC, W - 4, H - 4, This); Black Soldier 1 = baru catur ("prajurit", color.black, bc, w - 4, h - 4, ini); Black Soldier 1 = baru catur ("prajurit", color.black, bc, w - 4, h - 4, ini); Black Soldier 1 = Chesspiece baru ("mobil", color.black, bc, w - 4, h - 4, ini); Mobil Hitam 1. Kategori Potongan Catur (Warna Square Hitam); Mobil hitam 2 = catur baru ("mobil", color.black, bc, w - 4, h - 4, this); Mobil Hitam 2. Kategori Potongan Catur (Warna Square Hitam); Black Cannon 1 = Chesspiece baru ("Cannon", Color.Black, BC, W - 4, H - 4, This); Black Cannon 1. Kategori Potongan Catur (Warna Square Hitam); Black Cannon 2 = CHESSPIECE baru ("Cannon", Color.Black, BC, W - 4, H - 4, This); Black Cannon 2.Set Catur Piece Kategori (warna persegi hitam); Black Elephant 1 = CHESSPIECE BARU ("Gajah", Color.Black, BC, W - 4, H - 4, ini); Black Elephant 1. Kategori Potongan Catur (Warna Square Hitam); Black Elephant 2 = Chesspiece baru ("Gajah", Color.Black, BC, W - 4, H - 4, ini); Black Elephant 2.Set Catur Piece Kategori (warna persegi hitam); Black Elephant 1 = CHESSPIECE baru ("Gajah", Color.Black, BC, W - 4, H - 4, ini); Black Elephant 1. Kategori Potongan Catur (Warna Square Hitam); Black Elephant 2 = Chesspiece baru ("Gajah", Color.Black, BC, W - 4, H - 4, ini); Kuda Hitam 2. Kategori Sepotong Catur (Warna Hitam); Celana hitam 1 = catur baru ("zhi", color.black, bc, w - 4, h - 4, this); Celana Hitam 1. Kategori Potongan Catur (Warna Hitam); Black Pants 2 = baru catur ("zhi", color.black, bc, w - 4, h - 4, this); Celana Hitam 2. Kategori Potongan Catur (Warna Hitam); Black Pants 3 = Chesspiece baru ("Zhi", Color.Black, BC, W - 4, H - 4, This); Celana hitam 3. Kategori Potongan Catur (Warna Hitam); Black Pants 4 = baru catur ("zhi", color.black, bc, w - 4, h - 4, this); Black Phantom 4.Set Catur Kategori Piece (Warna Square Hitam); Black Phantom 5 = CHESSPIECE baru ("ZHI", Color.Black, BC, W - 4, H - 4, This); Black Phantom 5. Kategori Potongan Catur (Warna Square Hitam); titik [1] [10] .setpiece (kereta merah 1, ini); titik [2] [10] .setpiece (kuda merah 1, ini); titik [3] [10] .setpiece (fase merah 1, ini); Point [4] [10] .setpiece (Sage Red 1, This); Point [4] [10] .setpiece (Sage Red 1, This); Point [4] [10] .setpiece (Sage Red 1, This); titik [5] [10] .setpiece (prajurit merah, ini); titik [6] [10] .setpiece (prajurit merah 2, ini); titik [7] [10] .setpiece (prajurit merah 2, ini); titik [8] [10] .setpiece (Red Horse 2, This); titik [9] [10] .setpiece (prajurit merah 2, ini); titik [2] [8] .setpiece (prajurit merah 1, ini); titik [8] [8] .setpiece (prajurit merah 2, ini); titik [1] [7] .setpiece (prajurit merah 2, ini); titik [3] [7] .setpiece (prajurit merah 2, ini); titik [3] [7] .setpiece (prajurit merah 2, ini); titik [3] [7] .setpiece (prajurit merah 2, ini); titik [5] [7] .setpiece (prajurit merah 3, ini); titik [7] [7] .setpiece (prajurit merah 4, ini); titik [9] [7] .setpiece (prajurit merah 5, ini); titik [1] [1] .setpiece (Black Car 1, This); titik [2] [1] .setpiece (Black Horse 1, This); titik [3] [1] .setpiece (Black Elephant 1, This); titik [4] [1] .setpiece (Black Elephant 1, This); titik [5] [1] .setpiece (Black General, This); titik [6] [1] .setpiece (Black Elephant 1, This); titik [4] [1] .setpiece (Black General 1, This); titik [5] [1] .setpiece (Black General 2, This); titik [6] [1] .setpiece (Black General 2, This); titik [7] [1] .setpiece (Black Elephant 2, This); titik [8] [1] .setpiece (Black Horse 2, This); Point [9] [1] .setpiece (Black Car 2, This); titik [2] [3] .setpiece (Black Cannon 1, This); titik [8] [3] .setpiece (Black Cannon 2, This); titik [1] [4] .setpiece (Black Cannon 1, This); titik [3] [4] .setpiece (Black Cannon 2, This); titik [5] [4] .setpiece (Black Cannon 3, This); titik [7] [4] .setpiece (Black Cannon 4, This); titik [7] [4] .setpiece (Black Cannon 4, This); titik [3] [4] .setpiece (Black Cannon 4, This); titik [5] [4] .setpiece (Black Cannon 3, This); titik [7] [4] .setpiece (Black Cannon 4, This); titik [9] [4] .setpiece (dompet hitam 5, ini); } public void catcomponent (grafik g) {super.paintComponent (g); int imgwidth = img.getWidth (ini); int imgheight = img.getHeight (ini); // Dapatkan lebar dan tinggi gambar int fwidth = getWidth (); int fheight = getHeight (); // dapatkan lebar dan tinggi jendela int x = (fwidth - imgwidth)/ 2; int y = (fheight - imgheight) / 2; G.DrawImage (IMG, X, Y, NULL); untuk (int j = 1; j <= panjang sumbu y; j ++) {g.drawline (titik [1] [j] .x, titik [1] [j] .y, titik [panjang sumbu x] [j] .x, titik [panjang sumbu x] [j] .y); } untuk (int i = 1; i <= X-sumbu panjang; i ++) {if (i! = 1 && i! = Panjang sumbu x) {g.drawline (titik [i] [1] .x, poin [i] [1] .y, poin [i] [panjang y-sumbu-5] .x, point [i] [y-axis panjang [i]. g.drawline (titik [i] [panjang sumbu-4] .x, titik [i] [panjang sumbu y-4] .y, titik [i] [panjang sumbu y] .x, titik [i] [panjang sumbu y] .y); } else {g.drawline (titik [i] [1] .x, titik [i] [1] .y, titik [i] [panjang sumbu y] .x, titik [i] [panjang sumbu y] .y); }} g.drawline (titik [4] [1] .x, point [4] [1] .y, point [6] [3] .x, point [6] [3] .y); G.Drawline (titik [6] [1] .x, titik [6] [1] .y, titik [4] [3] .x, titik [6] [1] .y, titik [4] [3] .x, titik [4] [3] .y); G.Drawline (titik [4] [8] .x, titik [4] [8] .y, titik [6] [panjang sumbu y] .x, titik [6] [panjang sumbu y] .y); g.drawline (titik [4] [panjang sumbu-y] .x, titik [4] [panjang sumbu y] .y, titik [6] [8] .x, titik [6] [8] .y); untuk (int i = 1; i <= panjang sumbu x; i ++) {g.drawstring (""+i, i * unitwidth, unitheight / 2); } int j = 1; untuk (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; Persegi panjang rect = null; if (e.getSource () == this) move = false; if (move == false) if (e.getSource () instance dari cyspiece) {piece = (cyspiece) e.getSource (); startx = piece.getBounds (). x; starty = piece.getBounds (). y; Rect = PICE.GetBounds (); untuk (int i = 1; i <= panjang sumbu x; i ++) {for (int j = 1; j <= y-sumbu panjang; j ++) {int x = point [i] [j] .getx (); int y = point [i] [j] .gety (); if (rect.contains (x, y)) {startI = i; startj = j; merusak; }}}}}} public void mousemoved (mouseEvent e) {} / ** Acara drag mouse* / public void mousedragged (mouseEvent e) {chesspiece piece = null; if (e.getSource () instance cyspiece) {piece = (Chesspiece) e.getSource (); pindah = true; e = swingutilities.convertMouseEvent (piece, e, this); } if (e.getSource () == this) {if (move && piece! = null) {x = e.getx (); y = e.gety (); if (Red Square Chess && ((Piece.Chess Subcategory ()). Equals (Red Square Color))) {PICE.SETLOCATION (X - PICE.GETWIDTH () / 2, Y - PICE.GETHEIGHT () / 2); } if (Black Chess Move && (Piece.Chess class (). Equals (Black Font Color))) {PICE.SETLOCATION (X - PICE.GETWIDTH () / 2, Y - PICE.GetHeight () / 2); }}}} / ** Release mouse event* / public void mousereLeased (mouseEvent e) {chesspiece piece = null; pindah = false; Persegi panjang rect = null; if (e.getSource () instance dari cyspiece) {piece = (cyspiece) e.getSource (); Rect = PICE.GetBounds (); e = swingutilities.convertMouseEvent (piece, e, this); } if (e.getSource () == this) {boolean containschesspoint = false; int x = 0, y = 0; int m = 0, n = 0; if (piece! = null) {for (int i = 1; i <= x-sumbu panjang; i ++) {for (int j = 1; j <= y-sumbu panjang; j ++) {x = point [i] [j] .getX (); y = point [i] [j] .gety (); if (rect.contains (x, y)) {containchesspoint = true; m = i; n = j; merusak; }}}}} if (piece! = null && containschesspoint) {color pieceColor = piece.get piece.get color (); if (point [m] [n] .ispiece ()) {color c = (titik [m] [n] .getpiece ()). Dapatkan warna catur (); if (pieceColor.getrgb () == c.getrgb ()) {PITE.SetLocation (startx, starty); (poin [startI] [startj]). Set memiliki potongan catur (true); } else {boolean ok = aturan.movepiecerule (piece, starti, startj, m, n); if (ok) {cyspiece pieceremoved = point [m] [n] .getpiece (); titik [m] [n] .removepiece (piecemoved, this); titik [m] [n] .setpiece (bagian, ini); (poin [startI] [startj]). Set memiliki potongan catur (false); skor catur Record.Record (Piece, Starti, Startj, M, N); Record. Catat potongan catur yang dimakan (piecemoved); aturan.iswine (piecemoved); if (piece.paper Category (). Equals (Red Square Color)) {Red Square Move = FALSE; Black Square Move = True; } if (piece.paper Category (). Equals (Black Square Color)) {Black Square Move = FALSE; Move Square Merah = Benar; } validasi (); ulang (); } else {PICE.SetLocation (startx, starty); (poin [startI] [startj]). Set memiliki potongan catur (true); }}} else {boolean ok = aturan .movepiecerule (piece, start, startj, m, n); if (ok) {point [m] [n] .setpiece (piece, this); (poin [startI] [startj]). Set memiliki potongan catur (false); skor catur Record.Record (Piece, Starti, Startj, M, N); rekaman. Potongan catur yang dimakan ("tidak ada potongan catur"); if (piece.paper Category (). Equals (Red Square Color)) {Red Square Chess = false; Black Square Chess = true; } if (piece.paper Category (). Equals (Black Square Color)) {Black Square Chess = false; Catur persegi merah = true; }} else {PICE.SetLocation (startx, starty); (poin [startI] [startj]). Set memiliki potongan catur (true); }}} if (piece! = null &&! containchesspoint) {piece.setlocation (startx, starty); (poin [startI] [startj]). Set memiliki potongan catur (true); }}} public void mouseEntered (mouseEvent e) {} public void mouseExited (mouseEvent e) {} public void mouseclicked (mouseEvent e) {}} 3. File Chesspiece.java
paket cn.edu.ouc.chinesechess; impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; / ** * kelas catur * * @author cnlht */ kelas publik Chesspiece memperluas jlabel {string name; // nama catur warna backcolor = null, forecolor; // latar belakang dan foreground warna string warna kategori = null; Papan papan catur = null; Int Width, Height; // Ukuran Chesspiece Publik (Nama String, Warna FC, Warna BC, Lebar Int, Tinggi Int, Papan Catur) {// Bangun potongan catur this.name = name; this.board = board; this.width = lebar; this.height = tinggi; forecolor = fc; backcolor = bc; setsize (lebar, tinggi); Securbackground (BC); addMouseMotionListener (papan); AddMouseListener (papan); } // menggambar catur cat public void cat (grafik g) {g.drawimage (board.pieceimg, 2, 2, width-2, height-2, null); g.setColor (forecolor); G.SetFont (font baru ("Kaiti", Font.Bold, 26)); g.drawstring (nama, 7, tinggi - 8); // gambar "nama catur" pada bagian catur g.setColor (color.black); //g.drawoval(1, 1, lebar - 1, tinggi - 1); float linewidth = 2.3f; ((Graphics2d) g) .setstroke (New BasicStroke (linewidth)); ((Grafik2d) g) .drawoval (2, 2, lebar-2, tinggi-2); } public int getWidth () {return width; } public int getHeight () {return height; } public string getName () {return name; } warna publik dapatkan warna catur () {return forecolor; } public void set Catur kategori (kategori string) {Color Category = Category; } Public String Catur Kategori () {Kategori Warna Kembali; }} 4. File Koordinat Point Poece Piece
paket cn.edu.ouc.chinesechess; / *** kelas titik catur** @author cnlht*/ kelas publik CHESSPoint {/ ** Koordinat Chesspiece*/ int x, y; / ** Apakah koordinat ini memiliki bagian*/ boolean memiliki bagian; /** CHESSPIECE PIECE = NULL; / ** papan catur yang dimiliki koordinat*/ papan catur = null; Public Chesspoint (int x, int y, boolean boo) {this.x = x; this.y = y; Memiliki bagian = boo; } public boolean ispiece () {return ada potongan catur; } public void set memiliki potongan catur (boolean boo) {catur potongan = boo; } public int getX () {return x; } public int gety () {return y; } // Setel poin Ubah ke potongan catur public void setpiece (potongan catur, papan catur) {this.board = board; this.piece = piece; board.add (bagian); int w = (board.unitwidth); int h = (board.unitheight); PICE.SETBOUNDS (X - W / 2, Y - H / 2, W, H); // POSISI CHESS PIECE, Lebar, Tinggi dengan Potongan Catur = Benar; board.validate (); } getpiece catur publik () {return piece; } public void RemovePiece (potongan catur, papan catur) {this.board = board; this.piece = piece; board.remove (sepotong); board.validate (); potongan catur = false; }} 5. GAMEPLAY Rule kelas file aturan.java
paket cn.edu.ouc.chinesechess; impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; / ** * kelas aturan catur-playing * * @author cnlht */ aturan kelas publik {papan catur = null; Cyspiece piece = null; Titik catur [] []; int starti, startj, endi, endj; aturan publik (papan papan catur, titik chiSpoint [] []) {this.board = board; this.point = point; } public void iswine (cyspiece piece) {this.piece = piece; if (PICE.GetName () == "ALIGNED" || PICE.GetName () == "Handy") {if (PICE.COLOR Category == "Sisi Merah") {JOptionPane.ShowMessagedialog (Null, "Black Side Victory!"); } else {joptionpane.showmessageDialog (null, "Red Side Victory!"); }}} public boolean movepiecerule (potongan catur, 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 bisakah kamu bermain catur = false; if (PICE.GetName (). Equals ("car")) {if (startI == endi) {int j = 0; for (j = Minj+1; j <= maxj - 1; j ++) {if (point [startI] [j] .ispiece ()) {Bisakah Anda memindahkan catur = false; merusak; }} if (j == maxj) {Bisakah Anda memindahkan catur = true; }} lain jika (startj == endj) {int i = 0; untuk (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {Bisakah Anda memindahkan catur = false; merusak; }} if (i == maxi) {Bisakah Anda memindahkan catur = true; }} else {bisakah Anda memindahkan catur = false; }}} else if (PICE.GetName (). Equals ("")) {if (startI == endi) {int j = 0; for (j = Minj+1; j <= maxj - 1; j ++) {if (point [startI] [j] .ispiece ()) {Bisakah Anda memindahkan catur = false; merusak; }} if (j == maxj) {Bisakah Anda memindahkan catur = true; }} lain jika (startj == endj) {int i = 0; untuk (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {Bisakah Anda memindahkan catur = false; merusak; }} if (i == maxi) {dapatkah saya memindahkan catur = true; }} else {dapatkah saya memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("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 ()) {dapat saya memindahkan catur = false; } else {dapatkah saya memindahkan catur = true; }} if (endi <startI) {if (point [startI - 1] [startj] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }}} lain if (xaxle == 1 && yaxle == 2) {if (endj> startj) {if (point [startI] [startj + 1] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }} if (endj <startj) {if (point [startI] [startj - 1] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }} if (endj <startj) {if (point [startI] [startj - 1] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; } BENAR; }}} else {dapatkah saya memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("")) {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 ()) {dapat saya memindahkan catur = false; } else {dapatkah saya memindahkan catur = true; }} if (endi <startI) {if (point [startI - 1] [startj] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }}} lain if (xaxle == 1 && yaxle == 2) {if (endj> startj) {if (point [startI] [startj + 1] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }} if (endj <startj) {if (point [startI] [startj - 1] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }}} else {dapatkah Anda memindahkan catur = false; }} lain if (PICE.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) {if (point [centeri] [centerj] .ispiece ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }} else {bisakah Anda memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("fase")) {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 ()) {dapatkah Anda memindahkan catur = false; } else {bisakah kamu memindahkan catur = true; }} else {bisakah Anda memindahkan catur = true; }} else {bisakah Anda memindahkan catur = true; }} else {bisakah Anda memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("Cannon")) {int number = 0; if (startI == endi) {int j = 0; untuk (j = pinjikan+1; j <= maxj - 1; j ++) {if (point [startI] [j] .ispiece ()) {angka ++; }} if (number> 1) {Bisakah Anda memindahkan catur = false; } else if (number == 1) {if (point [endi] [endj] .ispiece ()) {dapatkah Anda memindahkan catur = true; }} lain if (number == 0 &&! point [endi] [endj] .ispiece ()) {Bisakah Anda memindahkan catur = true; }} lain jika (startj == endj) {int i = 0; untuk (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {angka ++; }} if (number> 1) {Bisakah Anda memindahkan catur = false; } else if (number == 1) {if (point [endi] [endj] .ispiece ()) {dapatkah Anda memindahkan catur = true; }} lain if (number == 0 &&! point [endi] [endj] .ispiece ()) {Bisakah Anda memindahkan catur = true; }} else {bisakah Anda memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("Soldier")) {int xaxle = math.abs (startI - endi); int yaxle = math.abs (startj - endj); if (endj> = 6) {if (startj - endj == 1 && xaxle == 0) {Bisakah Anda memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }} lain if (endj <= 5) {if ((startj - endj == 1) && (xaxle == 0)) {dapatkah saya memindahkan catur = true; } else if ((endj - startj == 0) && (xaxle == 1)) {dapatkah saya memindahkan catur = true; } else {dapatkah saya memindahkan catur = false; }}} lain if (PICE.GetName (). Equals ("Zhi")) {int xaxle = math.abs (startI - endi); int yaxle = math.abs (startj - endj); if (endj <= 5) {if (endj - startj == 1 && xaxle == 0) {dapatkah saya memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }} lain if (endj> = 6) {if ((endj - startj == 1) && (xaxle == 0)) {Bisakah Anda memindahkan catur = true; } else if ((endj - startj == 0) && (xaxle == 1)) {Bisakah Anda memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }}} lain if (PICE.GetName (). Equals ("shi")) {int xaxle = math.abs (startI - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {Bisakah Anda memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }} lain if (PICE.GetName (). Equals ("state")) {int xaxle = math.abs (startI - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {Bisakah Anda memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }} else if ((PICE.GetName (). Equals ("Handsome")) || (PICE.GetName (). Equals ("Replace"))) {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)) {Bisakah Anda memindahkan catur = true; } else {dapatkah Anda memindahkan catur = false; }} else {bisakah Anda memindahkan catur = false; }} return, bisakah Anda memindahkan catur; }} 6. File Movestep.java
paket cn.edu.ouc.chinesechess; impor java.awt.point; / ** * Kelas berjalan * * @author cnlht * */ kelas publik Movestep mengimplementasikan java.io.serializable {public point pstart, Pend; public movestep (titik p1, titik p2) {pStart = p1; pend = p2; }} 7. Buat cysmanual.java
paket cn.edu.ouc.chinesechess; impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; impor java.util.linkedlist; / ** * membuat kelas rekaman catur * * @author cnlht */ kelas publik makeChessmanual memperluas jpanel mengimplementasikan actionListener {jTextArea text = null; Jscrollpane scroll = null; Papan papan catur = null; Titik catur [] []; LinkedList Catur Catatan = NULL; LinkedList memakan potongan catur = null; Jbutton Buttonundo; int i = 0; Public MakechessManual (CHESSBoard Board, ChessPoint [] [] point) {this.board = board; this.point = point; text = new jTextArea (); gulir = jscrollpane baru (teks); Note catur = LinkedList baru (); Makan potongan catur = LinkedList baru (); Buttonundo = JButton baru ("Catur Terjawab"); Buttonundo.setfont (font baru ("Lishu", Font.Plain, 18)); setLayout (borderlayout baru ()); Tambah (gulir, borderlayout.center); Tambah (Buttonundo, BorderLayout.South); Buttonundo.addactionListener (ini); } public char numberToletter (int n) {char c = '/0'; switch (n) {case 1: c = 'a'; merusak; Kasus 2: C = 'B'; merusak; Kasus 3: C = 'C'; merusak; Kasus 4: C = 'D'; merusak; Kasus 5: C = 'E'; merusak; Kasus 6: c = 'f'; merusak; Kasus 7: C = 'G'; merusak; Kasus 8: C = 'H'; merusak; Kasus 9: c = 'i'; merusak; Kasus 10: C = 'J'; merusak; } return c; } public void rekor skor catur (cyspiece piece, int starti, int startj, int endi, int endj) {point pStart = new point (startI, startj); Titik pend = titik baru (endi, endj); MOVESTEP LANGKAH = MOVESTEP BARU (PSTART, PEND); Skor catur.add (langkah); String catur kategori = PICE.CHESS Category (); String name = PICE.GetName (); String m = "#" + Kategori Catur + Nama + ":" + startI + numberToletter (startj) + "ke" + endi + numberToletter (endj); Text.Append (M); if (piece. catur kategori (). Equals (board.black square color)) text.append ("/n"); } public void Rekam potongan catur yang dimakan (objek objek) {dimakan catur potongan.add (objek); } Public LinkedList dapatkan catur () {return catur karya; } public void actionPerformed (actionEvent e) {int position = text.getText (). lastIndexof ("#"); if (position! = -1) text.replacerange ("", position, text.getText (). length ()); if (catur potong.size ()> 0) {movestep laststep = (movestep) catur potongan.getLast (); Catur record.removelast (); Object qizi = makan sepotong catur.getLast (); Makan sepotong catur.removelast (); String 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; Cyspiece piece = point [endi] [endj] .getpiece (); point [startI] [startj] .setpiece (piece, board); (titik [endi] [endj]). Set memiliki potongan catur (false); 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 dihapuspiece = (cyspiece) qizi; int starti = laststep.pstart.x; int startj = laststep.pstart.y; int endi = laststep.pend.x; int endj = laststep.pend.y; Cyspiece piece = point [endi] [endj] .getpiece (); point [startI] [startj] .setpiece (piece, board); titik [endi] [endj] .setpiece (dihapus, papan); (titik [endi] [endj]). Set memiliki potongan catur (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.*; impor 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 (borderlayout baru ()); 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记录棋谱的方法。
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。