java图形界面五子棋源代码共享


有自己的加入也有书本的代码,整合,需要的素材网上找的。

是人机对战,但是没有AI,所以这个五子棋游戏的玩法就有改变了,最短

时间内让电脑也就是白棋获胜就赢了。奋斗(供学习 纪念之用)

import javax.swing.*;
import javax.imageio.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;


public class Gobang2 
{
//下面三个位图分别代表棋盘,黑棋,白棋
BufferedImage table;
BufferedImage black;
BufferedImage white;
//当鼠标移动时的选择框
BufferedImage selected;
//定义棋盘的大小
private static int BOARD_SIZE=15;
//定义棋盘宽,高多少个像素
private final int TABLE_WIDTH=500;
private final int TABLE_HEIGHT=501;
//定义棋盘坐标的像素值和棋盘数组之间的比率
private final int RATE=TABLE_WIDTH/BOARD_SIZE;
//定义棋盘坐标的像素值和棋盘数组之间的偏移距离
private final int X_OFFSET=12;
private final int Y_OFFSET=9;
//定义一个二维数组来当棋盘
private int[][] board=new int[BOARD_SIZE][BOARD_SIZE];
//五子棋游戏窗口
JFrame f=new JFrame("星星*五子棋游戏        玩法:在最短时间内让白棋赢");
//五子棋游戏棋盘对应的Canvas组件
ChessBoard chessBoard=new ChessBoard();
//当前选中点的坐标
private int selectedX=-1;
private int selectedY=-1;
    private Font fontGameOver = new Font("宋体", Font.BOLD, 50);
public void init()throws Exception
{
       ThreadLocalRandom rand= ThreadLocalRandom.current();
       table=ImageIO.read(new File("board.jpg"));
  black=ImageIO.read(new File("black.gif"));
  white=ImageIO.read(new File("white.gif"));
  selected=ImageIO.read(new File("select.gif"));
  //把每个元素赋值为0代表没有棋子
  for(int i=0;i<BOARD_SIZE;i++)
{
              for(int j=0;j<BOARD_SIZE;j++)
 {
 board[i][j]=0;
 }
  }
  chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));//设置组件的首选大小
  chessBoard.addMouseListener(new MouseAdapter()
{
  public void mouseClicked(MouseEvent e)
{
            //将用户鼠标事件的坐标转换成棋子数组的坐标
    int xPos=(int)((e.getX()-X_OFFSET)/RATE);
    int yPos=(int)((e.getY()-Y_OFFSET)/RATE);
board[xPos][yPos]=1;//黑棋
judge(1,xPos,yPos);
           int dxpos=rand.nextInt(0,14);
int dypos=rand.nextInt(0,14);
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java图形化界面五子棋的实现方法: 1. 创建一个棋盘类,用于绘制棋盘和处理子事件。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ChessBoard extends JPanel implements MouseListener { private static final int ROWS = 15; private static final int COLS = 15; private static final int CELL_SIZE = 30; private static final int MARGIN = 30; private static final int GRID_SIZE = CELL_SIZE * (ROWS - 1) + MARGIN * 2; private static final int PIECE_SIZE = (int) (CELL_SIZE * 0.8); private static final int PIECE_OFFSET = (CELL_SIZE - PIECE_SIZE) / 2; private static final int[][] POSITIONS = new int[ROWS][COLS]; private static final int EMPTY = 0; private static final int BLACK = 1; private static final int WHITE = 2; private int currentPlayer = BLACK; private boolean gameOver = false; public ChessBoard() { setPreferredSize(new Dimension(GRID_SIZE, GRID_SIZE)); setBackground(Color.ORANGE); addMouseListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < ROWS; i++) { g.drawLine(MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (COLS - 1) * CELL_SIZE, MARGIN + i * CELL_SIZE); g.drawLine(MARGIN + i * CELL_SIZE, MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (ROWS - 1) * CELL_SIZE); } for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (POSITIONS[i][j] == BLACK) { g.setColor(Color.BLACK); g.fillOval(MARGIN + j * CELL_SIZE + PIECE_OFFSET, MARGIN + i * CELL_SIZE + PIECE_OFFSET, PIECE_SIZE, PIECE_SIZE); } else if (POSITIONS[i][j] == WHITE) { g.setColor(Color.WHITE); g.fillOval(MARGIN + j * CELL_SIZE + PIECE_OFFSET, MARGIN + i * CELL_SIZE + PIECE_OFFSET, PIECE_SIZE, PIECE_SIZE); } } } } public void mouseClicked(MouseEvent e) { if (gameOver) { return; } int x = e.getX(); int y = e.getY(); if (x < MARGIN - PIECE_OFFSET || x > MARGIN + (COLS - 1) * CELL_SIZE + PIECE_OFFSET || y < MARGIN - PIECE_OFFSET || y > MARGIN + (ROWS - 1) * CELL_SIZE + PIECE_OFFSET) { return; } int row = Math.round((float) (y - MARGIN) / CELL_SIZE); int col = Math.round((float) (x - MARGIN) / CELL_SIZE); if (POSITIONS[row][col] != EMPTY) { return; } POSITIONS[row][col] = currentPlayer; repaint(); if (checkWin(row, col)) { gameOver = true; JOptionPane.showMessageDialog(this, (currentPlayer == BLACK ? "Black" : "White") + " wins!"); } else if (checkDraw()) { gameOver = true; JOptionPane.showMessageDialog(this, "Draw!"); } else { currentPlayer = (currentPlayer == BLACK ? WHITE : BLACK); } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} private boolean checkWin(int row, int col) { int count = 1; for (int i = row - 1; i >= 0 && POSITIONS[i][col] == currentPlayer; i--) { count++; } for (int i = row + 1; i < ROWS && POSITIONS[i][col] == currentPlayer; i++) { count++; } if (count >= 5) { return true; } count = 1; for (int j = col - 1; j >= 0 && POSITIONS[row][j] == currentPlayer; j--) { count++; } for (int j = col + 1; j < COLS && POSITIONS[row][j] == currentPlayer; j++) { count++; } if (count >= 5) { return true; } count = 1; for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && POSITIONS[i][j] == currentPlayer; i--, j--) { count++; } for (int i = row + 1, j = col + 1; i < ROWS && j < COLS && POSITIONS[i][j] == currentPlayer; i++, j++) { count++; } if (count >= 5) { return true; } count = 1; for (int i = row - 1, j = col + 1; i >= 0 && j < COLS && POSITIONS[i][j] == currentPlayer; i--, j++) { count++; } for (int i = row + 1, j = col - 1; i < ROWS && j >= 0 && POSITIONS[i][j] == currentPlayer; i++, j--) { count++; } if (count >= 5) { return true; } return false; } private boolean checkDraw() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (POSITIONS[i][j] == EMPTY) { return false; } } } return true; } } ``` 2. 创建一个主类,用于启动程序。 ```java import javax.swing.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Five in a Row"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ChessBoard()); frame.pack(); frame.setVisible(true); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值