Java简单五子棋(源码)
Chess
package com.kiligzzz.gobang;
import java.awt.*;
/**
* 五子棋-棋子类
*/
public class Chess {
private int x;
private int y;
private Color color;
public static final int DIAMETER=24;
public Chess(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
DrawPanel
package com.kiligzzz.gobang;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
/**
* 五子棋-棋盘类
*/
public class DrawPanel extends JPanel implements MouseListener{
//棋子坐标
private int x;
private int y;
//是否是黑子
private boolean isBlack = true;
//棋子数组
private Chess[] chessList = new Chess[15*15];
//棋子个数
private int chessCount = 0;
//游戏是否结束
private boolean gameOver = false;
// private boolean BlackVictory = false;
//
// private boolean WhiteVictory = false;
public DrawPanel() {
this.setBackground(Color.LIGHT_GRAY);
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
//画棋盘
for (int i = 1; i <16; i++) {
g.drawLine(35,35*i,525,35*i);
}
for (int j = 1; j <16; j++) {
g.drawLine(35*j,35,35*j,525);
}
g.fillOval(35+35*7-5,35+35*7-5,10,10);
g.fillOval(35+35*2-5,35+35*2-5,10,10);
g.fillOval(35+35*12-5,35+35*2-5,10,10);
g.fillOval(35+35*2-5,35+35*12-5,10,10);
g.fillOval(35+35*12-5,35+35*12-5,10,10);
//画棋子
for (int i = 0; i < chessCount; i++) {
int xPos = chessList[i].getX();
int yPos = chessList[i].getY();
g.setColor(chessList[i].getColor());
g.fillOval(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
//最后落的一个棋子加上红色框
if (i==chessCount-1){
g.setColor(Color.red);
g.drawRect(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
}
}
}
/**
* 自动调用
* 设置当前组件的大小是最佳的大小
* @return
*/
@Override
public Dimension getPreferredSize() {
return new Dimension(35*2+35*14,35*2+35*14);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
//将鼠标点击的坐标位置转换为网格索引
x = (e.getX()-35+35/2)/35*35+35;
y = (e.getY()-35+35/2)/35*35+35;
//判断棋子是否可以下到棋盘上
//游戏结束不能下棋子
if (gameOver){
return;
}
//棋盘外面不能下棋子
if (x > 35+35*14 || y > 35+35*14){
return;
}
//位置上已经有棋子不能下
if (haveChess(x,y)){
return;
}
//棋子对象
Chess chess = new Chess(x,y,isBlack?Color.black:Color.white);
chessList[chessCount++] = chess;
this.repaint();
//当某一方胜利时
if (victory1()||victory2()||victory3()||victory4()){
// if (chess.getColor() == Color.black){
// BlackVictory = true;
// }else {
// WhiteVictory = true;
// }
String msg = String.format("恭喜%s棋胜利!",isBlack?"黑":"白");
JOptionPane.showMessageDialog(this,msg);
gameOver = true;
}
// //如果某一方胜利了
// String msg;
// if (BlackVictory){
// msg="恭喜黑棋胜利!";
// JOptionPane.showMessageDialog(this,msg);
// }
// if (WhiteVictory){
// msg="恭喜白棋胜利!";
// JOptionPane.showMessageDialog(this,msg);
// }
//改变画笔颜色的判定值
isBlack = !isBlack;
}
//位置上是否有棋子
public boolean haveChess(int x,int y){
for (Chess chess : chessList) {
if (chess!=null&&chess.getX()==x&&chess.getY()==y){
return true;
}
}
return false;
}
//判断是否胜利
/**
* 得到棋盘上的棋子
* @param x 本次将要落下的棋子的横坐标
* @param y 本次将要落下的棋子的纵坐标
* @param color 本次将要落下的棋子的颜色
* @return 返回这个棋子
*/
public Chess getChess(int x,int y,Color color){
for (Chess chess : chessList) {
if (chess!=null&&chess.getX()==x&&chess.getY()==y&&chess.getColor()==color){
return chess;
}
}
return null;
}
//上下
public boolean victory1(){
//连续棋子的个数
int continueCount = 1;
//向上寻找
for (int xi=x,yi = y-35; yi>=35; yi-=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
//向下寻找
for (int xi=x,yi = y+35; yi<=35+35*14; yi+=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
return continueCount >= 5;
}
//左右
public boolean victory2(){
//连续棋子的个数
int continueCount = 1;
//向左寻找
for (int xi=x-35,yi = y; xi>=35; xi-=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
//向右寻找
for (int xi=x+35,yi = y; xi<=35+35*14; xi+=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
return continueCount >= 5;
}
//左上-右下
public boolean victory3(){
//连续棋子的个数
int continueCount = 1;
//向左上寻找
for (int xi=x-35,yi = y-35; xi>=35&&yi>=35; xi-=35,yi-=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
//向右下寻找
for (int xi=x+35,yi = y+35; xi<=35+35*14&&yi<=35+35*14; xi+=35,yi+=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
return continueCount >= 5;
}
//右上-左下
public boolean victory4(){
//连续棋子的个数
int continueCount = 1;
//向左下寻找
for (int xi=x-35,yi = y+35; xi>=35&&yi<=35+35*14; xi-=35,yi+=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
//向右上寻找
for (int xi=x+35,yi = y-35; xi<=35+35*14&&yi>=35; xi+=35,yi-=35){
Color color = isBlack?Color.black:Color.white;
if (getChess(xi,yi,color) !=null){
continueCount++;
}else {
break;
}
}
return continueCount >= 5;
}
/**
* 重新开始游戏
*/
public void restartGame(){
//清除棋子
Arrays.fill(chessList, null);
// for (int i = 0; i < chessList.length; i++) {
// chessList[i] = null;
// }
//恢复游戏相关的变量
isBlack = true;
gameOver = false;
chessCount = 0;
//重画棋盘
this.repaint();
}
/**
*
*/
public void regretChess(){
//没有棋子不能悔棋
if (chessCount == 0){
}
chessList[chessCount-1]=null;
chessCount--;
if (chessCount > 0) {
x=chessList[chessCount-1].getX();
y=chessList[chessCount-1].getY();
}
isBlack=!isBlack;
this.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
JFiveFrame
package com.kiligzzz.gobang;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 窗口类
*/
public class JFiveFrame extends JFrame {
/**
* 工具栏里面的三个按钮
*/
private JPanel toolBar;
private JButton startButton,backButton,exitButton;
/**
* 棋盘的面板
*/
private DrawPanel drawPanel;
private JMenuBar menuBar;
private JMenu sysMenu;
private JMenuItem startMenuItem,backMenuItem,exitMenuItem;
private MyListener listener;
public JFiveFrame() {
}
public void init(){
listener = new MyListener();
this.setTitle("单机版五子棋游戏");
toolBar = new JPanel();
startButton = new JButton("开始");
backButton = new JButton("悔棋");
exitButton = new JButton("退出");
drawPanel = new DrawPanel();
menuBar = new JMenuBar();
sysMenu = new JMenu("系统");
JMenu help = new JMenu("帮助");
JMenuItem ang = new JMenuItem("爱死我的小昂了!!!");
startMenuItem = new JMenuItem("开始");
backMenuItem = new JMenuItem("悔棋");
exitMenuItem = new JMenuItem("退出");
//给按钮加监听并变成小手
listen(startMenuItem);
listen(startButton);
listen(backMenuItem);
listen(backButton);
listen(exitMenuItem);
listen(exitButton);
//设置窗口的菜单栏
this.setJMenuBar(menuBar);
menuBar.add(sysMenu);
menuBar.add(help);
help.add(ang);
sysMenu.add(startMenuItem);
sysMenu.add(backMenuItem);
sysMenu.add(exitMenuItem);
toolBar.add(startButton);
toolBar.add(backButton);
toolBar.add(exitButton);
this.setLayout(new BorderLayout());
this.add(toolBar,BorderLayout.NORTH);
this.add(drawPanel,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3,Toolkit.getDefaultToolkit().getScreenSize().height/3-200);
//包裹组件
pack();
this.setVisible(true);
}
/**
* 给按钮加监听并变成小手
*/
public void listen(JButton jButton){
jButton.addActionListener(listener);
jButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void listen(JMenuItem jMenuItem){
jMenuItem.addActionListener(listener);
jMenuItem.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
/**
* 监听按钮
*/
public class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
/**
* 判断事件源来源
*/
if (e.getSource() == startButton || e.getSource() == startMenuItem){
//调用DrawPanel的重新开始游戏方法
drawPanel.restartGame();
}
if (e.getSource() == backButton || e.getSource() == backMenuItem){
//调用DrawPanel的悔棋方法
drawPanel.regretChess();
}
if (e.getSource() == exitButton || e.getSource() == exitMenuItem){
//正常退出
System.exit(0);
}
}
}
}
TestMain
package com.kiligzzz.gobang;
public class TestMain {
public static void main(String[] args) {
JFiveFrame jfiveFrame = new JFiveFrame();
jfiveFrame.init();
}
}