Ketika saya hendak belajar pemrograman Javagui, saya menulis permainan kecil Goji.
Saat ini, hanya versi yang berdiri sendiri yang telah diimplementasikan, dan semuanya hanya diproduksi. Jika Anda memiliki pertanyaan, beri saya beberapa saran.
Papan catur dan catur di sini bukan gambar, dan semuanya digambar menggunakan grafik di java.
Kode Sumber telah dikirimkan ke GitHub, klik di sini ke Kode Sumber GitHub
Daftar kategori catur Gozi berikut:
impor java.awt.borderlayout; impor java.awt.color; impor java.awt.container; impor java.awt.graphics; impor java.awt.event.mouseevent; impor java.awt.mouselistener; impor java.util.vector javax.swing.joptionpane; kelas publik limainarow memperluas jframe mengimplementasikan mouseListener {vector v = vektor baru (); // semua informasi catur untuk setiap langkah vektor putih = vektor baru (); // vektor informasi catur putih hitam = vektor baru (); // informasi catur hitam boolean b; // Digunakan untuk menilai apakah bendera putih atau catur hitam di Whitecount, Blackcount; // Hitung jumlah langkah penyesalan catur int w = 25; // Ukuran jarak int px = 100, py = 100; // Ukuran papan int pxw = px + w, pyw = py + w; Int width = w * 16, tinggi = w * 16; int vline = lebar + px; // panjang garis vertikal int hline = tinggi + py; // panjang garis horizontal/***Metode konstruksi*/public fiveinarow () {super ("goji berdiri sendiri"); this.setDefaultCloseOperation (jframe.exit_on_close); // tutup tombol con = this.getContentPane (); con.setlayout (borderlayout baru ()); this.addmouseListener (this); // tambahkan pendengar this.setsize (600, 600); // Atur ukuran bentuk this.setBackground (color.orange); this.setVisible (true); }/*** Gambar papan catur dan potongan -potongan catur* @param e*/public void cat (grafik g) {g.clearrect (0, 0, this.getwidth (), this.getheight ()); // Bersihkan artboard g.setcolor (color.black); // gambar grid warna g.drawrect (p. G.DrawString ("Game Mini Gozi yang berdiri sendiri, klik kanan untuk menyesali catur, selamat datang untuk digunakan", 180, 70); untuk (int i = 0; i <15; i ++) {g.drawline (pxw+i*w, py, pxw+i*w, hline); // Setiap garis horizontal dan vertikal G.Drawline (px, pyw+i*w, vline, pyw+i*w); } untuk (int x = 0; x <v.size (); x ++) {string str = (string) v.get (x); String tmp [] = str.split ("-"); int a = integer.parseint (tmp [0]); int b = integer.parseint (tmp [1]); a = a * w + px; b = b * w + py; if (x%2 == 0) {g.setColor (color.white); } else {g.setColor (color.black); } g.fillarc (aw/2, bw/2, w, w, 0, 360); }} @Override public void mouseclicked (mouseEvent e) {if (e.getButton () == E.button1) {int x = e.getx (); int y = e.gety (); x = (x - x % w) + (x % w> w / 2? w: 0); y = (y - y % w) + (y % w> w / 2? w: 0); x = (x - px) / w; y = (y - py) / w; if (x> = 0 && y> = 0 && x <= 16 && y <= 16) {if (v.contains (x+"-"+y)) {System.out.println ("Sudah ada catur!"); } else {v.add (x+"-"+y); this.repaint (); if (v.size () % 2 == 0) {black.add (x+"-"+y); this.victory (x, y, hitam); // system.out.println ("catur hitam"); } else {white.add (x+"-"+y); this.victory (x, y, white); // system.out.println ("catur putih"); } // System.out.println (e.getx ()+"-"+e.gety ()); }} else {// system.out.println (e.getx ()+"-"+e.gety ()+"|"+x+"-"+y+"/t melebihi batas"); }} if (e.getButton () == e.button3) {// Metode untuk klik kanan catur // system.out.println ("Klik kanan catur-- catur"); if (v.isempty ()) {joptionpane.showmessagealog (ini, "tidak ada catur untuk disesali"); } else {if (v.size () % 2 == 0) {// menilai apakah itu catur putih yang menyesali catur, atau catur hitam yang menyesali catur blackcount ++; if (blackcount> 3) {joptionpane.showmessagedialog (ini, "catur hitam telah menyesali 3 langkah"); } else {v.remove (v.LastElement ()); this.repaint (); }} else {whitecount ++; if (whitecount> 3) {joptionpane.showmessagedialog (ini, "catur hitam telah menyesali 3 langkah"); } else {v.remove (v.LastElement ()); this.repaint (); }}}}}}} / ** * Metode untuk menilai kemenangan * @param x * @param y * @param berisi * / victory void pribadi (int x, int y, vektor berisi) {int cv = 0; // Jumlah potongan catur dalam arah vertikal int ch = 0; // Jumlah potongan catur horizontal int CI1 = 0; // Jumlah potongan catur dalam arah kemiringan 1 int ci2 = 0; // Jumlah potongan catur dalam arah kemiringan 2 // Hitung jumlah potongan catur dalam arah horizontal untuk (int i = 1; i <5; i ++) {if (contain.contains ((x+i)+"-"+y)) {ch ++; } else {break; }} untuk (int i = 1; i <5; i ++) {if (contain.contains ((xi)+"-"+y)) {ch ++; } else {break; }} // Hitung jumlah potongan catur dalam arah vertikal untuk (int i = 1; i <5; i ++) {if (contain.contains (x+"-"+(y+i))) {cv ++; } else {break; }} untuk (int i = 1; i <5; i ++) {if (contain.contains (x+"-"+(yi))) {cv ++; } else {break; }} // Hitung jumlah potongan catur dalam arah miring 45 ° untuk (int i = 1; i <5; i ++) {if (contain.contains ((x+i)+"-"+(y+i))) {ci1 ++; } else {break; }} untuk (int i = 1; i <5; i ++) {if (contain.contains ((xi)+"-"+(yi))) {ci1 ++; } else {break; }} // Hitung jumlah potongan catur dalam arah kemiringan 135 ° untuk (int i = 1; i <5; i ++) {if (contain.contains ((x+i)+"-"+(yi))) {ci2 ++; } else {break; }} untuk (int i = 1; i <5; i ++) {if (contain.contains ((x+i)+"-"+(yi))) {ci2 ++; } else {break; }} untuk (int i = 1; i <5; i ++) {if (contain.contains ((xi)+"-"+(y+i))) {ci2 ++; } else {break; }} if (ch> = 4 || cv> = 4 || ci1> = 4 || ci2> = 4) {System.out.println (v.size ()+"langkah"); if (v.size () % 2 == 0) {// menilai apakah itu hitam atau putih untuk memenangkan joptionpane.showmessagealog (null, "hitam"); } else {joptionpane.showmessageDialog (null, "white"); } this.v.clear (); this.black.clear (); this.white.clear (); this.repaint (); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousepressed (mouseEvent e) {// TODO Metode yang dihasilkan otomatis} @Override public void mousereleased (mouseEvent e) {// todo Metode yang dihasilkan otomatis}}}PS: Saya berencana untuk menulis versi online Goji lainnya, tetapi sayangnya saya tidak tahu banyak tentang internet di Java. Saya harus belajar untuk sementara waktu sebelum terus menyelesaikannya dan menguji hasil pembelajaran pribadi saya.
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.