Dieser Artikel beschreibt den von Java implementierten chinesischen Schachspielcode. Es wird Ihnen als Referenz mit Ihnen geteilt. Der spezifische Code ist wie folgt
1. Praktischer Zweck:
1. Anwendung und Unterschied zwischen Mausklick, Drag und anderen Ereignissen
2. Speichern und Lesen von Schachdatensätzen
3.. Verbessern Sie die Schachregeln.
2. Praktischer Inhalt:
Chinesisches Schach hat eine lange Geschichte und hat unzählige Menschen zum Studium angezogen. Jetzt haben wir das folgende Design und die folgende Erklärung der Schlachten des chinesischen Schaches und der Produktion von Schachwerten für Ihre Referenz und Lernen gemacht.
1. Beim Spielen von Schach zwischen Maschinen und Maschinen übernimmt die rote Seite die Führung. Ziehen Sie das Schachstück zum Ziel, wenn es den Regeln erfüllt und das Eichhörnchen fallen gelassen wird.
Jeder spielt Schach
2. Machen Sie eine Schachaufzeichnung. Nach der Auswahl des Schachdatensatzes beginnt das Spiel und der Schachprozess wird aufgezeichnet.
Wählen Sie das Menü "Schachdatensatz machen"
Die rote Seite gewann, nachdem der Schachwert beendet war
Nach dem Gewinn einer Seite taucht das Dialogfeld zur Siegesnachricht auf. Wählen Sie nach dem Klicken auf OK das Menü "Schachdatensatz speichern" und das Dialogfeld Datei speichern.
Speichern Sie das Dialogfeld "Schachdatensatz"
3. Demonstrieren Sie den Schachwert. Nach Auswahl des Demonstrations -Schach -Score -Menüs, einem geöffneten Dialogfeld, wählen Sie die gespeicherte Schachbewertung aus, um die Demonstration zu starten.
Demonstrieren Sie das Dialogfeld "Schachdatensatz"
Demonstrieren Sie den Schachdatensatzprozess (automatisch und manuell)
3. Referenzcode:
1. Schach Hauptdatei ChineSechess.java
Paket cn.edu.ouc.chineSechess; Javax.swing importieren.*; import Java.awt.*; import Java.awt.event.*; import Java.io.*; import Java.util.linkedList; / ** * CHESS -Hauptklasse * * @Author Cnlht */ Public Class ChineSeChess erweitert JFrame implements actionListener {chessboard board = null; Dämon Dämon = Null; Makechessmanual record = null; Container con = null; JMenubar Bar; Jmenu Filemenu; JMenuitem macht Schachbewertung, speichern Sie die Schachbewertung, zeigt die Schachbewertung. JFilechooser filechooser = null; LinkedList Schach Score = NULL; public chineSechess () {bar = new JMenubar (); filemenu = neuer jmenu ("chinesisches Schach"); Schach Score = New JMenuitem ("Schachbewertung machen"); Schach Score speichern = new JMenuitem ("Schachbewertung speichern"); Schach Score speichern. Demonstration Schach Score = New JMenuitem ("Demonstration Schach Score"); filemenu.add (Schachbewertung machen); filemenu.add (Schachbewertung speichern); filemenu.add (Schachbewertung demonstrieren); bar.add (filemenu); setjMenubar (bar); settitle (make chess Score.getText ()); Schach Score machen. Schachspeicher speichern. Demonstrieren Sie den Schach Score.AddactionListener (this); Board = neues Schachbrett (45, 45, 9, 10); record = board.record; con = getContentPane (); JSplitPane split = new JSplitPane (jSplitPane.Horizontal_split, true, board, record); Split.SetDividErsize (5); Split.SetDividerLocation (460); con.add (split, borderLayout.Center); AddWindowlistener (neuer Fensteradapter () {public void windowsClosing (windowEvent e) {System.exit (0);}}); setvisible (wahr); Setbounds (60, 20, 690, 540); filechooser = new JFilechooSer (); con.Validate (); bestätigen(); } public void actionPerformed (actionEvent e) {if (e.getSource () == Make Chess Score) {con.removeall (); Schachspeicher speichern. this.settitle (erstellen schach. Board = neues Schachbrett (45, 45, 9, 10); record = board.record; JSplitPane split = new JSplitPane (jSplitPane.Horizontal_split, true, board, record); Split.SetDividErsize (5); Split.SetDividerLocation (460); con.add (split, borderLayout.Center); bestätigen(); } if (e.getSource () == Schachbewertung speichern) {int state = filechooser.showSavedialog (null); Datei 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 Ereignis) {}}} if (e.getSource () == Demonstrieren Sie die Schachbewertung) {con.removeall (); con.Repaint (); con.Validate (); bestätigen(); Schach Score speichern. int state = filechooser.showopendialog (null); Datei openFile = filechooser.getSelectedFile (); if (openFile! = null && state == jFilechooSer.Approve_Option) {try {fileInputStream inon = new FileInputStream (openFile); ObjectInputStream intWO = new ObjectInputStream (inone); Schess = (linkedList) intwo.readObject (); in aNone.close (); intwo.close (); Schachbrett = neues Schachbrett (45, 45, 9, 10); Dämon = neuer Dämon (Vorstand); Dämon.set -Schachdatensatz (Schachdatensatz); con.add (Demon, BorderLayout.Center); con.Validate (); bestätigen(); this.settitle (Demo -Schachrekord.GeteText () + ":" + openFile); } catch (Ausnahmeereignis) {jlabel label = new JLabel ("nicht Schachdatendatei"); Label.SetFont (neue Schriftart ("Lishu", font.bold, 60)); label.setforenground (color.red); label.sethorizontalalignment (SwingConstants.Center); con.add (label, borderLayout.Center); con.Validate (); this.settitle ("No Chess Record"); bestätigen(); }} else {jlabel label = new JLabel ("Keine Schachdatendatei wird nicht geöffnet"); Label.SetFont (neue Schriftart ("Lishu", font.bold, 50)); label.setforenground (color.pink); label.sethorizontalalignment (SwingConstants.Center); con.add (label, borderLayout.Center); con.Validate (); this.settitle ("Die Schachdatei ist nicht geöffnet"); bestätigen(); }}} public static void main (String args []) {new ChineSCHESS (); }} 2. Schachboard.java -Datei
Paket cn.edu.ouc.chineSechess; Javax.swing importieren.*; import Java.awt.*; import Java.awt.event.*; / ** * Schachbrettklasse * * @Author Cnlht */ Public Class Chessboard erweitert JPanel implementiert Mouselistener, MousemotionListener {public chesspoint point [] []; öffentliche Int Unitwidth, Unitheight; private int x-Achsenlänge, y-Achselänge; Privat int x, y; privates Bild img; geschütztes Bild pieffisch; private boolesche Bewegung = falsch; Public String Red Square Color = "Red Square", Black Square Color = "Black Square"; Schachstück rotes Karren 1, roter Karren 2, rotes Pferd 1, rotes Pferd 2, rotes Pferd 1, rotes Pferd 2, rotes Pferd 3, rote Soldaten 4, rote Soldaten 5, Rote Kanone 1, rote Kanone 2; Schachstück schwarzer Wagen 1, schwarzer Wagen 2, schwarzes Pferd 1, schwarzes Pferd 2, schwarzer General, schwarze Soldaten 1, schwarze Soldaten 2, schwarze Soldaten 3, schwarze Soldaten 4, schwarze Soldaten 5, schwarzer Elefant 1, schwarzer Elefant 2, Black Gun 1, Black Cannon 2; int startx, starty; int starti, startj; Public Boolean Red Side Chess = wahre, schwarze Seite Schach = Falsch; Regelregel = null; öffentliche Makechessmanual -Aufzeichnung = NULL; öffentliches Schachbrett (int w, int h, int r, int c) {setLayout (null); addmouselistener (dies); addmousemotionListener (this); Farbe BC = GetBackground (); UNITWIDTH = W; Unitheight = H; X-Achse Länge = R; Y-Achselänge = C; Punkt = neuer Schachpunkt [R + 1] [C + 1]; für (int i = 1; i <= r; i ++) {für (int j = 1; j <= c; j ++) {point [i] [j] = neuer Schachpunkt (i * Unitwidth, j * Unitheight, False); }} regel = neue Regel (this, point); record = new makechessmanual (this, point); IMG = Toolkit.getDefaulttoolkit (). getImage ("board.jpg"); pepimg = toolkit.getDefaulttoolkit (). getImage ("piece.gif"); rotes Auto 1 = neues Schachstück ("", color.red, bc, w - 4, h - 4, this); Rotes Auto 1. Schach -Stück -Kategorie (rote quadratische Farbe); Rotes Auto 2 = neues Schachstück ("", color.red, bc, w - 4, h - 4, this); Rotes Auto 2.Set Chess Stück Kategorie (rote quadratische Farbe); Rotes Pferd 1 = neues Schachstück ("", color.red, bc, w - 4, h - 4, this); Rotes Pferd 1 = neues Schachstück ("", color.red, bc, w - 4, h - 4, this); Red Horse 2.Set Chess Stück Kategorie (rote quadratische Farbe); Red Cannon 1 = neues Chessprung ("火", color.red, bc, w - 4, h - 4, this); Rote Kanone 1.Set Chess Stück Kategorie (rote quadratische Farbe); Red Cannon 2 = neues Schachstück ("火", color.red, bc, w - 4, h - 4, this); Rote Kanone 2.Set Chess Stück Kategorie (rote quadratische Farbe); Red Cannon 1 = neues Chessprung ("火", color.red, bc, w - 4, h - 4, this); Rote Kanone 1.Set Chess Stück Kategorie (rote quadratische Farbe); Red Cannon 2 = neues Schachstück ("火", color.red, bc, w - 4, h - 4, this); Rote Kanone 1.Set Chess Stück Kategorie (rote quadratische Farbe); Red Cannon 2 = neues Schachstück ("火", color.red, bc, w - 4, h - 4, this); Rot 2.set chess piece category (rote quadratfarbe); Rot 1 = neues Chessstück ("shi", color.red, bc, w - 4, h - 4, this); Rot 1.Set Chess Stück Kategorie (rote quadratische Farbe); Rot 2 = neues Chessstück ("shi", color.red, bc, w - 4, h - 4, this); Rot 2.set chess piece category (rote quadratfarbe); Rot 2.set chess piece category (rote quadratfarbe); Rot 2 = neues Chessstück ("shi", color.red, bc, w - 4, h - 4, this); Rot 1 = neues Chessstück ("shi", color.red, bc, w - 4, h - 4, this); Red Soldier 1.Set Chess Stück Kategorie (rote quadratische Farbe); Roter Soldat 2 = neues Schachstück ("Soldat", color.red, bc, w - 4, h - 4, this); Red Soldier 2.Set Chess Stück Kategorie (rote quadratische Farbe); Red Soldier 3 = neues Schachstück ("Soldier", Color.red, BC, W - 4, H - 4, This); Red Soldier 3 = neues Schachstück ("Soldier", Color.red, BC, W - 4, H - 4, This); Red Soldier 4. Set Chess Stück Kategorie (rote quadratische Farbe); Red Soldier 5 = neues Chessstück ("Soldier", Color.red, BC, W - 4, H - 4, This); Red Soldier 5.Set Chess Stück Kategorie (rote quadratische Farbe); Schwarzer Soldat = neues Cheske ("verkauft", color.black, bc, w - 4, h - 4, this); Schwarzer Soldat 1 = neues Chesspritzstück ("Soldier", Color.black, BC, W - 4, H - 4, This); Schwarzer Soldat 1 = neues Chesspritzstück ("Soldier", Color.black, BC, W - 4, H - 4, This); Schwarzer Soldat 1 = neues Schachstück ("Car", Color.black, BC, W - 4, H - 4, This); Schwarzes Auto 1. Schach -Stück -Kategorie (schwarze quadratische Farbe); Schwarzes Auto 2 = neues Schachstück ("Auto", color.black, bc, w - 4, h - 4, this); Schwarzes Auto 2. Set Chess Stück Kategorie (schwarz quadratische Farbe); Black Cannon 1 = neues Schachstück ("Cannon", Color.black, BC, W - 4, H - 4, This); Schwarze Kanone 1.Set Chess Stück Kategorie (schwarze quadratische Farbe); Black Cannon 2 = neues Schachstück ("Cannon", Color.black, BC, W - 4, H - 4, This); Schwarze Kanone 2.Set Chess Stück Kategorie (schwarze quadratische Farbe); Schwarzer Elefant 1 = neues Schachstück ("Elefant", Color.black, BC, W - 4, H - 4, This); Schwarzer Elefant 1. Schach -Stück -Kategorie (schwarze quadratische Farbe); Schwarzer Elefant 2 = neues Schachstück ("Elefant", color.black, bc, w - 4, h - 4, this); Schwarzer Elefant 2.Set Chess Stück Kategorie (schwarze quadratische Farbe); Schwarzer Elefant 1 = neues Schachstück ("Elefant", Color.black, BC, W - 4, H - 4, This); Schwarzer Elefant 1. Schach -Stück -Kategorie (schwarze quadratische Farbe); Schwarzer Elefant 2 = neues Schachstück ("Elefant", color.black, bc, w - 4, h - 4, this); Schwarzes Pferd 2.Set Chess Stück Kategorie (schwarze Farbe); Schwarze Hosen 1 = neues Chesspritzstück ("Zhi", Color.black, BC, W - 4, H - 4, this); Schwarze Hosen 1. Schachkategorie (schwarze Farbe); Schwarze Hosen 2 = neues Chesspritzstück ("Zhi", Color.black, BC, W - 4, H - 4, this); Schwarze Hosen 2. Schach -Stück -Kategorie (schwarze Farbe); Schwarze Hosen 3 = neues Chessprung ("Zhi", color.black, bc, w - 4, h - 4, this); Schwarze Hosen 3. Set Chess Stück Kategorie (schwarze Farbe); Schwarze Hosen 4 = neues Chessstück ("Zhi", color.black, bc, w - 4, h - 4, this); Schwarzes Phantom 4.Set Chess Stück Kategorie (schwarze quadratische Farbe); Schwarzes Phantom 5 = neues Schachstück ("Zhi", Color.black, BC, W - 4, H - 4, This); Schwarzes Phantom 5.Set Chess Stück Kategorie (schwarze quadratische Farbe); Punkt [1] [10] .Setpiece (roter Wagen 1, dies); Punkt [2] [10] .Setpiece (Red Horse 1, this); Punkt [3] [10] .Setpiece (rote Phase 1, dies); Punkt [4] [10] .Setpiece (roter Salbei 1, dies); Punkt [4] [10] .Setpiece (roter Salbei 1, dies); Punkt [4] [10] .Setpiece (roter Salbei 1, dies); Punkt [5] [10] .Setpiece (Red Soldier, this); Punkt [6] [10] .Setpiece (Red Soldier 2, this); Punkt [7] [10] .Setpiece (Red Soldier 2, this); Punkt [8] [10] .Setpiece (Red Horse 2, this); Punkt [9] [10] .Setpiece (Red Soldier 2, this); Punkt [2] [8] .Setpiece (Red Soldier 1, this); Punkt [8] [8] .Setpiece (Red Soldier 2, this); Punkt [1] [7] .Setpiece (Red Soldier 2, this); Punkt [3] [7] .Setpiece (Red Soldier 2, this); Punkt [3] [7] .Setpiece (Red Soldier 2, this); Punkt [3] [7] .Setpiece (Red Soldier 2, this); Punkt [5] [7] .Setpiece (Red Soldier 3, this); Punkt [7] [7] .Setpiece (Red Soldier 4, this); Punkt [9] [7] .Setpiece (Red Soldier 5, this); Punkt [1] [1] .Setpiece (schwarzes Auto 1, dies); Punkt [2] [1] .Setpiece (schwarzes Pferd 1, dies); Punkt [3] [1] .Setpiece (schwarzer Elefant 1, dies); Punkt [4] [1] .Setpiece (schwarzer Elefant 1, dies); Punkt [5] [1] .Setpiece (Black General, this); Punkt [6] [1] .Setpiece (schwarzer Elefant 1, dies); Punkt [4] [1] .Setpiece (Black General 1, this); Punkt [5] [1] .Setpiece (Black General 2, this); Punkt [6] [1] .Setpiece (Black General 2, this); Punkt [7] [1] .Setpiece (schwarzer Elefant 2, dies); Punkt [8] [1] .Setpiece (schwarzes Pferd 2, dies); Punkt [9] [1] .Setpiece (schwarzes Auto 2, dies); Punkt [2] [3] .Setpiece (Black Cannon 1, this); Punkt [8] [3] .Setpiece (Black Cannon 2, this); Punkt [1] [4] .Setpiece (Black Cannon 1, this); Punkt [3] [4] .Setpiece (Black Cannon 2, this); Punkt [5] [4] .Setpiece (Black Cannon 3, this); Punkt [7] [4] .Setpiece (Black Cannon 4, this); Punkt [7] [4] .Setpiece (Black Cannon 4, this); Punkt [3] [4] .Setpiece (Black Cannon 4, this); Punkt [5] [4] .Setpiece (Black Cannon 3, this); Punkt [7] [4] .Setpiece (Black Cannon 4, this); Punkt [9] [4]. } public void PaintComponent (Grafik g) {Super.PaintComponent (g); int imgwidth = img.getwidth (this); int imgHeight = img.Getheight (this); // Holen Sie sich die Breite und Höhe des Bildes int fwidth = getWidth (); int fheight = getheight (); // Holen Sie sich die Breite und Höhe des Fensters int x = (fwidth - imgwidth)/ 2; int y = (fheight - imgheight) / 2; g.drawimage (img, x, y, null); für (int j = 1; j <= y-axislänge; j ++) {g.drawline (punkt [1] [j] .x, punkt [1] [j] .y, point [x-achselänge] [j] .x, Punkt [x-axislänge] [j] .y); } für (int i = 1; i <= x-achse Länge; i ++) {if (i! = 1 && i! = x-axislänge) {g.drawline (point [i] [1] .x, point [i] [1] .y, point [i] [y-axis Länge-5] .x, Punkt [i] [y-axislänge). G.Drawline (Punkt [i] [y-axislänge-4] .x, Punkt [i] [Y-Achsenlänge-4] .Y, Punkt [i] [Y-Achselänge] .x, Punkt [i] [Y-Achselänge] .y); } else {g.drawline (point [i] [1] .x, point [i] [1] .y, point [i] [y-achselänge] .x, punkt [i] [y-achselänge] .y); }} g.drawline (Punkt [4] [1] .x, Punkt [4] [1] .Y, Punkt [6] [3] .x, Punkt [6] [3] .y); G.Drawline (Punkt [6] [1] .x, Punkt [6] [1] .Y, Punkt [4] [3] .x, Punkt [6] [1] .Y, Punkt [4] [3] .x, Punkt [4] [3] .Y); G.Drawline (Punkt [4] [8] .x, Punkt [4] [8] .Y, Punkt [6] [Y-Achselänge] .x, Punkt [6] [Y-Achselänge] .Y); G.Drawline (Punkt [4] [Y-Achselänge] .x, Punkt [4] [Y-Achselänge] .Y, Punkt [6] [8] .x, Punkt [6] [8] .Y); für (int i = 1; i <= x-achse Länge; i ++) {g.drawstring (""+i, i * Unitwidth, Unitheight / 2); } int j = 1; für (char c = 'a'; c <= 'j'; J ++; }} / ** Mausdruckereignis* / public void mousepression (mouseEvent e) {chespiece piece = null; Rechteck rect = null; if (e.getSource () == this) move = false; if (move == false) if (e.getSource () Instanz des Schachstücks) {piece = (chespiece) e.getSource (); startx = piece.getBounds (). x; starty = piece.getBounds (). y; rect = piece.getBounds (); für (int i = 1; i <= x-axislänge; i ++) {für (int j = 1; j <= y-axislänge; j ++) {int x = point [i] [j] .getX (); int y = point [i] [j] .gety (); if (rect.contains (x, y)) {starti = i; startj = j; brechen; }}}}}} public void mousemoved (MouseEvent e) {} / ** Maus -Drag -Ereignis* / public void mouseDragged (MouseEvent e) {Schachstück piece = null; if (e.getSource () Instanz des Schachstücks) {piece = (chespiece) e.getSource (); Bewegung = wahr; e = Swingutilities.ConvertMouseeEvent (Stück, e, this); } if (e.getSource () == this) {if (move && piece! = null) {x = e.getX (); y = e.gety (); if (rotes quadratisches Schach && ((piece.chess subcategory ()). Equals (rote quadratische Farbe))) {piece.setLocation (x - piece.getWidth () / 2, y - piece.getheight () / 2); } if (Black Chess Move && (piece.Chess class (). Equals (schwarze Schriftfarbe))) {piece.setLocation (x - piece.getWidth () / 2, y - piece.getheight () / 2); }}}} / ** Mausereignis veröffentlichen* / public void mouSerelest (MouseEvent e) {chespiece piece = null; Bewegung = falsch; Rechteck rect = null; if (e.getSource () Instanz des Schachstücks) {piece = (chespiece) e.getSource (); rect = piece.getBounds (); e = Swingutilities.ConvertMouseeEvent (Stück, e, this); } if (e.getSource () == this) {boolean containSchesspoint = false; int x = 0, y = 0; int m = 0, n = 0; if (piece! y = point [i] [j] .gety (); if (rect.contains (x, y)) {containSchesspoint = true; M = i; n = j; brechen; }}}}} if (piece! if (punkt [m] [n] .ispiece ()) {color c = (punkt [m] [n] .Getpiece ()). Farbe von Schach () erhalten; if (Prisecolor.getRGB () == C.GetRGB ()) {piece.setLocation (startx, starty); (Punkt [starti] [startj]). Set hat Schachsteine (wahr); } else {boolean ok = regel.movepiecerule (piece, starti, startj, m, n); if (ok) {chespiece piefteremoved = point [m] [n] .GetPiece (); Punkt [m] [n]. Punkt [m] [n] .Setpiece (Stück, this); (Punkt [starti] [startj]). Set hat Schachsteine (falsch); Record.Record Chess Score (Stück, Starti, Startj, M, N); aufnehmen. Erfahren Sie die gegessenen Schachstücke (stückweise); Regel.iswine (stückweise); if (piece.Paper category (). Equals (Red Square Color)) {Red Square Move = False; Schwarzes quadratisch move = true; } if (piece.Paper category (). Equals (Black Square Color)) {Black Square Move = False; Red Square Move = True; } validate (); Repaint (); } else {piece.setLocation (startx, starty); (Punkt [starti] [startj]). Set hat Schachsteine (wahr); }}} else {boolean ok = regel. if (ok) {point [m] [n] .setpiece (piece, this); (Punkt [starti] [startj]). Set hat Schachsteine (falsch); Record.Record Chess Score (Stück, Starti, Startj, M, N); Record.Record -Schachstücke gegessen ("Keine Schachstücke"); if (piece.Paper category (). Equals (Red Square Color)) {Red Square Chess = False; Schwarzes quadratisches Schach = wahr; } if (piece.Paper category (). Equals (schwarze quadratische Farbe)) {schwarzer quadratischer Schach = False; Rotes quadratisches Schach = wahr; }} else {piece.setLocation (startx, starty); (Punkt [starti] [startj]). Set hat Schachsteine (wahr); }}} if (piece! (Punkt [starti] [startj]). Set hat Schachsteine (wahr); }}} public void mausented (MouseEvent e) {} public void mouseexited (MouseEvent e) {} public void mouseclicked (MouseEvent e) {}}} 3.
Paket cn.edu.ouc.chineSechess; Javax.swing importieren.*; import Java.awt.*; import Java.awt.event.*; / ** * Schachklasse * * @Author Cnlht */ Public Class Chespiece erweitert JLabel {String Name; // Schachname Farbe BackColor = Null, Prognosen; // Hintergrund und Vordergrundfarbe Farbkategorie = NULL; Schachbrett = NULL; int width, Höhe; // Größe öffentliches Chessstück (Stringname, Farbe FC, Farbe BC, int Breite, inthöhe, Schachbrett) {// Konstruieren Sie das Schachstück this.name = name; this.board = board; this.width = width; this.height = Höhe; procesolor = fc; BackColor = BC; SetSize (Breite, Höhe); SetBackground (BC); AddMousemotionListener (Board); Addmouselistener (Board); } // Schachstück zeichnen public void Paint (Grafik g) {g.drawimage (board.pieceImg, 2, 2, width-2, height-2, null); G.SetColor (Prognose); G.SetFont (neue Schriftart ("Kaiti", Font.bold, 26)); G.Drawstring (Name, 7, Höhe - 8); // "Schachname" auf dem Schachstück G.SetColor (color.black) zeichnen; //g.drawval(1, 1, Breite - 1, Höhe - 1); float linewidth = 2,3f; ((Graphics2d) g) .setStroke (neuer Basicictroke (Linewidth)); ((Graphics2d) G) .Drawalval (2, 2, Breite-2, Höhe-2); } public int getWidth () {Rückkehr Breite; } public int getheight () {return Height; } public String getName () {return name; } public Color Get the Chess Color () {return prognolor; } public void Set Schachkategorie (String -Kategorie) {Farbkategorie = Kategorie; } public String chess category () {return farb category; }} 4..
Paket cn.edu.ouc.chineSechess; / *** Schachpunktklasse** @author cnlht*/ public class schachpoint {/ ** chespiece -Koordinaten*/ int x, y; / ** Hat diese Koordinate ein Stück*/ boolean hat ein Stück; /** Schachstück Stück = NULL; / ** Schachbrett, zu dem die Koordinate gehört*/ Schachbrett = NULL; öffentlicher Schachpunkt (int x, int y, boolean boo) {this.x = x; this.y = y; Habe ein Stück = boo; } public boolean ispiece () {return Es gibt Schachstücke; } public void Set hat Schachstücke (boolean boo) {Schachsteine = Boo; } public int getX () {return x; } public int gety () {return y; } // Änderungspunkte auf Schachstücke einstellen öffentliches Void -Setpiece (Schachstück, Schachbrett) {this.board = board; this.piece = piece; board.add (piece); int w = (board.unitwidth); int h = (board.unitheight); piece.setBounds (x - w / 2, y - h / 2, w, h); // Schach -Stück -Position, Breite, Höhe mit Schachstücken = wahr; board.Validate (); } public chespiece getPiece () {return piece; } public void Remepiece (Schachstück, Schachbrett) {this.board = board; this.piece = piece; board.remove (piece); board.Validate (); Schachstücke = falsch; }} 5. Gameplay -Regelklassendateiregel.java
Paket cn.edu.ouc.chineSechess; Javax.swing importieren.*; import Java.awt.*; import Java.awt.event.*; / ** * Schachspiel-Regelnklasse * * @Author Cnlht */ Public Class Rule {Schachbrett = NULL; Schachstoffstück = NULL; Schachpunktpunkt [] []; int starti, startj, endi, endj; Öffentliche Regel (Schachbrett, Schachpunktpunkt [] []) {this.board = board; this.point = point; } public void iswine (chesbiece piece) {this.piece = piece; if (piece.getName () == "ausgerichtet" || piece.getName () == "Handy") {if (piece.Color category == "rote Seite") {joptionpane.showMessagedialog (Null, "Black Side Victory!"); } else {joptionpane.showMessagedialog (null, "Red Side Victory!"); }}} public boolean MovePieCerule (Schachstück, 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 kannst du Schach spielen = falsch; if (piece.getName (). Equals ("Car")) {if (starti == endi) {int j = 0; für (j = minj+1; j <= maxj - 1; j ++) {if (point [starti] [j] .ispiece ()) {Kannst du Schach = False bewegen; brechen; }} if (j == maxj) {Kannst du Schach = True bewegen; }} else if (startj == endj) {int i = 0; für (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {Kannst du Schach = false; brechen; }} if (i == maxi) {Kannst du Schach = True bewegen; }} else {Kannst du Schach = False bewegen; }}} else if (piece.getName (). Equals ("") {if (starti == endi) {int j = 0; für (j = minj+1; j <= maxj - 1; j ++) {if (point [starti] [j] .ispiece ()) {Kannst du Schach = False bewegen; brechen; }} if (j == maxj) {Kannst du Schach = True bewegen; }} else if (startj == endj) {int i = 0; für (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {Kannst du Schach = false; brechen; }} if (i == maxi) {Kann ich schach = true; }} else {kann ich schach = false; }} else if (piece.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 ()) {Kann ich Schach = false; } else {kann ich schach = true; }} if (endi <starti) {if (point [starti - 1] [startj] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }}} else if (xaxle == 1 && yaxle == 2) {if (endj> startj) {if (point [starti] [startj + 1] .ispiece ()) {Kannst du Schach = False; } else {Kannst du Schach = True bewegen; }} if (endj <startj) {if (point [starti] [startj - 1] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }} if (endj <startj) {if (point [starti] [startj - 1] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; } WAHR; }}} else {kann ich schach = false; }} else if (piece.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 ()) {Kann ich Schach = false; } else {kann ich schach = true; }} if (endi <starti) {if (point [starti - 1] [startj] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }}} else if (xaxle == 1 && yaxle == 2) {if (endj> startj) {if (point [starti] [startj + 1] .ispiece ()) {Kannst du Schach = False; } else {Kannst du Schach = True bewegen; }} if (endj <startj) {if (point [starti] [startj - 1] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }}} else {Kannst du Schach = False bewegen; }} else if (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) {if (point [centeri] [centerj] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }} else {Kannst du Schach = False bewegen; }} else if (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) {if (point [centeri] [centerj] .ispiece ()) {Kannst du Schach = False bewegen; } else {Kannst du Schach = True bewegen; }} else {Kannst du Schach = true bewegen; }} else {Kannst du Schach = true bewegen; }} else {Kannst du Schach = False bewegen; }} else if (piece.getName (). Equals ("Cannon") {int number = 0; if (starti == endi) {int j = 0; für (j = minj+1; j <= maxj - 1; j ++) {if (point [starti] [j] .ispiece ()) {number ++; }} if (number> 1) {Kannst du Schach = False bewegen; } else if (number == 1) {if (point [endi] [endj] .ispiece ()) {Kannst du Schach = true; }} else if (number == 0 &&! point [endi] [endj] .ispiece ()) {Kannst du Schach = true; }} else if (startj == endj) {int i = 0; für (i = mini+1; i <= maxi - 1; i ++) {if (point [i] [startj] .ispiece ()) {number ++; }} if (number> 1) {Kannst du Schach = False bewegen; } else if (number == 1) {if (point [endi] [endj] .ispiece ()) {Kannst du Schach = true; }} else if (number == 0 &&! point [endi] [endj] .ispiece ()) {Kannst du Schach = true; }} else {Kannst du Schach = False bewegen; }} else if (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) {Kannst du Schach = true; } else {Kannst du Schach = False bewegen; }} else if (endj <= 5) {if ((startj - endj == 1) && (xaxle == 0)) {Kann ich schotte = true; } else if ((endj - startj == 0) && (xaxle == 1)) {Kann ich Schach = true; } else {kann ich schach = false; }}} else if (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) {Kann ich schotte = true; } else {Kannst du Schach = False bewegen; }} else if (endj> = 6) {if ((endj - startj == 1) && (xaxle == 0)) {Kannst du Schach = true; } else if ((endj - startj == 0) && (xaxle == 1)) {Kannst du Schach = true; } else {Kannst du Schach = False bewegen; }}} else if (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) {Kannst du Schach = true; } else {Kannst du Schach = False bewegen; }} else if (piece.getName (). Equals ("Status") {int xaxle = math.abs (starti - endi); int yaxle = math.abs (startj - endj); if (endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1) {Kannst du Schach = true; } else {Kannst du Schach = False bewegen; }} else if ((piece.getName (). Equals ("hübsch") || (piece.getName (). Equals ("ersetzen")) {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)) {Kannst du Schach = true; } else {Kannst du Schach = False bewegen; }} else {Kannst du Schach = False bewegen; }} return können Sie Schach bewegen? }} 6. Datei bewegestep.java
Paket cn.edu.ouc.chineSechess; Import Java.awt.Point; / ** * Walking Class * * @author cnlht * */ public class bewegung implementiert java.io.serializable {public point pstart, pend; public besiepep (Punkt p1, Punkt p2) {pstart = p1; Pend = P2; }} 7. Machen Sie Chessmanual.java
Paket cn.edu.ouc.chineSechess; Javax.swing importieren.*; import Java.awt.*; import Java.awt.event.*; import Java.util.linkedList; / ** * Machen Sie die Schach -Datensatzklasse * * @author cnlht */ public class makechessmanual erweitert JPanel implements actionListener {jtextarea text = null; JScrollPane scroll = null; Schachbrett = NULL; Schachpunkt [] [] Punkt; LinkedList Schach Note = NULL; LinkedList gefressene Schachstücke = null; Jbutton Buttonundo; int i = 0; public makechessmanual (Schachbrett, Schachpunkt [] [] Punkt) {this.board = board; this.point = point; text = new jTextArea (); scroll = new JscrollPane (Text); Schachnote = new LinkedList (); Gefressene Schachstücke = new LinkedList (); buttonundo = new Jbutton ("Buße Schach"); buttonundo.setfont (neue Schriftart ("lishu", font.plain, 18)); setLayout (neuer BorderLayout ()); add (scrollen, borderLayout.Center); add (buttonundo, borderLayout.South); buttonundo.addactionListener (this); } public char numberToletter (int n) {char c = '/0'; Switch (n) {Fall 1: c = 'a'; brechen; Fall 2: C = 'B'; brechen; Fall 3: C = 'C'; brechen; Fall 4: C = 'D'; brechen; Fall 5: c = 'e'; brechen; Fall 6: c = 'f'; brechen; Fall 7: c = 'g'; brechen; Fall 8: C = 'H'; brechen; Fall 9: c = 'i'; brechen; Fall 10: c = 'J'; brechen; } return c; } public void Record Chess Score (Schachstück, int starti, int startj, int endi, int endj) {point pstart = new Point (starti, startj); Point Pend = neuer Punkt (Endi, Endj); WitchEstep step = new bewegungep (pstart, pend); Schach Score.Add (Schritt); String -Schach -Kategorie = piece.chess category (); String name = piece.getName (); String m = "#" + Schachkategorie + Name + ":" + starti + numberToletter (startj) + "to" + endi + numberToletter (endj); text.Append (m); if (piece. Schachkategorie (). Equals (Board.Black Square Color)) Text.Append ("/n"); } public void zeichnen Sie das gegessene Schachstück auf (Objektobjekt) {gegessenes Schachstück.Add (Objekt); } public LinkedList GET DAS CHESS PIEFT () {RETUCT CHESS PIEFT; } public void actionPerformed (actionEvent e) {int Position = text.getText (). lastIndexof ("#"); if (Position! = -1) text.replacerange ("", Position, text.getText (). Länge ()); if (chess piece.size ()> 0) {meverstep laststep = (bewegestep) schachsteuel.getLast (); Schachaufzeichnung.Removelast (); Objekt QIZI = gefressenes Schachstück.GetLast (); Gefressenes Schachstück.Removelast (); String temp = QIZI.ToString (); if (temp.equals ("kein Schachstück") {int starti = laststep.pstart.x; int startj = laststep.pstart.y; int endi = laststep.pend.x; int endj = laststep.pend.y; Schachstück piece = point [endi] [endj] .GetPiece (); point [starti] [startj] .setpiece (piece, board); (Punkt [endi] [endj]). Set hat Schachsteine (falsch); 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 {chespiece remedPiece = (chespiece) QIZI; int starti = laststep.pstart.x; int startj = laststep.pstart.y; int endi = laststep.pend.x; int endj = laststep.pend.y; Schachstück piece = point [endi] [endj] .GetPiece (); point [starti] [startj] .setpiece (piece, board); Punkt [endi] [endj]. (Punkt [endi] [endj]). Set hat Schachsteine (wahr); 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.*; import 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 (neuer 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记录棋谱的方法。
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。