Caro

package bai_tap; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseListener; import java.util.ArrayList; import javafx.scene.control.Alert; import javax.swing.JFrame; import javax.swing.JOptionPane; import org.w3c.dom.events.MouseEvent; public class Co_ca_ro extends JFrame implements MouseListener { static int o = 15; int o_size = 40; int offset = 80; static ArrayList<Point2D> list = new ArrayList<Point2D>(); static int mangX[][] = new int[o][o]; static int mangO[][] = new int[o][o]; static int i = 0; public static void Khoitao() { for (int i = 0; i < o; i++) { for (int j = 0; j < o; j++) { Co_ca_ro.mangO[i][j] = 0; Co_ca_ro.mangX[i][j] = 0; } } } public Co_ca_ro() { Co_ca_ro.Khoitao(); this.setSize(o * o_size + offset * 2, o * o_size + offset * 2); this.setTitle("Cờ ca rô"); this.setDefaultCloseOperation(3); this.addMouseListener(this); this.setVisible(true); } public void paint(Graphics g) { for (int i = 0; i <= o; i++) { g.drawLine(offset, i * o_size + offset, o * o_size + offset, i * o_size + offset); g.drawLine(i * o_size + offset, offset, i * o_size + offset, o * o_size + offset); } Font f = new Font("Arial", Font.CENTER_BASELINE, 30); g.setFont(f); for (int i = 0; i < o; i++) { for (int j = 0; j < o; j++) { if (Co_ca_ro.mangX[i][j] == 1) { g.setColor(Color.red); g.drawString("X", i * o_size + offset + o_size * 1 / 4, j * o_size + offset - o_size * 1 / 4); } if (Co_ca_ro.mangO[i][j] == 1) { g.setColor(Color.BLUE); g.drawString("O", i * o_size + offset + o_size * 1 / 4, j * o_size + offset - o_size * 1 / 4); } } } } public static String checkEnd() { for (int i = 0; i < Co_ca_ro.o - 4; i++) { for (int j = 0; j < Co_ca_ro.o - 4; j++) { if (mangX[i][j] == 1 && mangX[i][j + 1] == 1 && mangX[i][j + 2] == 1 && mangX[i][j + 3] == 1 && mangX[i][j + 4] == 1) { return "X"; } if (mangO[i][j] == 1 && mangO[i][j + 1] == 1 && mangO[i][j + 2] == 1 && mangO[i][j + 3] == 1 && mangO[i][j + 4] == 1) { return "O"; } if (mangX[i][j] == 1 && mangX[i + 1][j] == 1 && mangX[i + 2][j] == 1 && mangX[i + 3][j] == 1 && mangX[i + 4][j] == 1) { return "X"; } if (mangO[i][j] == 1 && mangO[i + 1][j] == 1 && mangO[i + 2][j] == 1 && mangO[i + 3][j] == 1 && mangO[i + 4][j] == 1) { return "O"; } } } for (int i = 4; i < Co_ca_ro.o; i++) { for (int j = 4; j < Co_ca_ro.o; j++) { if (mangX[i][j] == 1 && mangX[i - 1][j - 1] == 1 && mangX[i - 2][j - 2] == 1 && mangX[i - 3][j - 3] == 1 && mangX[i - 4][j - 4] == 1) { return "X"; } if (mangO[i][j] == 1 && mangO[i - 1][j - 1] == 1 && mangO[i - 2][j - 2] == 1 && mangO[i - 3][j - 3] == 1 && mangO[i - 4][j - 4] == 1) { return "O"; } } } for (int i = 4; i < Co_ca_ro.o; i++) { for (int j = 0; j < Co_ca_ro.o - 4; j++) { if (mangX[i][j] == 1 && mangX[i - 1][j + 1] == 1 && mangX[i - 2][j + 2] == 1 && mangX[i - 3][j + 3] == 1 && mangX[i - 4][j + 4] == 1) { return "X"; } if (mangO[i][j] == 1 && mangO[i - 1][j + 1] == 1 && mangO[i - 2][j + 2] == 1 && mangO[i - 3][j + 3] == 1 && mangO[i - 4][j + 4] == 1) { return "O"; } } } return ""; } public static void main(String[] args) { new Co_ca_ro(); } @Override public void mouseClicked(java.awt.event.MouseEvent e) { int x = e.getX(); int y = e.getY(); if (x < offset || y < offset) { return; } int ix = (x - offset) / o_size; int iy = (y - offset) / o_size; if (i % 2 == 0) { if (mangX[ix][iy + 1] == 1 || mangO[ix][iy + 1] == 1) { return; } mangX[ix][iy + 1] = 1; i++; } else { if (mangO[ix][iy + 1] == 1 || mangX[ix][iy + 1] == 1) { return; } mangO[ix][iy + 1] = 1; i++; } this.repaint(); if (!Co_ca_ro.checkEnd().equals("")) { JOptionPane.showMessageDialog(null, Co_ca_ro.checkEnd() + " da win", "WINNNNNNNN", JOptionPane.WARNING_MESSAGE); new Co_ca_ro(); this.setVisible(false); } } public static void infoBox(String infoMessage, String titleBar, String headerMessage) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle(titleBar); alert.setHeaderText(headerMessage); alert.setContentText(infoMessage); alert.showAndWait(); } @Override public void mousePressed(java.awt.event.MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void mouseReleased(java.awt.event.MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void mouseEntered(java.awt.event.MouseEvent e) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void mouseExited(java.awt.event.MouseEvent e) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }

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.