import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
class MainPanel extends JPanel {
private ArrayList items = new ArrayList(100);
private Line2D.Float[] rect = new Line2D.Float[16];
private Line2D.Float[] vert = new Line2D.Float[16];
private JButton reset = new JButton("Reset");
private JButton quit = new JButton("Quit");
private int[][] data = new int[15][15];
private boolean isBlack = true;
private boolean paintEnabled = true;
public MainPanel() {
initPanel();
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(reset);
panel.add(quit);
add(panel, BorderLayout.SOUTH);
addMouseListener(new MouseHandler());
}
private void initPanel() {
int startX = 50;
int startY = 50;
for(int i = 0; i < 16; i++) {
rect[i] = new Line2D.Float(startX, startY + 20*i, startX + 300, startY + 20*i);
vert[i] = new Line2D.Float(startX + 20*i, startY, startX + 20*i, startY + 300);
}
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
data[i][j] = 0;
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isBlack = true;
paintEnabled = true;
items.clear();
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
data[i][j] = 0;
repaint();
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(int i = 0; i < 16; i++) {
g2.draw(rect[i]);
g2.draw(vert[i]);
}
boolean black = true;
for(int i = 0; i < items.size(); i++) {
if(black) {
g.setColor(Color.BLACK);
black = false;
} else {
g.setColor(Color.WHITE);
black = true;
}
g2.fill((Ellipse2D)items.get(i));
}
}
private class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if((x > 50 && x < 350 && y > 50 && y < 350) && paintEnabled) {
int i = (y - 50) / 20;
int j = (x - 50) / 20;
if(data[i][j] == 0) {
if(isBlack) {
data[i][j] = 1;
isBlack = false;
} else {
data[i][j] = -1;
isBlack = true;
}
int cx = j * 20 + 60;
int cy = i * 20 + 60;
Ellipse2D ellipse = new Ellipse2D.Float();
ellipse.setFrameFromCenter(cx, cy, cx + 8, cy + 8);
items.add(ellipse);
repaint();
int result = check(data);
int select = -999;
if(result == 1)
select = JOptionPane.showConfirmDialog(
MainPanel.this,
"Black player wins!\nDo you want to play again?",
"Message Box",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
else if(result == -1)
select = JOptionPane.showConfirmDialog(
MainPanel.this,
"White player wins!\nDo you want to play again?",
"Message Box",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(select == JOptionPane.YES_OPTION) {
isBlack = true;
paintEnabled = true;
items.clear();
for(int p = 0; p < 15; p++)
for(int q = 0; q < 15; q++)
data[p][q] = 0;
repaint();
} else if(select == JOptionPane.NO_OPTION) {
paintEnabled = false;
}
}
}
}
}
public static int check(int[][] a) {
int res = 0;
int num = 0;
for(int i = 0; i < 15; i++) {
res = 0;
num = 0;
if((res = a[i][0]) != 0)
num = 1;
for(int j = 1; j < 15; j++) {
if((a[i][j] == res) && res != 0) {
if(++num == 5) //find one
return res;
} else if(a[i][j] == 0) {
res = 0;
num = 0;
} else {
res = a[i][j];
num = 1;
}
}
}
for(int j = 0; j < 15; j++) {
res = 0;
num = 0;
if((res = a[0][j]) != 0)
num = 1;
for(int i = 1; i < 15; i++) {
if((a[i][j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[i][j] == 0) {
res = 0;
num = 0;
} else {
res = a[i][j];
num = 1;
}
}
}
for(int i = 4; i <= 24; i++) {
res = 0;
num = 0;
if(i <= 14) {
if((res = a[0][i]) != 0)
num = 1;
for(int j = 1; j <= i; j++) {
if((a[j][i-j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][i-j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][i-j];
num = 1;
}
}
} else {
if((res = a[i-14][14]) != 0)
num = 1;
for(int j = i - 13; j <= 14; j++) {
if((a[j][i-j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][i-j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][i-j];
num = 1;
}
}
}
}
for(int i = 4; i <= 24; i++) {
res = 0;
num = 0;
if( i <= 14) {
if((res = a[0][14-i]) != 0)
num = 1;
for(int j = 1; j <= i; j++) {
if((a[j][14-i+j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][14-i+j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][14-i+j];
num = 1;
}
}
} else {
if((res = a[i-14][0]) != 0)
num = 1;
for(int j = i - 13, k = 1; j <= 14; j++, k++) {
if((a[j][k] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][k] == 0) {
res = 0;
num = 0;
} else {
res = a[j][k];
num = 1;
}
}
}
}
return res;
}
}
class MainFrame extends JFrame {
public MainFrame() {
setTitle("Five's Game");
setSize(400, 500);
setResizable(false);
getContentPane().add(new MainPanel());
}
}
public class Five {
public static void main(String[] args) {
JFrame jellen = new MainFrame();
jellen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jellen.show();
}
}
----------------解决方案--------------------------------------------------------
什么?
----------------解决方案--------------------------------------------------------
五子棋哦,不错,有个小问题,这个背景色,和白色棋子一样啊,要改一下好些啊,,呵呵。
----------------解决方案--------------------------------------------------------
看的不是很清楚,颜色调整下好看
----------------解决方案--------------------------------------------------------