import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FiveChessFrame extends JFrame implements MouseListener,Runnable {
int width= Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
MyChess my=new MyChess();
int x1,x2,y1,y2;
int turn=2;
int maxTime=9999;
int whiteTime=maxTime,blackTime=maxTime;
String blackMessage,whiteMessage;
Thread t=new Thread(this);
BufferedImage bgImage=null;
public FiveChessFrame(){
this.setTitle("六子棋");
this.setSize(500,500);
this.setLocation((width-500)/2,(height-500)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
this.setVisible(true);
t.start();
}
@Override
public void run() {
if(maxTime>0){
while(true){
if(turn%2==0){
blackTime--;
if(blackTime==0){
JOptionPane.showMessageDialog(this,"黑方超时,游戏结束");
System.exit(0);
}
}else{
whiteTime--;
if(whiteTime==0){
JOptionPane.showMessageDialog(this,"白方超时,游戏结束");
System.exit(0);
}
}
blackMessage=blackTime/3600+":"+(blackTime/60-(blackTime/3600*60))+":"+(blackTime-blackTime/60*60);
whiteMessage=whiteTime/3600+":"+(whiteTime/60-(whiteTime/3600*60))+":"+(whiteTime-whiteTime/60*60);
this.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paint(Graphics g){
BufferedImage bufferedImage=new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
Graphics g2=bufferedImage.createGraphics();
g2.setColor(Color.BLACK);
try {
bgImage= ImageIO.read(new File("E:/nn.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
g2.drawImage(bgImage,0,0,this);
g2.setFont(new Font("黑体",Font.BOLD,20));
g2.drawString("游戏信息",20,50);
g2.drawString("黑方时间"+blackMessage,30,470);
g2.drawString("白方时间"+whiteMessage,260,470);
if(turn%2==0)g2.drawString("黑方轮次",115,50);
else g2.drawString("白方轮次",115,50);
g2.setFont(new Font("黑体",Font.BOLD,15));
g2.drawString("开始游戏",410,95);
g2.drawString("游戏设置",410,144);
g2.drawString("游戏规则",410,188);
g2.drawString("认输",426,290);
g2.drawString("关于",426,336);
g2.drawString("退出",426,386);
g2.drawLine(28,70,388,70);
g2.drawLine(28,430,388,430);
g2.drawLine(28,190,388,190);
g2.drawLine(28,310,388,310);
g2.drawLine(28,70,28,430);
g2.drawLine(148,70,148,430);
g2.drawLine(268,70,268,430);
g2.drawLine(388,70,388,430);
my.paint(g2);
g.drawImage(bufferedImage,0,0,this);
}
public void kill(int x,int y) {
for (int i = 0; i < 4; i++) {
if (my.chessmap[i][0] == 0 && my.chessmap[i][1] != 0 && my.chessmap[i][2] != 0 && my.chessmap[i][3] != 0) {
if ((my.chessmap[i][1] == my.chessmap[i][2]) && (my.chessmap[i][2] != my.chessmap[i][3]) && x == i && (y == 1 || y == 2)) {
my.chessmap[i][3] = 0;
my.chessnumber[i][3] = 9999;
}
if (my.chessmap[i][1] != my.chessmap[i][2] && my.chessmap[i][2] == my.chessmap[i][3] && x == i && (y == 3 || y == 2)) {
my.chessmap[i][1] = 0;
my.chessnumber[i][1] = 9999;
}
}
if (my.chessmap[i][0] != 0 && my.chessmap[i][1] != 0 && my.chessmap[i][2] != 0 && my.chessmap[i][3] == 0) {
if (my.chessmap[i][1] == my.chessmap[i][0] && my.chessmap[i][1] != my.chessmap[i][2] && x == i && (y == 1 || y == 0)) {
my.chessmap[i][2] = 0;
my.chessnumber[i][2] = 9999;
}
if (my.chessmap[i][1] != my.chessmap[i][0] && my.chessmap[i][1] == my.chessmap[i][2] && x == i && (y == 1 || y == 2)) {
my.chessmap[i][0] = 0;
my.chessnumber[i][0] = 9999;
}
}
}
for (int j = 0; j < 4; j++) {
if (my.chessmap[0][j] == 0 && my.chessmap[1][j] != 0 && my.chessmap[2][j] != 0 && my.chessmap[3][j] != 0) {
if (my.chessmap[1][j] == my.chessmap[2][j] && my.chessmap[2][j] != my.chessmap[3][j] && y == j && (x == 1 || x == 2)) {
my.chessmap[3][j] = 0;
my.chessnumber[3][j] = 9999;
}
if (my.chessmap[1][j] != my.chessmap[2][j] && my.chessmap[2][j] == my.chessmap[3][j] && y == j && (x == 3 || x == 2)) {
my.chessmap[1][j] = 0;
my.chessnumber[1][j] = 9999;
}
}
if (my.chessmap[0][j] != 0 && my.chessmap[1][j] != 0 && my.chessmap[2][j] != 0 && my.chessmap[3][j] == 0) {
if (my.chessmap[1][j] == my.chessmap[0][j] && my.chessmap[1][j] != my.chessmap[2][j] && y == j && (x == 1 || x == 0)) {
my.chessmap[2][j] = 0;
my.chessnumber[2][j] = 9999;
}
if (my.chessmap[1][j] != my.chessmap[0][j] && my.chessmap[1][j] == my.chessmap[2][j] && y == j && (x == 1 || x == 2)) {
my.chessmap[0][j] = 0;
my.chessnumber[0][j] = 9999;
}
}
}
}
public void isWin(){
int number1 = 0,number2=0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(my.chessmap[i][j]==1){
number1++;
}
if(my.chessmap[i][j]==2){
number2++;
}
}
}if(number1<2){
JOptionPane.showMessageDialog(this,"黑方胜");my.flag=0;
}
if(number2<2){
JOptionPane.showMessageDialog(this,"白方胜");my.flag=0;
}
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=78&&e.getY()<=106){
my.flag=0;
blackTime=maxTime;
whiteTime=maxTime;
t.resume();
this.repaint();
}
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=126&&e.getY()<=138){
String input=JOptionPane.showInputDialog("请输入游戏最大时间");
try{
maxTime=Integer.parseInt(input)*60;
if(maxTime<0){
JOptionPane.showMessageDialog(this,"请输入正确时间");
}else{
JOptionPane.showMessageDialog(this,"设置完成");
blackTime=maxTime;
whiteTime=maxTime;
blackMessage=blackTime/3600+":"+(blackTime/60-(blackTime/3600*60))+":"+(blackTime-blackTime/60*60);
whiteMessage=whiteTime/3600+":"+(whiteTime/60-(whiteTime/3600*60))+":"+(whiteTime-whiteTime/60*60);
this.repaint();
}
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this,"请输入正确信息");
}
}
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=172&&e.getY()<=197){
JOptionPane.showMessageDialog(this,"棋子只能停留在棋盘上的落子点,\n"+
"棋子只能在线上移动,棋子只能移动一步(即相邻落子点),每回合只能移动1个棋子。\n" +
"消灭对方棋子的方法只有一条,也很简单。那就是:二子打一子。\n" +
"即在棋盘上攻击方的2个棋子(2子必须相连并主动移动其中的1个)与被攻方的1个棋子皆处在一条直线上并相邻时,被攻方的这个棋子就被消灭。\n" +
"双方轮流走子,保护自己的棋子并消灭所有对方的棋子,直到最后胜利。\n");
}
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=271&&e.getY()<=296){
JOptionPane.showMessageDialog(this,"对方胜");
System.exit(0);
}
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=320&&e.getY()<=344){
JOptionPane.showMessageDialog(this,"软件一班\n"+"谈建雯\n"+"2220192466");
}
if(e.getX()>=410&&e.getX()<=474&&e.getY()>=368&&e.getY()<=391){
System.exit(0);
}
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getX());
System.out.println(e.getY());
if((e.getX()-28)%120>=60){
x2=(e.getX()-28)/120+1;
}
if((e.getY()-70)%120>=60){
y2=(e.getY()-70)/120+1;
}
if((e.getX()-28)%120<60){
x2=(e.getX()-28)/120;
}
if((e.getY()-70)%120<60){
y2=(e.getY()-70)/120;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if((e.getX()-28)%120>=60){
x1=(e.getX()-28)/120+1;
}
if((e.getY()-70)%120>=60){
y1=(e.getY()-70)/120+1;
}
if((e.getX()-28)%120<60){
x1=(e.getX()-28)/120;
}
if((e.getY()-70)%120<60){
y1=(e.getY()-70)/120;
}
boolean rightMove;
if((x1-x2<=1&&x1-x2>=-1&&y1-y2<=1&&y1-y2>=-1)&&((x1-x2)!=(y1-y2)&&(x1-x2)!=-(y1-y2))){
rightMove=true;
}else
rightMove=false;
if(((x1!=x2)||(y1!=y2))&&my.chessmap[x2][y2]!=0&&my.chessmap[x1][y1]==0&&rightMove&&(my.chessmap[x2][y2]==turn%2+2||my.chessmap[x2][y2]==turn%2) ){
my.chessmap[x2][y2]=0;
if (my.chessnumber[x2][y2] >= 0 && my.chessnumber[x2][y2] <= 7) {
my.chessmap[x1][y1] = 1;
my.chessnumber[x1][y1] = my.chessnumber[x2][y2];
}
else {
my.chessmap[x1][y1] = 2;
my.chessnumber[x1][y1] = my.chessnumber[x2][y2];
}
blackTime=maxTime;
whiteTime=maxTime;
t.resume();
kill(x1,y1);
this.repaint();
isWin();
this.repaint();
turn++;
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
import java.awt.*;
public class MyChess {
public int flag = 0;
int[][] chessmap=new int[4][4];
int[][] chessnumber=new int[4][4];
public MyChess() { }
public void paint(Graphics g) {
g.setColor(Color.WHITE);
if (flag == 0) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
chessnumber[i][j]=j*4+i;
if ((i >= 1 && i <= 2) && (j >= 1 && j <= 2)) {
chessmap[i][j]=0;
continue;
} else if (j <= 1) {
chessmap[i][j]=1;
g.setColor(Color.WHITE);
g.fillOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
} else {
chessmap[i][j]=2;
g.setColor(Color.BLACK);
g.fillOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
}
}
}
}
else {
for (int i = 0; i < 4; i++) {
for (int j = 0; j <4; j++) {
if(chessmap[i][j]==1){
g.setColor(Color.WHITE);
g.fillOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
}
else if(chessmap[i][j]==2){
g.setColor(Color.BLACK);
g.fillOval(i * 120 + 28 - 15, j * 120 + 70 - 15, 30, 30);
}
}
}
}flag++;
}
}
public class Test {
public static void main(String[] args) {
FiveChessFrame f=new FiveChessFrame();
}
}
