package chess.fivechess;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class FiveChess extends JFrame
implements MouseListener,Runnable,MouseMotionListener{
private static final long serialVersionUID = 1L;
BufferedImage img = null;
int allchess[][] = new int[19][19];
boolean isblack = true;
boolean isContinue = true;
int x = 0;
int y = 0;
String message = "黑方先下";
Thread t = new Thread(this);
int maxTime = 30;
int blackTime = maxTime+3;
int redTime = maxTime;
/**
* @param args
* @throws IOException
*/
public FiveChess() {
this.setTitle("五子棋");
this.setSize(500, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation((width - 500)/2, (height - 500)/2);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.repaint();
t.start();
}
public void paint(Graphics g2) {
BufferedImage bi = (BufferedImage)createImage(500, 500);
Graphics g = bi.getGraphics();
g.setColor(Color.black);
g.setFont(new Font("黑体",Font.BOLD,30));
g.drawString("游戏信息:" + message, 50, 70);
g.setFont(new Font("黑体",Font.PLAIN,20));
g.drawString("黑方时间:" + blackTime, 40, 470);
g.drawString("红方时间:" + redTime, 230, 470);
for (int i=0;i<19;i++) {
g.drawLine(30, 80+ i*20, 390, 80+ i*20);
g.drawLine(30+ i*20, 80, 30+ i*20, 440);
}
g.fillOval(88, 138, 4, 4);
g.fillOval(88, 258, 4, 4);
g.fillOval(88, 378, 4, 4);
g.fillOval(208, 138, 4, 4);
g.fillOval(208, 258, 4, 4);
g.fillOval(208, 378, 4, 4);
g.fillOval(328, 138, 4, 4);
g.fillOval(328, 258, 4, 4);
g.fillOval(328, 378, 4, 4);
for (int i=0;i<19;i++) {
for (int j=0;j<19;j++) {
int temp_x = i*20 +30;
int temp_y = j*20 +80;
if (allchess[i][j] == 1) {
g.setColor(Color.black);
g.fillOval(temp_x-10,
temp_y-10, 20, 20);
}
if (allchess[i][j] == 2) {
g.setColor(Color.red);
g.fillOval(temp_x-10,
temp_y-10, 20, 20);
}
}
}
g.setColor(Color.red);
g.fillOval(400, 100, 80, 50);
g.setColor(Color.white);
g.setFont(new Font("黑体",Font.PLAIN,20));
g.drawString("开 始", 415, 131);
g2.drawImage(bi, 0, 0, this);
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public boolean is_over() {
int color = allchess[x][y];
//横行
int count = 1;
for (int i=1;x + i <=13 && color == allchess[x + i][y];i++) {
count++;
}
for (int i=1; x - i >=0 && color == allchess[x - i][y];i++) {
count++;
}
if (count >= 5) {
return true;
}
//竖行
count = 1;
for (int i=1;y + i <=13 && color == allchess[x][y + i] ;i++) {
count++;
}
for (int i=1;y - i >=0 && color == allchess[x][y - i];i++) {
count++;
}
if (count >= 5) {
return true;
}
//斜行/
count = 1;
for (int i=1;x + i <=13 && y - i >=0 && color == allchess[x + i][y - i];i++) {
count++;
}
for (int i=1;x - i >=0 && y + i <=13 && color == allchess[x - i][y + i];i++) {
count++;
}
if (count >= 5) {
return true;
}
//斜行\
count = 1;
for (int i=1;x + i <=13 && y + i <=13 && color == allchess[x + i][y + i];i++) {
count++;
}
for (int i=1;x - i >=0 && y - i >=0 && color == allchess[x - i][y - i];i++) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
x = arg0.getX();
y = arg0.getY();
if (isContinue) {
if (x >= 25 && x <= 395
&& y >= 75 && y <= 445) {
x = (x-30 + 10)/20;
y = (y-80 + 10)/20;
if (allchess[x][y] != 0) {
JOptionPane.showMessageDialog(this,
"不能在该位置重复下棋");
return;
}
if (isblack) {
allchess[x][y] = 1;
isblack = false;
message = "轮到红方";
} else {
allchess[x][y] = 2;
isblack = true;
message = "轮到黑方";
}
redTime = maxTime;
blackTime = maxTime;
this.repaint();
if (is_over()) {
JOptionPane.showMessageDialog(this,
"游戏结束," + (allchess[x][y] == 1?"黑方":"红方") + "获胜");
isContinue = false;
return;
}
}
}
if (x >= 410 && x <= 490
&& y >= 110 && y <= 160) {
JOptionPane.showMessageDialog(this,
"游戏开始");
for (int i=0;i<19;i++) {
for (int j=0;j<19;j++) {
allchess[i][j] = 0;
}
}
isblack = true;
isContinue = true;
x = 0;
y = 0;
message = "黑方先下";
redTime = maxTime;
blackTime = maxTime;
this.repaint();
}
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FiveChess tj = new FiveChess();
tj.setVisible(true);
}
public void run() {
// TODO Auto-generated method stub
while (true) {
if (isContinue) {
if (isblack) {
blackTime--;
} else {
redTime--;
}
this.repaint();
if (blackTime == 0) {
JOptionPane.showMessageDialog(this,
"游戏结束,红方获胜");
isContinue = false;
}
if (redTime == 0) {
JOptionPane.showMessageDialog(this,
"游戏结束,黑方获胜");
isContinue = false;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void mouseDragged(MouseEvent arg0) {
}
public void mouseMoved(MouseEvent arg0) {
int temp_x = arg0.getX();
int temp_y = arg0.getY();
if (isContinue) {
if (temp_x >= 25 && temp_x <= 395
&& temp_y >= 75 && temp_y <= 445) {
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
} else {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
} else {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
if (temp_x >= 410 && temp_x <= 490
&& temp_y >= 110 && temp_y <= 160) {
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
}