この記事では、Javaが実装した中国のチェスゲームコードについて説明します。それはあなたの参照のためにあなたと共有されます。特定のコードは次のとおりです
1。実用的な目的:
1。マウスのクリック、ドラッグ、その他のイベント間のアプリケーションと違い
2。チェスレコードファイルの保存と読み取り
3。チェスのルールを改善します。
2。実用的なコンテンツ:
中国のチェスには長い歴史があり、勉強する無数の人々を引き付けました。今、私たちはあなたの参照と学習のために、中国のチェスの戦いとチェススコアの生産について次のデザインと説明をしました。
1.マシンとマシンの間でチェスをするとき、赤い側がリードします。ルールを満たし、リスが落とされた場合、チェスのピースを目的地にドラッグします。
誰もがチェスをします
2。チェスの記録を作ります。チェスレコードメニューを選択した後、ゲームが開始され、チェスプロセスが記録されます。
「チェスレコードの作成」メニューを選択します
チェススコアが終了した後、レッドサイドが勝った
片側が勝った後、ビクトリーメッセージダイアログボックスがポップアップします。 [OK]をクリックした後、[チェスレコードの保存]メニューを選択して、[ファイルの保存]ダイアログボックスを表示します。
チェスレコードダイアログボックスを保存します
3.チェススコアを実証します。デモンストレーションチェススコアメニューを選択した後、開いたダイアログボックスがポップアップします。保存されたチェススコアを選択してデモンストレーションを開始します。
チェスレコードダイアログボックスを示します
チェスの記録プロセスを実証する(自動およびマニュアル)
3。参照コード:
1。チェスメインファイルChinesechess.java
パッケージcn.edu.ouc.chinesechess; javax.swing。*; java.awt。*; java.awt.event。*; java.io.*をインポートします。 java.util.linkedListをインポートします。 / ** *チェスメインクラス * * @author cnlht */ public class chinesechess extends jframe explments actionlistener {chessboardboard = null;悪魔の悪魔= null; MakeChessManual Record = null;コンテナcon = null; Jmenubar Bar; jmenu filemenu; Jmenuitemはチェススコアを作り、チェススコアを保存し、チェススコアを実証します。 jfilechooser filechooser = null; LinkedList Chessスコア= null; public chinesechess(){bar = new jmenubar(); filemenu = new jmenu( "中国のチェス");チェススコアを作成= new JMenuitem( "Make Chess Score");チェススコアを保存= new Jmenuitem( "Chess Score"); Chess score.setEnabledを保存(false);デモンストレーションチェススコア= new Jmenuitem(「デモンストレーションチェススコア」); filemenu.add(チェススコアを作成); filemenu.add(チェススコアを保存); filemenu.add(チェススコアを実証); bar.add(filemenu); setjmenubar(bar); settitle(make chess score.getText());チェススコアを作成します。 Chess Score.AddActionListener(This)を保存します。チェススコアをデモンストレーションします。ボード=新しいチェスボード(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(new WindowAdapter(){public void windowclosing(windowevent e){system.exit(0);}}); setVisible(true);セットバウンド(60、20、690、540); fileChooser = new JFileChooser(); con.validate(); validate(); } public void ActionPerformed(actionEvent e){if(e.getSource()== chess score){con.RemoveAll(); Chess score.setEnabled(true)を保存します。 this.settitle(create chess score.getText());ボード=新しいチェスボード(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); validate(); } if(e.getSource()== save chess score){int state = filechooser.showsavedialog(null);ファイル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 event){}}} if(e.getSource()== chess score){con.removeall(); con.repaint(); con.validate(); validate(); Chess score.setEnabledを保存(false); int state = filechooser.showopendialog(null); file openfile = filechooser.getSelectedFile(); if(openfile!= null && state == jfilechooser.approve_option){try {fileinputStream inone = new fileInputStream(openFile); ObjectInputStream intwo = new ObjectInputStream(inONE); chess =(linkedlist)intwo.readobject(); inone.close(); intwo.close();チェスボードボード=新しいチェスボード(45、45、9、10); Demon = New Demon(Board); Demon.set Chess Record(Chess Record); con.add(demon、borderlayout.center); con.validate(); validate(); this.settitle(demo chess record.getText() + ":" + openFile); } catch(例外イベント){jlabel label = new jlabel( "not chess record file"); label.setfont(new Font( "Lishu"、font.bold、60)); label.setforeground(color.red); label.sethorizontalAlignment(SwingConstants.Center); con.add(label、borderlayout.center); con.validate(); this.settitle( "チェスレコードなし"); validate(); }} else {jlabel label = new jlabel( "Chess Recordファイルは開かれていません"); label.setfont(new Font( "Lishu"、font.bold、50)); label.setforeground(color.pink); label.sethorizontalAlignment(SwingConstants.Center); con.add(label、borderlayout.center); con.validate(); this.settitle( "チェスファイルが開かれていない"); validate(); }}} public static void main(string args []){new Chinesechess(); }} 2。Chessboard.javaファイル
パッケージcn.edu.ouc.chinesechess; javax.swing。*; java.awt。*; java.awt.event。*; / ** *チェスボードクラス * * @author cnlht */ public class chessboard extends jpanel explention muselistener、mousemotionlistener {public chesspoint point [] []; public int unitwidth、unsheight;プライベートINT X軸の長さ、Y軸の長さ。プライベートint x、y;プライベートイメージIMG;保護された画像断片。プライベートブール移動= false;パブリックストリングレッドスクエアカラー= "レッドスクエア"、ブラックスクエアカラー= "ブラックスクエア";チェスピース赤いカート1、赤いカート2、赤い馬1、赤い馬2、赤い馬1、赤い馬2、赤い馬3、レッド兵士4、レッド兵士5、レッドキャノン1、レッドキャノン2。チェスピースブラックカート1、ブラックカート2、ブラックホース1、ブラックホース2、ブラックジェネラル、ブラック兵士1、ブラック兵士2、ブラック兵士3、ブラック兵士4、ブラック兵士5、ブラックエレファント1、ブラックエレファント2、ブラックガン1、ブラックキャノン2; int startx、starty; int starti、startj;パブリックブールレッドサイドチェス=真、ブラックサイドチェス= false;ルールルール= null; Public Makechessmanual Record = null;パブリックチェスボード(int w、int h、int r、int c){setlayout(null); addmouseListener(this); addmouseMotionListener(this); color bc = getBackground(); unitWidth = w; uniteight = h; x軸の長さ= r; y軸の長さ= c; point = new Chesspoint [r + 1] [c + 1]; for(int i = 1; i <= r; i ++){for(int j = 1; j <= c; j ++){point [i] [j] = new chesspoint(i * unitWidth、j * unitheigh、false); }}ルール= new Rule(this、point); Record = new MakeChessManual(This、Point); img = toolkit.getDefaultToolkit()。getImage( "board.jpg"); feedimg = toolkit.getDefaultToolkit()。getImage( "feed.gif"); Red Car 1 = New Chesspiece( ""、Color.Red、BC、W -4、H -4、this);赤い車1.セットチェスピースカテゴリ(赤い四角い色); Red Car 2 = new Chesspiece( ""、color.red、bc、w -4、h -4、this);赤い車2.セットチェスピースカテゴリ(赤い四角い色); Red Horse 1 = new Chesspiece( ""、color.red、bc、w -4、h -4、this); Red Horse 1 = new Chesspiece( ""、color.red、bc、w -4、h -4、this);赤い馬2.チェスピースカテゴリをセット(赤い四角い色); Red Cannon 1 = new Chesspiece( "火"、color.red、bc、w -4、h -4、this);レッドキャノン1.セットチェスピースカテゴリ(赤い四角い色); Red Cannon 2 = new Chesspiece( "火"、color.red、bc、w -4、h -4、this);レッドキャノン2.セットチェスピースカテゴリ(赤い四角い色); Red Cannon 1 = new Chesspiece( "火"、color.red、bc、w -4、h -4、this);レッドキャノン1.セットチェスピースカテゴリ(赤い四角い色); Red Cannon 2 = new Chesspiece( "火"、color.red、bc、w -4、h -4、this);レッドキャノン1.セットチェスピースカテゴリ(赤い四角い色); Red Cannon 2 = new Chesspiece( "火"、color.red、bc、w -4、h -4、this);赤2.チェスピースカテゴリをセット(赤い四角い色); Red 1 = new Chesspiece( "shi"、color.red、bc、w -4、h -4、this);赤1.セットチェスピースカテゴリ(赤い四角い色); Red 2 = new Chesspiece( "shi"、color.red、bc、w -4、h -4、this);赤2.チェスピースカテゴリをセット(赤い四角い色);赤2.チェスピースカテゴリをセット(赤い四角い色); Red 2 = new Chesspiece( "shi"、color.red、bc、w -4、h -4、this); Red 1 = new Chesspiece( "shi"、color.red、bc、w -4、h -4、this);赤い兵士1.チェスピースカテゴリをセット(赤い四角い色); Red Soldier 2 = New Chesspiece( "Soldier"、Color.Red、BC、W -4、H -4、this);レッドソルジャー2.セットチェスピースカテゴリ(赤い四角い色); Red Soldier 3 = New Chesspiece( "Soldier"、Color.Red、BC、W -4、H -4、this); Red Soldier 3 = New Chesspiece( "Soldier"、Color.Red、BC、W -4、H -4、this);レッドソルジャー4.セットチェスピースカテゴリ(赤い四角い色); Red Soldier 5 = new Chesspiece( "Soldier"、Color.Red、BC、W -4、H -4、this);レッドソルジャー5.チェスピースカテゴリをセット(赤い四角い色);ブラックソルジャー= new Chesspiece( "販売"、color.black、bc、w -4、h -4、this);ブラックソルジャー1 = new Chesspiece( "Soldier"、Color.Black、BC、W -4、H -4、this);ブラックソルジャー1 = new Chesspiece( "Soldier"、Color.Black、BC、W -4、H -4、this);ブラックソルジャー1 = new Chesspiece( "car"、color.black、bc、w -4、h -4、this);ブラックカー1.セットチェスピースカテゴリ(黒の正方形の色);ブラックカー2 = new Chesspiece( "car"、color.black、bc、w -4、h -4、this);ブラックカー2.セットチェスピースカテゴリ(黒い四角い色); Black Cannon 1 = New Chesspiece( "Cannon"、Color.Black、BC、W -4、H -4、this);ブラックキャノン1.セットチェスピースカテゴリ(ブラックスクエアカラー); Black Cannon 2 = new Chesspiece( "Cannon"、Color.Black、BC、W -4、H -4、this);ブラックキャノン2.セットチェスピースカテゴリ(ブラックスクエアカラー);ブラックエレファント1 =新しいチェスピース( "象"、color.black、bc、w -4、h -4、this);ブラックエレファント1.セットチェスピースカテゴリ(黒の正方形の色); Black Elephant 2 = new Chesspiece( "Elephant"、Color.Black、BC、W -4、H -4、this);ブラックエレファント2.セットチェスピースカテゴリ(黒の正方形の色);ブラックエレファント1 =新しいチェスピース( "象"、color.black、bc、w -4、h -4、this);ブラックエレファント1.セットチェスピースカテゴリ(黒の正方形の色); Black Elephant 2 = new Chesspiece( "Elephant"、Color.Black、BC、W -4、H -4、this);ブラックホース2.セットチェスピースカテゴリ(黒色);ブラックパンツ1 = new Chesspiece( "Zhi"、Color.Black、BC、W -4、H -4、this);黒いズボン1.チェスピースカテゴリをセット(黒色);ブラックパンツ2 = new Chesspiece( "zhi"、color.black、BC、w -4、h -4、this);ブラックパンツ2.チェスピースカテゴリをセット(黒色);ブラックパンツ3 = new Chesspiece( "zhi"、color.black、BC、w -4、h -4、this);ブラックパンツ3.チェスピースカテゴリをセット(黒色);ブラックパンツ4 = new Chesspiece( "zhi"、color.black、BC、w -4、h -4、this);ブラックファントム4.セットチェスピースカテゴリ(ブラックスクエアカラー); Black Phantom 5 = new Chesspiece( "Zhi"、Color.Black、BC、W -4、H -4、this);ブラックファントム5.セットチェスピースカテゴリ(ブラックスクエアカラー);ポイント[1] [10] .setpiece(赤いキャリッジ1、this);ポイント[2] [10] .setpiece(Red Horse 1、this);ポイント[3] [10] .setpiece(赤フェーズ1、this);ポイント[4] [10] .setpiece(Red Sage 1、this);ポイント[4] [10] .setpiece(Red Sage 1、this);ポイント[4] [10] .setpiece(Red Sage 1、this);ポイント[5] [10] .setpiece(赤い兵士、これ);ポイント[6] [10] .setpiece(Red Soldier 2、this);ポイント[7] [10] .setpiece(Red Soldier 2、this);ポイント[8] [10] .setpiece(Red Horse 2、this);ポイント[9] [10] .setpiece(Red Soldier 2、this);ポイント[2] [8] .setpiece(Red Soldier 1、this);ポイント[8] [8] .setpiece(Red Soldier 2、this);ポイント[1] [7] .setpiece(Red Soldier 2、this);ポイント[3] [7] .setpiece(Red Soldier 2、this);ポイント[3] [7] .setpiece(Red Soldier 2、this);ポイント[3] [7] .setpiece(Red Soldier 2、this);ポイント[5] [7] .setpiece(Red Soldier 3、this);ポイント[7] [7] .setpiece(Red Soldier 4、this);ポイント[9] [7] .setpiece(Red Soldier 5、this);ポイント[1] [1] .setpiece(黒の車1、this);ポイント[2] [1] .setpiece(黒い馬1、これ);ポイント[3] [1] .setpiece(ブラックエレファント1、これ);ポイント[4] [1] .setpiece(ブラックエレファント1、これ);ポイント[5] [1] .setpiece(ブラックジェネラル、これ);ポイント[6] [1] .setpiece(黒の象1、これ);ポイント[4] [1] .setpiece(黒の一般1、this);ポイント[5] [1] .setpiece(ブラックジェネラル2、this);ポイント[6] [1] .setpiece(ブラックジェネラル2、this);ポイント[7] [1] .setpiece(ブラックエレファント2、これ);ポイント[8] [1] .setpiece(ブラックホース2、これ);ポイント[9] [1] .setpiece(ブラックカー2、これ);ポイント[2] [3] .setpiece(ブラックキャノン1、これ);ポイント[8] [3] .setpiece(ブラックキャノン2、これ);ポイント[1] [4] .setpiece(ブラックキャノン1、これ);ポイント[3] [4] .setpiece(ブラックキャノン2、これ);ポイント[5] [4] .setpiece(ブラックキャノン3、これ);ポイント[7] [4] .setpiece(ブラックキャノン4、これ);ポイント[7] [4] .setpiece(ブラックキャノン4、これ);ポイント[3] [4] .setpiece(ブラックキャノン4、これ);ポイント[5] [4] .setpiece(ブラックキャノン3、これ);ポイント[7] [4] .setpiece(ブラックキャノン4、これ);ポイント[9] [4] .setpiece(黒の財布5、これ); } public void paintComponent(Graphics G){super.paintComponent(g); int imgwidth = img.getWidth(this); int imgheight = img.getheight(this); //画像の幅と高さを取得int fwidth = getWidth(); int fheight = getheight(); //ウィンドウの幅と高さを取得x =(fwidth -imgwidth)/ 2; int y =(fheight -imgheight) / 2; G.Drawimage(IMG、X、Y、null); for(int j = 1; j <= y-axis length; j ++){g.drawline(point [1] [j] .x、point [1] [j] .y、point [x-axis length] [j] .x、point [x-axis length] [j] .y); } for(int i = 1; i <= x-axis length; i ++){if(i!= 1 && i!= x-axis length){g.drawline(point [i] [1] [1] .x、point [i] [1] .y、point [i] [y-axis length-5] .x、point [i] [y-axis length-5]; g.drawline(point [i] [y軸の長さ-4] .x、point [i] [y軸の長さ-4] .y、point [i] [y軸長] .x、point [i] [y-xis length] .y); } else {g.drawline(point [i] [1] .x、point [i] [1] .y、point [i] [y axis length] .x、point [i] [y-axis length] .y); }} g.drawline(point [4] [1] .x、point [4] [1] .y、point [6] [3] .x、point [6] [3] .y); g.drawline(point [6] [1] .x、point [6] [1] .y、point [4] [3] .x、point [6] [1] .y、point [4] [3] .x、point [4] [3] .y); g.drawline(point [4] [8] .x、point [4] [8] .y、point [6] [y-axis length] .x、point [6] [y-axis length] .y); g.drawline(point [4] [y軸の長さ] .x、point [4] [y軸長] .y、ポイント[6] [8] .x、point [6] [8] .y); for(int i = 1; i <= x-axis length; i ++){g.drawString( "+i、i * unitWidth、unitheight / 2); } int j = 1; for(char c = 'a'; c <= 'j'; c ++){g.drawstring( "+c、unitWidth / 4、j * unitheight); J ++; }} / **マウスプレスイベント* / public void mousepressed(mousevent e){chesspiece feice = null;長方形rect = null; if(e.getSource()== this)move = false; if(move == false)if(e.getSource()instanceof chesspiece){feed =(chesspiece)e.getSource(); startx = feed.getBounds()。x; starty = feice.getBounds()。y; rect = feice.getBounds(); for(int i = 1; i <= x-axis length; i ++){for(int j = 1; j <= y-axis length; j ++){int x = point [i] [j] .getx(); int y = point [i] [j] .gety(); if(rect.contains(x、y)){starti = i; startj = j;壊す; }}}}}}} public void mousemoved(mousevent e){} / **マウスドラッグイベント* / public void mousedRagged(mouseEvent e){chesspiece feice = null; if(e.getSource()instance of chesspiece){feed =(chesspiece)e.getSource(); move = true; e = swhingutilities.convertMouseEvent(fee、e、this); } if(e.getSource()== this){if(move && feice!= null){x = e.getx(); y = e.gety(); if(red square chess &&((feed.chess subcategory())。equals(red square color))){feed.setlocation(x -ceep.getWidth() / 2、y -feace.getheigh() / 2); } if(black chess move &&(feace.chess class()。equals(black font color))){feed.setlocation(x -ceep.getWidth() / 2、y -feed.getheigh() / 2); }}}}} / **マウスイベントをリリース* / public void mousereleased(mousevent e){chesspiece feice = null; move = false;長方形rect = null; if(e.getSource()instanceof chesspiece){feed =(chesspiece)e.getSource(); rect = feice.getBounds(); e = swhingutilities.convertMouseEvent(fee、e、this); } if(e.getSource()== this){boolean containschesspoint = false; int x = 0、y = 0; int m = 0、n = 0; if(feed!= null){for(int i = 1; i <= x-axis length; i ++){for(int j = 1; j <= y-axis length; j ++){x = point [i] [j] .getx(); y = point [i] [j] .gety(); if(rect.contains(x、y)){containschesspoint = true; m = i; n = j;壊す; }}}}} if(feice!= null && containschesspoint){color pececolor = feed.get color.get color(); if(point [m] [n] .ispiece()){color c =(point [m] [n] .getpiece())。 if(peacecolor.getrgb()== c.getrgb()){feed.setlocation(startx、starty); (point [starti] [startj])。セットにはチェスピースがあります(true); } else {boolean ok = rule.movepiecerule(feice、starti、startj、m、n); if(ok){chesspiece piceRemoved = point [m] [n] .getpiece();ポイント[m] [n] .removepiece(peceremoved、this);ポイント[m] [n] .setpiece(断片、this); (point [starti] [startj])。セットにはチェスピースがあります(false); Record.Record Chessスコア(ピース、starti、startj、m、n); Record.Eat Chessの断片を録音します(断片化); Rule.iswine(peceRemoved); if(ecee.paper category()。equals(red square color)){red square move = false;ブラックスクエアの動き= true; } if(feace.paper category()。equals(black square color)){black square move = false;レッドスクエアの動き= true; } validate(); Repaint(); } else {feice.setlocation(startx、starty); (point [starti] [startj])。セットにはチェスピースがあります(true); }}} else {boolean ok = rule .movepiecerule(feice、starti、startj、m、n); if(ok){point [m] [n] .setpiece(feice、this); (point [starti] [startj])。セットにはチェスピースがあります(false); Record.Record Chessスコア(ピース、starti、startj、m、n); Record.Record Chess Pieces eat( "Chess Pieces no"); if(eace.paper category()。equals(red square color)){red square chess = false;ブラックスクエアチェス= true; } if(feed.paper category()。equals(black square color)){black square chess = false;レッドスクエアチェス= true; }} else {feice.setLocation(startx、starty); (point [starti] [startj])。セットにはチェスピースがあります(true); }}} if(feed!= null &&!contatrchesspoint){feed.setlocation(startx、starty); (point [starti] [startj])。セットにはチェスピースがあります(true); }}} public void mouseentered(mousevent e){} public void mouseExited(mousevent e){} public void mouseclicked(mousevent e){}}}} 3。Chesspiece.javaファイル
パッケージcn.edu.ouc.chinesechess; javax.swing。*; java.awt。*; java.awt.event。*; / ** *チェスクラス * * @author cnlht */ public class chesspiece extends jlabel {string name; //チェス名の色backcolor = null、forecolor; //背景と前景の色文字列色カテゴリ= null;チェスボードボード= null; int width、height; // size public chesspiece(string name、color fc、color bc、int width、int height、chessboard board){//チェスピースthis.name = name; this.board = board; this.width = width; this.height = height; ForeColor = fc; backcolor = bc; SetSize(幅、高さ); Setbackground(BC); addmouseMotionListener(ボード); addmouseListener(ボード); } //チェスピースのパブリックボイドペイント(グラフィックスG){g.drawimage(board.pieceimg、2、2、width-2、height-2、null); G.SetColor(ForeColor); g.setFont(new Font( "Kaiti"、font.bold、26)); G.DrawString(名前、7、高さ-8); //チェスのピースに「チェス名」を描画しますg.setcolor(color.black); //g.drawoval(1、1、width -1、height -1); Float linewidth = 2.3f; ((Graphics2d)g).SetStroke(新しいBasicStroke(LineWidth)); ((Graphics2d)g).drawoval(2、2、width-2、height-2); } public int getWidth(){return width; } public int getheight(){return height; } public string getname(){return name; } public color chess color(){return forecolor; } public void set chess category(string category){color category = category; } public string chess category(){return color category; }} 4。チェスピースポイント座標ファイル
パッケージcn.edu.ouc.chinesechess; / ***チェスポイントクラス** @author cnlht*/ public class chesspoint {/ ** chesspiece座標*/ int x、y; / **この座標にはピースがあります*/ booleanにはピースがあります。 /**チェスピースピース= null; / **座標が属するチェスボード*/チェスボードボード= null; Public Chesspoint(int x、int y、boolean boo){this.x = x; this.y = y;ピースを持っている= boo; } public boolean ispiece(){チェスの断片があります。 } public void setにはチェスピースがあります(ブールブー){chess peaces = boo; } public int getX(){return x; } public int gety(){return y; } // Chess Piecesに変更点を設定しますpublic void setpiece(Chesspiece Piece、Chessboard Board){this.board = board; this.piece = feice; board.add(断片); int w =(board.unitwidth); int h =(board.unitheight); feice.setbounds(x -w / 2、y -h / 2、w、h); //チェスピースの位置、幅、チェスピース付きの高さ= true; board.validate(); } public chesspiece getpiece(){return peice; } public void removepiece(チェスピースピース、チェスボードボード){this.board = board; this.piece = feice; board.remove(断片); board.validate();チェスピース= false; }} 5。ゲームプレイルールクラスファイルルール.java
パッケージcn.edu.ouc.chinesechess; javax.swing。*; java.awt。*; java.awt.event。*; / ** *チェスプレイルールクラス * * @author cnlht */ public classルール{chessboard board = null;チェスピースピース= null;チェスポイントポイント[] []; int starti、startj、endi、endj;パブリックルール(チェスボードボード、チェスポイントポイント[] []){this.board = board; this.point = point; } public void iswine(chesspiece piece){this.piece = feice; if(feace.getName()== "aligned" || feice.getName()== "handy"){if(feed.color category == "red Side"){joptionpane.showmessageialog(null、 "black side victory!"); } else {joptionpane.showmessageialog(null、 "Red Side Victory!"); }}} public boolean movepiecerule(chesspiece feice、int starti、int startj、int endi、int endj){this.piece = feice; 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はチェス= falseをプレイできますか。 if(feace.getName()。equals( "car")){if(starti == endi){int j = 0; for(j = minj+1; j <= maxj -1; j ++){if(point [starti] [j] .ispiece()){chess = falseを動かすことができます。壊す; }} if(j == maxj){chess = trueを動かすことができます。 }} else if(startj == endj){int i = 0; for(i = mini+1; i <= maxi -1; i ++){if(point [i] [startj] .ispiece()){chess = falseを動かすことができます。壊す; }} if(i == maxi){chess = trueを動かすことができます。 }} else {chess = falseを動かすことができます。 }}} else if(feed.getname()。equals( "")){if(starti == endi){int j = 0; for(j = minj+1; j <= maxj -1; j ++){if(point [starti] [j] .ispiece()){chess = falseを動かすことができます。壊す; }} if(j == maxj){chess = trueを動かすことができます。 }} else if(startj == endj){int i = 0; for(i = mini+1; i <= maxi -1; i ++){if(point [i] [startj] .ispiece()){chess = falseを動かすことができます。壊す; }} if(i == maxi){chess = trueを動かすことができます。 }} else {chess = falseを移動できますか; }} else if(feed.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()){chess = falseを動かすことができます; } else {chess = trueを動かすことはできますか。 }} if(endi <starti){if(point [starti -1] [startj] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }}} else if(xaxle == 1 && yaxle == 2){if(endj> startj){if(point [starti] [startj + 1] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }} if(endj <startj){if(point [starti] [startj -1] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }} if(endj <startj){if(point [starti] [startj -1] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 } 真実; }}} else {chess = falseを移動できますか; }} else if(feed.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()){chess = falseを動かすことができます; } else {chess = trueを動かすことはできますか。 }} if(endi <starti){if(point [starti -1] [startj] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }}} else if(xaxle == 1 && yaxle == 2){if(endj> startj){if(point [starti] [startj + 1] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }} if(endj <startj){if(point [starti] [startj -1] .ispiece()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }}} else {chess = falseを動かすことができますか; }} else if(feed.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()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }} else {chess = falseを動かすことができます。 }} else if(feed.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()){chess = falseを動かすことができます。 } else {chess = trueを動かすことができます。 }} else {chess = trueを動かすことができます。 }} else {chess = trueを動かすことができます。 }} else {chess = falseを動かすことができます。 }} else if(feed.getname()。equals( "cannon")){int number = 0; if(starti == endi){int j = 0; for(j = minj+1; j <= maxj -1; j ++){if(point [starti] [j] .ispiece()){number ++; }} if(number> 1){chess = falseを動かすことができます。 } else if(number == 1){if(point [endi] [endj] .ispiece()){chess = trueを動かすことができます。 }} else if(number == 0 &&!point [endi] [endj] .ispiece()){chess = trueを動かすことができます。 }} else if(startj == endj){int i = 0; for(i = mini+1; i <= maxi -1; i ++){if(point [i] [startj] .ispiece()){number ++; }} if(number> 1){chess = falseを動かすことができます。 } else if(number == 1){if(point [endi] [endj] .ispiece()){chess = trueを動かすことができます。 }} else if(number == 0 &&!point [endi] [endj] .ispiece()){chess = trueを動かすことができます。 }} else {chess = falseを動かすことができます。 }} else if(feed.getname()。equals( "soldier")){int xaxle = math.abs(starti -endi); int yaxle = math.abs(startj -endj); if(endj> = 6){if(startj -endj == 1 && xaxle == 0){chess = trueを動かすことができます。 } else {chess = falseを動かすことができます。 }} else if(endj <= 5){if((startj -endj == 1)&&(xaxle == 0)){chess = trueを移動できますか; } else if((endj -startj == 0)&&(xaxle == 1)){chess = trueを動かすことができます。 } else {chess = falseを動かすことはできますか。 }}} else if(feed.getname()。equals( "zhi")){int xaxle = math.abs(starti -endi); int yaxle = math.abs(startj -endj); if(endj <= 5){if(endj -startj == 1 && xaxle == 0){chess = trueを移動できますか; } else {chess = falseを動かすことができます。 }} else if(endj> = 6){if((endj -startj == 1)&&(xaxle == 0)){chess = trueを動かすことができます。 } else if((endj -startj == 0)&&(xaxle == 1)){chess = trueを動かすことができます。 } else {chess = falseを動かすことができます。 }}} else if(feed.getname()。equals( "shi")){int xaxle = math.abs(starti -endi); int yaxle = math.abs(startj -endj); if(endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1){chess = trueを動かすことができます。 } else {chess = falseを動かすことができます。 }} else if(feed.getname()。equals( "state")){int xaxle = math.abs(starti -endi); int yaxle = math.abs(startj -endj); if(endi <= 6 && endi> = 4 && xaxle == 1 && yaxle == 1){chess = trueを動かすことができます。 } else {chess = falseを動かすことができます。 }} else if((feed.getName()。equals( "handome"))||(feed.getname()。equals( "falling"))){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)){chess = trueを動かすことができます。 } else {chess = falseを動かすことができます。 }} else {chess = falseを動かすことができます。 }}返品チェスを動かすことができます。 }} 6。MOVESTEP.JAVAファイル
パッケージcn.edu.ouc.chinesechess; java.awt.pointをインポートします。 / ** *ウォーキングクラス * * @author cnlht * */ public class movestepを実装java.io.serializable {public point pstart、pend; public Movestep(Point P1、Point P2){pstart = p1;ペンド= p2; }} 7. Chessmanual.javaを作成します
パッケージcn.edu.ouc.chinesechess; javax.swing。*; java.awt。*; java.awt.event。*; java.util.linkedListをインポートします。 / ** *チェスレコードクラスを作成 * * @author cnlht */ public class makechessmanual拡張jpanel実装actionlistener {jtextarea text = null; jscrollpane scroll = null;チェスボードボード= null;チェスポイント[] []ポイント; LinkedList Chess note = null; linkedlist eated chess fiece = null; jbutton buttonundo; int i = 0; Public MakechessManual(チェスボードボード、チェスポイント[] []ポイント){this.board = board; this.point = point; text = new jtextarea(); scroll = new jscrollpane(テキスト); Chess note = new LinkedList();食べたチェスの断片= new linkedlist(); buttonnundo = new jbutton( "Repent Chess"); buttonnundo.setfont(new font( "lishu"、font.plain、18)); setLayout(new borderlayout()); add(scroll、borderlayout.center); add(buttonnundo、borderlayout.south); buttonnundo.addactionlistener(this); } public char numberToletter(int n){char c = '/0'; switch(n){case 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; } public void Record Chess Score(Chesspiece Peice、int starti、int startj、int endi、int endj){point pstart = new point(starti、startj);ポイントペンド= new Point(endi、endj); movestep step = new Movestep(pstart、pend); Chess Score.Add(step); String Chess category = feice.chess category();文字列name = feed.getName(); string m = "#" + chess category + name + ":" + starti + numbertoletter(startj) + "to" + endi + numbertoletter(endj); text.append(m); if(feed。Chesscategory()。equals(board.black color))text.append( "/n"); } public void Record the seat chess fiee(object object){eate chess feice.add(object); } public linkedlist get chess feice(){return chess feice; } public void ActionPerformed(actionEvent e){int position = text.getText()。lastIndexof( "#"); if(position!= -1)text.replacerange( ""、position、text.getText()。length()); if(chess feice.size()> 0){movestep lastStep =(movestep)chess feices.getLast(); Chess Record.Removelast(); Object Qizi = eated Chess feices.getLast(); eated chess fiece.removelast(); string temp = qizi.toString(); if(temp.equals( "no chess fiece")){int starti = lastStep.pstart.x; int startj = lastStep.pstart.y; int endi = lastStep.pend.x; int endj = lastStep.pend.y; chesspiece feice = point [endi] [endj] .getpiece(); point [starti] [startj] .setpiece(断片、ボード); (point [endi] [endj])。セットにはチェスピースがあります(false); if(feace.chess cees category()。equals(board.red square color)){board.red square move = true; board.black square move = false; } if(feed.chess cees category()。equals(board.black color)){board.black square move = true; Board.Red Square Move = false; }} else {chesspiece removevedpiece =(chesspiece)qizi; int starti = lastStep.pstart.x; int startj = lastStep.pstart.y; int endi = lastStep.pend.x; int endj = lastStep.pend.y; chesspiece feice = point [endi] [endj] .getpiece(); point [starti] [startj] .setpiece(断片、ボード); point [endi] [endj] .setpiece(removedpiece、board); (point[endI][endJ]).set has chess pieces(true); if (piece.Paper category().equals(board.Red square color)) { board.Red square move = true; board.Black square move = false; } if (piece.Paper Category().equals(board.Black Square Color)) { board.Black Square Chess = true; board.Red Square Chess = false; } } } } } } 8.演示棋谱类文件Demon.java
package cn.edu.ouc.chineseChess; import javax.swing.*; import java.awt.*; import java.awt.event.*; Java.util。*をインポートします。 /** * Demo chess class* * @author cnlht */ public class Demon extends JPanel implements ActionListener, Runnable { public JButton replay = null, next = null, auto = null, stop = null; LinkedList chess = null; Thread Automatic demonstration = null; int index = -1; ChessBoard board = null; JTextArea text; JTextField Time interval= null; int time = 1000; String Demo process = ""; JSplitPane splitH = null, splitV = null; public Demon(ChessBoard board) { this.board = board; replay = new JButton("Redemo"); next = new JButton("Next"); auto = new JButton("Automatic Demo"); stop = new JButton("Pause Demo"); Automatic Demo= new Thread(this); replay.addActionListener(this); next.addActionListener(this); auto.addActionListener(this); stop.addActionListener(this); text = new JTextArea(); Time interval = new JTextField("1"); setLayout(new borderlayout()); JScrollPane pane = new JScrollPane(text); JPanel p = new JPanel(new GridLayout(3, 2)); p.add(next); p.add(replay); p.add(auto); p.add(stop); p.add(new JLabel("Time interval (seconds), SwingConstants.CENTER)); p.add(time interval); splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, p); splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, board, splitV); splitV.setDividerSize(5); splitV.setDividerLocation(400); splitH.setDividerSize(5); splitH.setDividerLocation(460); add(splitH, BorderLayout.CENTER); validate(); } public void set chess score(LinkedList chess score) { this. chess score= chess score; } public char numberToLetter(int n) { char c = '/0'; switch (n) { case 1: c = 'A'; break; case 2: c = 'B'; break; case 3: c = 'C'; break; case 4: c = 'D'; break; case 5: c = 'E'; break; case 6: c = 'F'; break; case 7: c = 'G'; break; case 8: c = 'H'; break; case 9: c = 'I'; break; case 10: c = 'J'; break; } return c; } public void actionPerformed(ActionEvent e) { if (e.getSource() == next) { index++; if (index < chess score.size()) { demonstration step(index); } else { demonstration end ("chess score demonstration completed"); } } if (e.getSource() == replay) { board = new ChessBoard(45, 45, 9, 10); splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); splitH.setLeftComponent(board); splitH.validate(); index = -1; text.setText(null); } if (e.getSource() == auto) { next.setEnabled(false); replay.setEnabled(false); try { time = 1000 * Integer.parseInt(Time interval.getText().trim()); } catch (NumberFormatException ee) { time = 1000; } if (!(automatic demonstration.isAlive())) {automatic demonstration = new Thread(this); board = new ChessBoard(45, 45, 9, 10); splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); splitH.setLeftComponent(board); splitH.validate(); text.setText(null); automatic demonstration.start(); } } if (e.getSource() == stop) { if (e.getActionCommand().equals("Pause Demo")) { Demo process = "Pause Demo"; stop.setText("Continue Demo"); stop.repaint(); } if (e.getActionCommand().equals("Continue Demo")) { Demo process = "Continue Demo"; Automatic demonstration.interrupt(); stop.setText("Pause Demo"); stop.repaint(); } } } public synchronized void run() { for (index = 0; index < chess score.size(); index++) { try { Thread.sleep(time); } catch (InterruptedException e) { } while (Demo process.equals("Pause demo")) { try { wait(); } catch (InterruptedException e) { notifyAll(); } } Demo step(index); } if (index >= Chess Note.size()) { Demo end("Chess Note demonstration completed"); next.setEnabled(true); replay.setEnabled(true); } } public void Demo step(int index) { MoveStep step = (MoveStep) Chess Note.get(index); Point pStart = step.pStart; Point pEnd = step.pEnd; int startI = pStart.x; int startJ = pStart.y; int endI = pEnd.x; int endJ = pEnd.y; ChessPiece piece = (board.point)[startI][startJ].getPiece(); if ((board.point)[endI][endJ].isPiece() == true) { ChessPiece pieceRemoved = (board.point)[endI][endJ].getPiece(); (board.point)[endI][endJ].reMovePiece(pieceRemoved, board); board.repaint(); (board.point)[endI][endJ].setPiece(piece, board); (board.point)[startI][startJ].set has chess pieces (false); board.repaint(); } else { (board.point)[endI][endJ].setPiece(piece, board); (board.point)[startI][startJ].set has chess pieces (false); } String chess piece category = piece.chess piece category(); String name = piece.getName(); String m = "#" + chess class + name + ": " + startI + numberToLetter(startJ) + " to " + endI + numberToLetter(endJ); text.append(m); if (piece. chess class().equals(board.black square color)) text.append("/n"); } public void demonstration end(String message) { splitH.remove(board); splitH.setDividerSize(5); splitH.setDividerLocation(460); JLabel label = new JLabel(message); label.setFont(new Font("Lishu", Font.BOLD, 40)); label.setForeground(Color.blue); label.setHorizontalAlignment(SwingConstants.CENTER); splitH.setLeftComponent(label); splitH.validate(); } }四、总结与要求
1.理解8个文件,没有太复杂的代码。
2.理解鼠标的MouseListener,MouseMotionListener两个接口的区别,五子棋的实现不需要MouseMotionListener。
3.使用LinkedList记录棋谱的方法。
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。