当前位置:网站首页>棋类游戏-五子棋小游戏
棋类游戏-五子棋小游戏
2022-08-10 05:32:00 【cbys-1357】
活动地址:CSDN21天学习挑战赛
界面效果:
public class Gobang {
TextArea result=new TextArea();
//定义五子棋游戏窗口
private JFrame f = new JFrame("五子棋游戏");
//声明四个BufferedImage对象,分别记录四张图片
BufferedImage table;
BufferedImage black;
BufferedImage white;
BufferedImage selected;
//声明棋盘的宽和高
final int TABLE_WIDTH = 535;
final int TABLE_HEIGHT = 536;
//声明棋盘横向和纵向分别可以下多少子,他们的值都为15
final int BOARD_SIZE = 15;
//声明每个棋子占用棋盘的比率
final int RATE = TABLE_WIDTH / BOARD_SIZE;
//声明变量,记录棋子对于x方向和y方向的偏移量
final int X_OFFSET = 5;
final int Y_OFFSET = 6;
//声明一个二维数组,记录棋子, 如果索引[i][j]处的值为 0-没有棋子 1-白棋 2-黑棋
int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
//声明红色选择框的坐标 该坐标其实就是二维数组board中的索引
int selected_X = -1;
int selected_Y = -1;
//自定义类,继承Canvas
private class ChessBoard extends JPanel {
@Override
public void paint(Graphics g) {
//绘图
//绘制棋盘
g.drawImage(table,0,0,null);
//绘制选择框
if (selected_X>0 && selected_Y>0){
g.drawImage(selected,selected_X*RATE+X_OFFSET,selected_Y*RATE+Y_OFFSET,null);
}
//绘制棋子
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
//绘制黑棋
if (board[i][j] == 2){
g.drawImage(black,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
//绘制白棋
if (board[i][j] == 1){
g.drawImage(white,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
}
}
}
}
ChessBoard chessBoard = new ChessBoard();
//声明变量,记录当前下棋的颜色
int board_type = 2;
//声明底部需要用到的组件
Panel p = new Panel();
Button whiteBtn = new Button("白棋");
Button blackBtn = new Button("黑棋");
Button deleteBtn = new Button("删除");
public void refreshBtnColor(Color whiteBtnColor,Color blackBtnColor,Color deleteBtnColor){
whiteBtn.setBackground(whiteBtnColor);
blackBtn.setBackground(blackBtnColor);
deleteBtn.setBackground(deleteBtnColor);
}
public void init() throws Exception {
//组装视图,编写逻辑
whiteBtn.addActionListener(e->{
// 修改当前要下的棋子的标志为1
board_type = 1;
// 刷新按钮的颜色
refreshBtnColor(Color.GREEN,Color.GRAY,Color.GRAY);
});
blackBtn.addActionListener(e->{
// 修改当前要下的棋子的标志为2
board_type = 2;
// 刷新按钮的颜色
refreshBtnColor(Color.GRAY,Color.GREEN,Color.GRAY);
});
deleteBtn.addActionListener(e->{
// 修改当前要下的棋子的标志为0
board_type = 0;
// 刷新按钮的颜色
refreshBtnColor(Color.GRAY,Color.GRAY,Color.GREEN);
});
p.add(whiteBtn);
p.add(blackBtn);
p.add(deleteBtn);
f.add(p,BorderLayout.SOUTH);
//组装棋盘
//初始化图片
table = ImageIO.read(new File("src\\awt\\img\\board.jpg"));
white = ImageIO.read(new File("src\\awt\\img\\white.gif"));
black = ImageIO.read(new File("src\\awt\\img\\black.gif"));
selected = ImageIO.read(new File("src\\awt\\img\\selected.gif"));
//处理鼠标移动
chessBoard.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
selected_X = (e.getX()-X_OFFSET)/RATE;
selected_Y = (e.getY()-Y_OFFSET)/RATE;
chessBoard.repaint();
}
});
//处理鼠标点击
chessBoard.addMouseListener(new MouseAdapter() {
//当鼠标被点击后 会调用这个方法
@Override
public void mouseClicked(MouseEvent e) {
int xPos = (e.getX()-X_OFFSET)/RATE;
int yPos = (e.getY()-Y_OFFSET)/RATE;
board[xPos][yPos] = board_type;
if(isOwn(xPos,yPos,1)){
result.append("白棋获胜");
chessBoard.setVisible(false);
p.setVisible(false);
f.add(result);
}
if(isOwn(xPos,yPos,2)){
result.append("黑棋获胜");
chessBoard.setVisible(false);
p.setVisible(false);
f.add(result);
}
chessBoard.repaint();
}
@Override
public void mouseExited(MouseEvent e) {
selected_X=-1;
selected_Y=-1;
chessBoard.repaint();
}
});
chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
f.add(chessBoard);
// 关闭窗口
f.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//设置frame最佳大小并可见
f.pack();
f.setVisible(true);
}
// 某玩家是否赢了
private boolean isOwn(int x,int y,int board_type) {
int count = 1;
int posX = 0;
int posY = 0;
// 水平方向
// 向左遍历
for (posX = x - 1; posX > 0; posX--) {
if (board[posX][y] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
// 向右遍历
for (posX = x + 1; posX <= BOARD_SIZE; posX++) {
if (board[posX][y] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
// 垂直方向
// 重新更count值
count = 1;
// 向上遍历
for (posY = y - 1; posY > 0; posY--) {
if (board[x][posY] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
// 向下遍历
for (posY = y + 1; posY <= BOARD_SIZE; posY++) {
if (board[x][posY] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
// 左下-右上方向
// 重新更新count值
count=1;
// 向左下角遍历
for (posX = x - 1, posY = y - 1; posX > 0 && posY > 0; posX--, posY--) {
if (board[posX][posY] == board_type) {
count++;
if (count >= 5) {
count = 1;
return true;
}
} else {
break;
}
}
// 向右上角遍历
for (posX = x + 1, posY = y + 1; posX <= BOARD_SIZE && posY <= BOARD_SIZE; posX++, posY++) {
if (board[posX][posY] == board_type) {
count++;
if (count >= 5) {
count = 1;
return true;
}
} else {
break;
}
}
// 左上-右下
// 更新count值
count=1;
// 向右下角遍历
for (posX = x + 1, posY = y - 1; posX <= BOARD_SIZE && posY > 0; posX++, posY--) {
if (board[posX][posY] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
// 向左上角遍历
for (posX = x - 1, posY = y + 1; posX > 0 && posY <= 15; posX--, posY++) {
if (board[posX][posY] == board_type) {
count++;
if (count >= 5) {
return true;
}
} else {
break;
}
}
return false;
}
public static void main(String[] args) throws Exception {
new Gobang().init();
}
}
如果有些小伙在运行时,出现下面这个问题。可以把new File()中的相对路径改成关于盘符的绝对路径。
代码中使用的图片在下面的百度网盘中
链接:https://pan.baidu.com/s/1jw8L61lgE7Iy7_9wrfHL-g
提取码:2bw7
边栏推荐
- Module build failed TypeError this.getOptions is not a function报错解决方案
- redis常见的面试题
- 第六次实验
- win12 修改dns脚本
- One step ahead, don't miss it again, the chain reading APP will be launched soon!
- redis---非关系型数据库(NoSql)
- Chain Reading|The latest and most complete digital collection sales calendar-07.29
- IDEA的database使用教程(使用mysql数据库)
- OSPF实验
- 链读|最新最全的数字藏品发售日历-08.02
猜你喜欢
随机推荐
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
Linux数据库Oracle客户端安装,用于shell脚本用sqlplus连接数据库
【写下自用】每次都忘记如何train?记录如何训练自己的yolov5
I use this recruit let the team to improve the development efficiency of 100%!
Content related to ZigBee network devices
Privatisation build personal network backup NextCloud
智能合约和去中心化应用DAPP
Collection工具类
cesium 旋转图片
sqlplus displays the previous command and the available backspace key
深度学习中的学习率调整策略(1)
数据库 笔记 创建数据库、表 备份
笔记1
ResNet的基础:残差块的原理
深度学习中数据到底要不要归一化?实测数据来说明!
文章复现:SRCNN
R绘制图像,图像特征提取
wiki confluence 安装
事务、存储引擎
小程序wx.request简单Promise封装