用Java写的一个五子棋

先看效果

目前实现了基本的落子,重复落子判定,越界判定等功能,一下是一部分关键代码

public interface v {
    int x=50,y=50,len=750,jianJu = 50;
    int[][] list = new int[15][15];

    //把一些基础的常量定义到接口中
}

棋盘的绘制(继承JFrame)

import java.awt.*;
import javax.swing.*;
public class ChessFrame extends JFrame implements v{

    public void paint(Graphics g){
        //背景板的重绘
        g.setColor(new Color(182, 132, 0));
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(new Color(0, 0, 0));

        //按钮重绘
        Component[] components = getComponents();
        Component[] components1 = getRootPane().getComponents();
        JLayeredPane jPanel = (JLayeredPane) components1[1];
        Component[] components2 = jPanel.getComponents();
        JPanel panle = (JPanel) components2[0];
        Component[] components3 = panle.getComponents();
        for (int i = 0; i < components3.length; i++) {
            components3[i].repaint();
        }

        //棋盘重绘
        for (int i = 0; i <= 15; i++) {
            g.setColor(new Color(0, 0, 0));
            //竖线
            g.drawLine(x+i*jianJu+jianJu/2,y+jianJu/2,x+i*jianJu+jianJu/2,y+len+jianJu/2);
            //横线
            g.drawLine(x+jianJu/2,y+i*jianJu+jianJu/2,x+len+jianJu/2,y+i*jianJu+jianJu/2);
            for (int j = 0; j < list.length; j++) {
                for (int k = 0; k < list[j].length; k++) {
                    if (list[j][k] == 1){
                        //return;
                        g.setColor(Color.black);
                        g.fillOval(k*jianJu,j*jianJu,jianJu,jianJu);
                    } else if (list[j][k] == 2) {
                        g.setColor(Color.white);
                        g.fillOval(k*jianJu,j*jianJu,jianJu,jianJu);
                    }
                }
            }
        }


    }

}

棋子对象的定义

public class ChessPiece implements v{
    int h;
    int l;
    int chessFlag;
    int index;
    public void drawChess(Graphics g){
        //颜色轮换
        if (chessFlag==1){
            g.setColor(Color.black);

        } else if (chessFlag==2) {
            g.setColor(Color.white);

        }
        //绘制棋子
        g.fillOval(l*jianJu,h*jianJu,jianJu,jianJu);
        g.setColor(Color.cyan);
        g.drawString(index+"",l*jianJu,h*jianJu);
    }

}

关键的逻辑判断

import javax.swing.*;
import java.awt.*;

public class ChessSever implements v{
    private int chessFlag =1;
    //int[][] list = new int[15][15];
    ChessPiece[] chessList = new ChessPiece[255];
    int chessIndex ;
    public boolean checkPlay(int x,int y){
        int h= y /jianJu;
        int l = x /jianJu;
        if (h>16||l>16||h<=0||l<=0){
            System.out.println(x+","+y);
            JOptionPane.showMessageDialog(null,"请勿越界");
            return false;
        } else if (list[h][l]!=0){
            System.out.println(list[h][l]);
            JOptionPane.showMessageDialog(null,"请勿重复落子");
            return false;
        }



        if (chessFlag==0){
            JOptionPane.showMessageDialog(null,"游戏未开始,请开始游戏");
            return false;
        }
        return true;
    }

    public ChessPiece saveChess(int h,int l){
        list[h][l] = chessFlag;

        ChessPiece chess = new ChessPiece();
        chess.h=h;
        chess.l=l;
        chess.chessFlag=chessFlag;

        chess.index=chessIndex;
        chessList[chessIndex] = chess;
        chessIndex++;
        return chess;
    }

    public void playChess(int x,int y,Graphics g){
        int h=y/jianJu;
        int l = x/jianJu;
        if (checkPlay(x,y)){
            ChessPiece chess =saveChess(h,l);
            chess.drawChess(g);
            chessFlag = (chessFlag==1) ? 2 : 1;
        }



    }
}

输赢判定(正在整合中)

public class IsWin {
    public static boolean IsWin(int[][] arr,int h,int l){
        if (hang(arr, h,l) >= 5 || lie(arr, h,l) >= 5 || xieShang(arr, h,l)>=5||xieXia(arr, h,l)>=5) {
            System.out.println("win");
            return true;

        }else {
            System.out.println("defeated");
            return false;
        }
    }
    //横纵判断
    private static int lie(int[][] arr, int h, int l){
        int count = 1;
        for (int i = h+1; i <arr.length ; i++) {
            if (arr[h][l]==arr[i][l]){
                count++;
            }else {
                break;
            }
        }
        for (int j = h-1; j >0 ; j--) {
            if (arr[h][l]==arr[j][l]){
                count++;
            }else {

                break;
            }
        }
        return count;
    }
    
    private static int hang(int[][] arr, int h, int l){
        int count = 1;
        for (int i = l+1; i <arr[0].length ; i++) {
            if (arr[h][l]==arr[h][i]){
                count++;
            }else {
                break;
            }
        }
        for (int j = l-1; j >0 ; j--) {
            if (arr[h][l]==arr[h][j]){
                count++;
            }else {
                break;
            }
        }
        return count;

    }
    //斜方向的判定
    private static int xieShang(int[][] arr, int h, int l){
        int count = 1;
        for (int i = h+1,j=l+1; i <arr.length&j<arr[0].length ; i++,j++) {
            if (arr[h][l]==arr[i][j]){
                count++;
            }else {
                break;
            }
        }
        for (int i = h-1,j=l-1; i >0&j>0 ; i--,j--) {
            if (arr[h][l]==arr[i][j]){
                count++;
            }else {
                break;
            }
        }
        return count;
    }
    private static int xieXia(int[][] arr, int h, int l){
        int count = 1;
        for (int i = h+1,j=l-1; i <arr.length&j>0 ; i++,j--) {
            if (arr[h][l]==arr[i][j]){
                count++;
            }else {
                break;
            }
        }
        for (int i = h-1,j=l+1; i >0&j<arr[0].length ; i--,j++) {
            if (arr[h][l]==arr[i][j]){
                count++;
            }else {
                break;
            }
        }

        return count;
    }
}

监听器部分

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Listener implements v,MouseListener {
    Graphics g;
    ChessSever chessSever = new ChessSever();
    public void mousePressed(MouseEvent e) {
        int x= e.getX();
        int y= e.getY();

        chessSever.playChess(x,y,g);
    }


    public void mouseClicked(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}

绘制窗体的主方法

import java.awt.*;
import javax.swing.*;

public class Chess {

    public void show(){
        Graphics g;
        ChessFrame chessFrame = new ChessFrame();
        chessFrame.setTitle("五子棋");
        chessFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        chessFrame.setBounds(100,100,900,900);
        chessFrame.setLayout(null);
        String[] btnText = {"开始游戏","悔棋"};
        for (int i = 0; i < btnText.length; i++) {
            JButton btn = new JButton(btnText[i]);
            btn.setBounds(850,20+i*40,100,30);

            chessFrame.add(btn);
        }

        chessFrame.setVisible(true);
        Listener listener = new Listener();
        listener.g = chessFrame.getGraphics();
        chessFrame.addMouseListener(listener);
    }

    public static void main(String[] args) {
        Chess chess = new Chess();
        chess.show();
    }
}

好的,以下是一个简单的五子棋 Java 代码示例。 ```java import java.util.Scanner; public class Gobang { public static void main(String[] args) { // 初始化棋盘二维数组 int[][] chessboard = new int[15][15]; // 初始化玩家 int player = 1; // 初始化输入对象 Scanner input = new Scanner(System.in); // 初始化棋子坐标 int x, y; // 游戏循环 while (true) { // 打印当前棋盘状态 printChessboard(chessboard); // 获取当前玩家输入的坐标 System.out.print("玩家" + player + "落子,请输入横坐标和纵坐标(用空格分隔):"); x = input.nextInt(); y = input.nextInt(); // 判断输入的坐标是否合法 if (x < 0 || x >= 15 || y < 0 || y >= 15) { System.out.println("坐标不合法,请重新输入!"); continue; } if (chessboard[x][y] != 0) { System.out.println("该位置已有棋子,请重新输入!"); continue; } // 在棋盘上落子 chessboard[x][y] = player; // 判断游戏是否结束 if (isGameOver(chessboard, x, y)) { System.out.println("玩家" + player + "获胜!"); break; } // 切换玩家 player = 3 - player; } } /** * 打印棋盘状态 */ public static void printChessboard(int[][] chessboard) { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { System.out.print(chessboard[i][j] + " "); } System.out.println(); } } /** * 判断当前局面是否有胜者 */ public static boolean isGameOver(int[][] chessboard, int x, int y) { int player = chessboard[x][y]; // 检查行 int count = 1; for (int i = x - 1; i >= 0; i--) { if (chessboard[i][y] == player) { count++; } else { break; } } for (int i = x + 1; i < 15; i++) { if (chessboard[i][y] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 检查列 count = 1; for (int i = y - 1; i >= 0; i--) { if (chessboard[x][i] == player) { count++; } else { break; } } for (int i = y + 1; i < 15; i++) { if (chessboard[x][i] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 检查左上到右下的斜线 count = 1; for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { if (chessboard[i][j] == player) { count++; } else { break; } } for (int i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) { if (chessboard[i][j] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 检查左下到右上的斜线 count = 1; for (int i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) { if (chessboard[i][j] == player) { count++; } else { break; } } for (int i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) { if (chessboard[i][j] == player) { count++; } else { break; } } if (count >= 5) { return true; } return false; } } ``` 这个五子棋游戏使用的是控制台输入和输出,棋盘大小为 15x15。玩家落子时输入坐标,程序会检查坐标是否合法,如果合法则在棋盘上落子。每次落子后,程序会判断当前局面是否有胜者,如果有,则游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值