java五子棋代码设计
完成面板
创建一个类Panel用于创建面板
package com.ningmeng;
import javax.swing.*;
import java.awt.*;
//面板
public class Panel extends JFrame {
//创建面板
Panel(){
setVisible(true);//显示窗口
this.setTitle("欢迎来到柠檬的五子棋游戏");//标题
this.setSize(600,700);//设置窗口大小
this.setResizable(false);//禁止改变大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口退出程序
JPanel jPanel = new JPanel();//创建面板组件
JLabel jlabel = new JLabel("鼠标左键下棋,右键悔棋");//创建文本
jPanel.add(jlabel);//将文本放入面板组件里
this.add(jPanel,BorderLayout.NORTH);//将面板组件放入JFrame
}
public static void main(String[] args) {
new Panel();
}
}
设置开始按钮和提示文字
新建一个类Checkerboard用于绘制棋盘
package com.ningmeng;
import javax.swing.*;
//棋盘
//绘画的话需要继承JPanel
public class Checkerboard extends JPanel {
//定义重置按钮
JButton button = new JButton("重置");
//设置文本内容(用来显示谁来下棋)
JLabel jLabel_1 = new JLabel("请黑棋下子");
//设置文本内容(用来显示输赢)
JLabel jLabel_2 = new JLabel("加油哦!!!");
//构造函数
Checkerboard(){
this.setSize(500, 500);//设置大小
this.setLayout(null);//设置布局(绝对布局)
this.add(button);//添加按钮到容器
this.button.setBounds(130, 10, 70, 30);//设置在容器中的坐标和大小
this.add(jLabel_1);//添加文本到容器
jLabel_1.setBounds(230, 10, 100, 30);//设置在容器中的坐标和大小
this.add(jLabel_2);//添加文本到容器
jLabel_2.setBounds(320, 10, 100, 30);//设置在容器中的坐标和大小
}
}
将该内加到面板
package com.ningmeng;
import javax.swing.*;
import java.awt.*;
//面板
public class Panel extends JFrame {
//创建面板
Panel(){
setVisible(true);//显示窗口
this.setTitle("欢迎来到柠檬的五子棋游戏");//标题
this.setSize(600,700);//设置窗口大小
this.setResizable(false);//禁止改变大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口退出程序
JPanel jPanel = new JPanel();//创建面板组件
JLabel jlabel = new JLabel("鼠标左键下棋,右键悔棋");//创建文本
jPanel.add(jlabel);//将文本放入面板组件里
this.add(jPanel,BorderLayout.NORTH);//将面板组件放入JFrame
//下面为添加的代码部分
Checkerboard checkerboard = new Checkerboard();//实例化Checkerboard类
add(checkerboard);//将创建好的
checkerboard.setBounds(70, 90, 440, 440);//设置在容器中的坐标和大小
///
}
public static void main(String[] args) {
new Panel();
}
}
添加网格
在Panel里添加下面的方法
/**
* 创建 Paint类(为画笔类)
* @param g Graphics这个可以理解为笔
* 这里记一下五指棋的格子是15*15的
*/
public void paint(Graphics g) {
super.paint(g);//调用父类函数
//绘制横线
for (int i = 80; i <= 380; i = i + 20) {
g.drawLine(120, i, 420, i);//划线
}
//绘制竖线
for (int j = 120; j <= 420; j = j + 20) {
g.drawLine(j, 80, j, 380);//划线
}
//画点
g.fillOval(257, 217, 6, 6);//中间
g.fillOval(177, 137, 6, 6);//左上
g.fillOval(357, 137, 6, 6);//右上
g.fillOval(177, 317, 6, 6);//左下
g.fillOval(357, 317, 6, 6);//右下
}
实现下棋
首先我们可以先设置一个颜色,比如黑棋为1白棋为-1,默认为黑棋就是1。这样我们下了黑棋传递-1,让-1*1=-1也就是下白棋。
我们先定义出2个棋子
棋子只能在棋盘中那么棋子的位置怎么计算呢?
//用输出语句的方法可以知道现在表格左上点的位置为119和77
//用输出语句的方法可以知道现在表格右下点的位置为419和378
System.out.println("x="+x);
System.out.println("y="+y)
白棋
package com.ningmeng;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//白棋 继承绘图类 实现鼠标监听接口
public class ChessPointWhite extends Canvas implements MouseListener {
//实例化棋盘
Checkerboard checkerboard;
//构造方法 定义旗子
ChessPointWhite(){
this.addMouseListener(this);//设置监听
}
//制作白棋
public void paint(Graphics g) {
g.setColor(Color.white);//设置白棋颜色
g.fillOval(0, 0, 20, 20);//设置白棋的大小
}
public void mouseClicked(MouseEvent e) {
}
//鼠标按键在组件上按下时调用
public void mousePressed(MouseEvent e) {
//判断是不是鼠标左击
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
checkerboard.remove(this);//在棋盘中移除白棋组件
checkerboard.color = 1;//设置黑棋
checkerboard.jLabel_1.setText("请白棋下棋");//设置提示文字
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
黑棋
package com.ningmeng;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//黑棋 继承绘图类 实现鼠标监听接口
public class ChessPointBlack extends Canvas implements MouseListener {
//实例化棋盘
Checkerboard checkerboard;
//构造方法 定义旗子
ChessPointBlack(){
addMouseListener(this);//设置监听
}
//制作黑棋
public void paint(Graphics g) {
g.setColor(Color.black);//设置黑棋颜色
g.fillOval(0, 0, 20, 20);//设置黑棋的大小
}
public void mouseClicked(MouseEvent e) {
}
//鼠标按键在组件上按下时调用
public void mousePressed(MouseEvent e) {
//判断是不是鼠标左击
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
checkerboard.remove(this);//在棋盘中移除黑棋组件
checkerboard.color = -1;//设置白棋
checkerboard.jLabel_1.setText("请黑棋下棋");//设置提示文字
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
下棋
在ChessPointWhite里实现接口MouseListener,并且构造方法里设置监听,重写mousePressed方法,在构造函数里设置监听
package com.ningmeng;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//棋盘
//绘画的话需要继承JPanel 鼠标监听MouseListener
public class Checkerboard extends JPanel implements MouseListener {
//定义重置按钮
JButton button = new JButton("重置");
//设置文本内容(用来显示谁来下棋)
JLabel jLabel_1 = new JLabel("请黑棋下子");
//设置文本内容(用来显示输赢)
JLabel jLabel_2 = new JLabel("加油哦!!!");
int color = 1;//设置棋的颜色默认为黑棋
int x,y;//设置鼠标点击的默认位置
//构造函数
Checkerboard(){
this.setSize(500, 500);//设置大小
this.setLayout(null);//设置布局(绝对布局)
this.add(button);//添加按钮到容器
this.button.setBounds(130, 10, 70, 30);//设置在容器中的坐标和大小
this.add(jLabel_1);//添加文本到容器
jLabel_1.setBounds(230, 10, 100, 30);//设置在容器中的坐标和大小
this.add(jLabel_2);//添加文本到容器
jLabel_2.setBounds(320, 10, 100, 30);//设置在容器中的坐标和大小
//设置监听
addMouseListener(this);
}
/**
* 创建 Paint类(为画笔类)
* @param g Graphics这个可以理解为笔
* 这里记一下五指棋的格子是15*15的
*/
public void paint(Graphics g) {
super.paint(g);//调用父类函数
//绘制横线
for (int i = 80; i <= 380; i = i + 20) {
g.drawLine(120, i, 420, i);//划线
}
//绘制竖线
for (int j = 120; j <= 420; j = j + 20) {
g.drawLine(j, 80, j, 380);//划线
}
//画点
g.fillOval(257, 217, 6, 6);//中间
g.fillOval(177, 137, 6, 6);//左上
g.fillOval(357, 137, 6, 6);//右上
g.fillOval(177, 317, 6, 6);//左下
g.fillOval(357, 317, 6, 6);//右下
}
//鼠标按键在组件上按下时调用。
public void mousePressed(MouseEvent e) {
//判断鼠标是否为单击
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
//x和y和起来代表鼠标点击的位置
x = e.getX();//获取鼠标点击的x位置
y = e.getY();//获取鼠标点击的y位置
//实例化出黑棋和白棋
ChessPointBlack black = new ChessPointBlack();
ChessPointWhite white = new ChessPointWhite();
//设置下棋的范围
if (x < 119 || y <77 || x > 419 || y > 378) {
} else {
if (color == 1) {
//添加黑棋
this.add(black);
//设置黑棋位置
black.setBounds((x + 10) / 20 * 20 - 10, (y + 10) / 20 * 20 - 10, 20, 20);
//设置为白棋
color = color * (-1);
//修改提示位置
jLabel_1.setText("请白棋下子");
} else if (color == -1) {
//添加白棋
this.add(white);
//设置白棋位置
white.setBounds((x + 10) / 20 * 20 - 10, (y + 10) / 20 * 20 - 10, 20, 20);
//设置为黑棋
color = color * (-1);
jLabel_1.setText("请黑棋下子");
}
}
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
判断输赢的方法
定义游戏开始和结束的标记和存放棋子的数组,下棋之后需要保持棋子的位置和判断输赢。
下面为修改好的代码
package com.ningmeng;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//棋盘
//绘画的话需要继承JPanel 鼠标监听MouseListener 事件ActionListener
public class Checkerboard extends JPanel implements MouseListener {
//定义重置按钮
JButton button = new JButton("重置");
//设置文本内容(用来显示谁来下棋)
JLabel jLabel_1 = new JLabel("请黑棋下子");
//设置文本内容(用来显示输赢)
JLabel jLabel_2 = new JLabel("加油哦!!!");
int color = 1;//设置棋的颜色默认为黑棋
int x,y;//设置鼠标点击的默认位置
int[][] allChess = new int[25][25];//定义数组存放棋子
boolean flag = false;//定义一个游戏结束和开始的标记
//构造函数
Checkerboard(){
this.setSize(500, 500);//设置大小
this.setLayout(null);//设置布局(绝对布局)
this.add(button);//添加按钮到容器
this.button.setBounds(130, 10, 70, 30);//设置在容器中的坐标和大小
this.add(jLabel_1);//添加文本到容器
jLabel_1.setBounds(230, 10, 100, 30);//设置在容器中的坐标和大小
this.add(jLabel_2);//添加文本到容器
jLabel_2.setBounds(320, 10, 100, 30);//设置在容器中的坐标和大小
//设置监听
addMouseListener(this);
}
/**
* 创建 Paint类(为画笔类)
* @param g Graphics这个可以理解为笔
* 这里记一下五指棋的格子是15*15的
*/
public void paint(Graphics g) {
super.paint(g);//调用父类函数
//绘制横线
for (int i = 80; i <= 380; i = i + 20) {
g.drawLine(120, i, 420, i);//划线
}
//绘制竖线
for (int j = 120; j <= 420; j = j + 20) {
g.drawLine(j, 80, j, 380);//划线
}
//画点
g.fillOval(257, 217, 6, 6);//中间
g.fillOval(177, 137, 6, 6);//左上
g.fillOval(357, 137, 6, 6);//右上
g.fillOval(177, 317, 6, 6);//左下
g.fillOval(357, 317, 6, 6);//右下
}
//鼠标按键在组件上按下时调用。
public void mousePressed(MouseEvent e) {
if(flag){ //如果为true 游戏结束无法下棋
return;
}
//判断鼠标是否为单击
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
//x和y和起来代表鼠标点击的位置
x = e.getX();//获取鼠标点击的x位置
y = e.getY();//获取鼠标点击的y位置
//实例化出黑棋和白棋
ChessPointBlack black = new ChessPointBlack();
ChessPointWhite white = new ChessPointWhite();
//设置下棋的范围
if (x < 119 || y <77 || x > 419 || y > 378) {
} else {
if (color == 1) {
//添加黑棋
this.add(black);
//设置黑棋位置
black.setBounds((x + 10) / 20 * 20 - 10, (y + 10) / 20 * 20 - 10, 20, 20);
//设置为白棋
color = color * (-1);
//修改提示位置
jLabel_1.setText("请白棋下子");
} else if (color == -1) {
//添加白棋
this.add(white);
//设置白棋位置
white.setBounds((x + 10) / 20 * 20 - 10, (y + 10) / 20 * 20 - 10, 20, 20);
//设置为黑棋
color = color * (-1);
jLabel_1.setText("请黑棋下子");
}
flag = save(x, y, color);//保存棋子判断输赢
}
}
}
//保存下过的棋子
public boolean save(int x, int y,int color){
//计算x和y的位置(这里不能直接用鼠标点击的位置,将x和y设置25的范围内,并且不能为小数)
x = (int) Math.round((x - 40)/20.0);
y = (int) Math.round((y - 40)/20.0);
allChess[x][y] = color;//保存棋子
boolean flag = checkWin(x,y);//调用判断输赢的方法
if(flag == true){
if(color == 1){
jLabel_2.setText("白棋胜利!!!");
}else{
jLabel_2.setText("黑棋胜利!!!");
}
return true;
}
return false;
}
private boolean checkWin(int x,int y) {
boolean flag = false; //判断输赢的时候默认为false
int count = 1;// 保存共有相同颜色多少棋子相连 默认为一个
int color = allChess[x][y];//保存棋子的数组 根据x和y可以取出棋子的颜色
// 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allChess[x][y]中y值是相同(下面的方法思路一样)
// 通过循环来做棋子相连的判断
// 横向的判断
int i = 1;
//判断向左边加棋子
while (color == allChess[x + i][y]) {
count++;
i++;
}
//判断向右边加棋子
i = 1;
while (x-i >= 0 && color == allChess[x - i][y]) {
count++;
i++;
}
//大于大于5获胜
if (count >= 5) {
flag = true;
}
// 纵向的判断
int i2 = 1;
int count2 = 1;
//判断向上下棋
while (color == allChess[x][y + i2]) {
count2++;
i2++;
}
i2 = 1;
//判断向上下棋
while (y >= i2 && color == allChess[x][y - i2]) {
count2++;
i2++;
}
//判断输赢
if (count2 >= 5) {
flag = true;
}
// 斜方向的判断(右上 + 左下)
int i3 = 1;
int count3 = 1;
//判断左下
while (y >= i3 && color == allChess[x + i3][y - i3]) {
count3++;
i3++;
}
//判断右上
i3 = 1;
while (x >= i3 && color == allChess[x - i3][y + i3]) {
count3++;
i3++;
}
//判断输赢
if (count3 >= 5) {
flag = true;
}
// 斜方向的判断(右下 +左上)
int i4 = 1;
int count4 = 1;
//判断左上
while (color == allChess[x + i4][y + i4]) {
count4++;
i4++;
}
i4 = 1;
//判断右下
while (x >= i4 && y >= i4 && color == allChess[x - i4][y - i4]) {
count4++;
i4++;
}
//判断输赢
if (count4 >= 5) {
flag = true;
}
return flag;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
重置按钮
在ChessPointWhite里实现接口ActionListener,并且构造方法里设置监听,重写actionPerformed方法
并且同样在构造函数里设置按钮监听,内容和上面基本一致就复制部分代码了
//设置按钮监听
button.addActionListener(this);
//重置按钮
public void actionPerformed(ActionEvent e) {
this.removeAll();//移除所有
color = 1;//设置为黑棋
this.add(button);//添加按钮到容器
this.button.setBounds(130, 10, 70, 30);//设置在容器中的坐标和大小
this.add(jLabel_1);//添加文本到容器
jLabel_1.setBounds(230, 10, 100, 30);//设置在容器中的坐标和大小
this.add(jLabel_2);//添加文本到容器
jLabel_2.setBounds(320, 10, 100, 30);//设置在容器中的坐标和大小
jLabel_1.setText("请黑棋下子");//设置提示文字
jLabel_2.setText("加油哦!!!");//设置提示文字
}
源码地址
链接:https://pan.baidu.com/s/1CE-v0NdmEODS0bStuufCmQ
提取码:tc29
复制这段内容后打开百度网盘手机App,操作更方便哦