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 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 = 30;
int offset = 50;
static int mangX[][] = new int[o+2][o+2];
static int mangO[][] = new int[o+2][o+2];
static int i = 0;
public static void Khoitao() {
for (int i = 0; i < o+2; i++) {
for (int j = 0; j < o+2; j++) {
Co_ca_ro.mangO[i][j] = 0;
Co_ca_ro.mangX[i][j] = 0;
if(i==0 || i==o+1) {
mangX[i][j]=1;
mangO[i][j]=1;
}
if(j==0||j==o+1) {
mangX[i][j]=1;
mangO[i][j]=1;
}
}
}
}
public Co_ca_ro() {
Co_ca_ro.Khoitao();
this.setSize((o+2) * 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+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);
}
Font f = new Font("Arial", Font.CENTER_BASELINE, 30);
g.setFont(f);
for (int i = 1; i < o+1; i++) {
for (int j = 1; j < o+1; 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 = 1; i < Co_ca_ro.o - 3; i++) {
for (int j = 1; j < Co_ca_ro.o - 3; j++) {
if ((mangO[i][j-1]==0||mangO[i][j+5]==0)&&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 ((mangX[i][j-1]==0||mangX[i][j+5]==0)&&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 ((mangO[i-1][j]==0||mangO[i+5][j]==0)&&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 ((mangX[i-1][j]==0||mangX[i+5][j]==0)&&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 = 5; i < Co_ca_ro.o+1; i++) {
for (int j = 5; j < Co_ca_ro.o+1; j++) {
if ((mangO[i-5][j-5]==0||mangO[i+1][j+1]==0)&&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 ((mangX[i-5][j-5]==0||mangX[i+1][j+1]==0)&&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 = 5; i < Co_ca_ro.o+1; i++) {
for (int j = 1; j < Co_ca_ro.o - 3; j++) {
if ((mangO[i-5][j+5]==0||mangO[i+1][j-1]==0)&&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 ((mangX[i-5][j+5]==0||mangX[i+1][j-1]==0)&&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);
}
}
@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.