Lat So

package bai_tap; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseListener; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Lat_hinh extends JFrame implements MouseListener { static int dem = 0; static int o =4; static int o_size = 30; static int offset = 50; static int before; static int mang[][] = new int[1000][1000]; static int danhap[] = new int[1000]; static int hienthi[][] = new int[1000][1000]; static int ib=0,jb=0; public static void Khoitao() { before = 0; for(int i=0; i<o; i++) for(int j=0; j<o; j++) hienthi[i][j]=0; for(int i = 1; i<o*o; i++) danhap[i]=0; Random ran = new Random(); int temp; for(int i = 0; i<o; i++){ for (int j = 0; j<o; j++){ while(true){ temp = ran.nextInt(o*o); if(danhap[temp]==1); else{mang[i][j]=temp; danhap[temp]=1; break; } } } } for(int i = 0; i<o; i++) for (int j = 0; j<o; j++){ mang[i][j]++; if(mang[i][j]>o*o/2) mang[i][j]-=o*o/2; System.out.print(" "+mang[i][j]); if (j==o-1) System.out.println(""); } } public Lat_hinh() { Lat_hinh.Khoitao(); this.setSize((o+2) * o_size + offset * 2, o * o_size + offset * 2); this.setTitle("Lật Số"); this.setDefaultCloseOperation(3); this.addMouseListener(this); this.setVisible(true); } @Override public void paint(Graphics g) { // g.setColor(Color.red); // g.fillRect(0, 0, (o+2) * o_size + offset * 2, o * o_size + offset * 2); Font f = new Font("Arial", Font.CENTER_BASELINE, 15); g.setFont(f); if(ib!=-1&&jb!=-1) { g.drawString(""+mang[ib][jb], (jb+1) * o_size + offset + o_size * 1 / 4, (ib+1) * o_size + offset - o_size * 1 / 4); ib=-1;jb=-1; } g.setColor(Color.BLACK); try { Thread.sleep(100); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } g.clearRect(0, 0, (o+2) * o_size + offset * 2, o * o_size + offset * 2); for (int i = 0; i < o+1 ; i++) { g.drawLine(offset + o_size , i * o_size + offset, (o+1) * (o_size) + offset, i * (o_size) + offset); g.drawLine((i+1) * o_size + offset, offset , (i+1) * o_size + offset, (o) * o_size + offset); } for (int i = 0; i < o; i++) { for (int j = 0; j < o; j++) { if(hienthi[i][j]==1) g.drawString(""+mang[i][j], (j+1) * o_size + offset + o_size * 1 / 4, (i+1) * o_size + offset - o_size * 1 / 4); else g.drawString("", (j+1) * o_size + offset + o_size * 1 / 4, (i+1) * o_size + offset - o_size * 1 / 4); if(mang[i][j]==0) { g.setColor(Color.red); g.fillRect((j+1)*o_size + offset, (i)*o_size+offset, o_size, o_size); g.setColor(Color.BLACK); } } } } // public static String checkEnd() { //} public static void main(String[] args) { new Lat_hinh(); } @Override public void mouseClicked(java.awt.event.MouseEvent e) { int x = e.getX(); int y = e.getY(); if (x < offset || y < offset) { return; } int iy = (x - offset) / o_size-1; int ix = (y - offset) / o_size; if(mang[ix][iy]==0) return; else{ hienthi[ix][iy]=1; if(before == 0) { hienthi[ix][iy] = 1; before = mang[ix][iy]; } else { if(mang[ix][iy]==before) { for (int i = 0; i < o; i ++) for (int j = 0; j< o; j ++) if(mang[i][j]==before) mang[i][j]=0; before = 0; for (int i = 0; i < o; i ++) for (int j = 0; j< o; j ++){ System.out.print(" "+mang[i][j]); if (j==o-1) System.out.println("");} ib=-1; jb=-1; dem++; } else { dem++; ib=ix; jb=iy; AllHide(); } }} System.out.println(ix+" "+iy); this.repaint(); if (CheckEnd()){ JOptionPane.showMessageDialog(null, "You WINNNNNNNN \n" + "Số lần mở cặp số:"+dem, "WINNNNNNNN", JOptionPane.INFORMATION_MESSAGE); //new Lat_hinh(); this.setVisible(false); System.exit(0); } } static void AllHide(){ for(int i=0; i<o; i++) for(int j=0; j<o; j++) hienthi[i][j]=0; before = 0; } static boolean CheckEnd(){ int sum =0; for(int i = 0; i<o; i++) for(int j =0; j<o; j++) sum+=mang[i][j]; if (sum==0) return true; return false; } @Override public void mousePressed(java.awt.event.MouseEvent e) { } @Override public void mouseReleased(java.awt.event.MouseEvent e) { } @Override public void mouseEntered(java.awt.event.MouseEvent e) { } @Override public void mouseExited(java.awt.event.MouseEvent e) { } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.