一. 项目简介:
五子棋是全国智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏。五子棋的玩法是:双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成五子连线者获胜。五子棋的棋具与围棋通用,是一种传统的棋种。五子棋容易上手,老少皆宜,而且趣味横生,引人入胜:它不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。
以下是我们用Java语言写出的单机版五子棋游戏的简单介绍。本次课程设计是
二.功能需求分析
1. 游戏的简单流程:
三.思路分析
四个类:
Chess-棋子类
DrawPanel-画棋盘类
FiveFrame-窗口类
TestMain-测试类(主类)
六个模块:
主窗口模块 画棋子模块 棋盘构造模块 按钮模块 鼠标按下模块 判断胜负模块
其中我负责的是 主窗口模块 画棋子模块 和 按钮模块
接下来给大家展示
1.主窗口模块:
public class FiveFrame extends JFrame{
private JPanel toolBar;//工具栏里有三个按钮
private JButton startButton,backButton,exitButton;
private DrawTest drawTest;//棋盒的面板
private JMenuBar menuBar;
private JMenu sysMenu;
private JMenuItem startMenuItem,backMenuItem,exitMenuItem;
private MyListener listener;
首先,创建一个类FiveFrame为窗口类。
再创建一个Jpanel类的toolbar,即面板容器类的工具栏,再创建按钮类的开始,悔棋和退出,即startbutton,backbutton,exitbutton。
再创建一个面板drawtest,再创建jmenubar即菜单栏,创建菜单,创建开始,悔棋,退出键,创建一个监听器。
this.setJMenuBar(menuBar); //设置窗口菜单栏
menuBar.add(sysMenu);
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(drawTest,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
设置一个菜单栏,将sysmenu添加进去,后将三个按钮添加至sysmenu;
同理将三个按钮添加到工具栏;
用setlayout()布局管理器自动调整组件的摆放位置,注意将drawtest置于中间,将toolbar置于上方。
设置关闭窗口时,程序运行自动停止,以及不可自由改变窗口大小。
2.画棋子模块
public class Chess {
private int x;//棋盘中x的索引值
private int y;
private Color color;
public static final int DIANETER=30;
public Chess(int x, int y, Color color) {
super();
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;
}
创建一个chess类,设置变量x,y为棋子位置的索引值,设置颜色变量,设置常量直径为30。
添加一个get方法,再加一个构造方法。
for(int i=0;i<chessCount;i++) {
int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;
int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;
g.setColor(chessList[i].getColor());
g.fillOval(xPos-Chess.DIANETER/2, yPos-Chess.DIANETER/2, Chess.DIANETER, Chess.DIANETER);
//最后一颗棋子上画红色框
if(i==chessCount-1) {
g.setColor(Color.red);
g.drawRect(xPos-Chess.DIANETER/2, yPos-Chess.DIANETER/2, Chess.DIANETER, Chess.DIANETER);
}
}
得到x的下标,y的下标,得到颜色,画出。
如果棋子是最后一颗,即刚画出的那一颗,则画上红色框。
3.按钮模块
public void init() {
listener=new MyListener();
this.setTitle("单机版五子棋游戏");
toolBar=new JPanel();
startButton=new JButton("开始");
startButton.addActionListener(listener);
backButton=new JButton("悔棋");
backButton.addActionListener(listener);
exitButton=new JButton("退出");
exitButton.addActionListener(listener);
drawTest=new DrawTest();
menuBar=new JMenuBar();
sysMenu=new JMenu("系统");
startMenuItem=new JMenuItem("开始");
startMenuItem.addActionListener(listener);
backMenuItem=new JMenuItem("悔棋");
backMenuItem.addActionListener(listener);
exitMenuItem=new JMenuItem("退出");
exitMenuItem.addActionListener(listener);
设置按钮,并且给他们加上监听。
按钮的功能中,悔棋:
public void goback() {
//棋盘中没有棋子不能悔棋
if(chessCount==0) {
return;
}
chessList[chessCount-1]=null;
chessCount--;
if(chessCount>0) {
x_index=chessList[chessCount-1].getX();
y_index=chessList[chessCount-1].getY();
}
isBlack=!isBlack;
this.repaint();
}
若棋盘中没有棋子,则不能悔棋,return;
若棋盘中有棋子,则让最后下的棋子等于null,且让当前棋子的x,y的索引值等于上一个棋子的索引值,将颜色相反,重画。
重新开始:
//重新开始游戏的方法
public void restartGame() {
//清除棋子
for(int i=0;i<chessList.length;i++) {
chessList[i]=null;
}
//恢复游戏相关的变量
isBlack=true;
gameOver=false;
chessCount=0;
this.repaint();
}
将已有的棋子都等于null,将游戏相关变量恢复,重画。