写完了的六子棋小游戏

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){//时间变为0,游戏结束
                        JOptionPane.showMessageDialog(this,"黑方超时,游戏结束");
                        System.exit(0);
                    }
                }else{//白子
                    whiteTime--;
                    if(whiteTime==0){
                        JOptionPane.showMessageDialog(this,"白方超时,游戏结束");
                        System.exit(0);
                    }
                }//计算剩余时间以xx:xx:xx格式显示
                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();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值